Play & Akka and blocking threads to access the database

I want to make a call to a database with a lot of data, and it may take some time to return.

I plan to do this work inside an Akka.future (f) call and use Async {} to display the answer when the work is done.

Does it make sense to do this, or should I just make a long database call in the controller without sending Akka work?

Or is there a way to make access to the database without blocking?

+7
source share
3 answers

If the response is completed after the database call is completed, then it is only useful to make it asynchronous if you can do other work to assemble the response during the call.

Non-blocking database access can mean a couple of different things: a client library that gives you a callback-based API that will be very similar to a solution in the future, or one that uses non-blocking sockets to save on threads. I assume that you mean the first, and in this case, I think it will be functionally equivalent to using the future.

+1
source

If you are forced to use a lock driver for your database (if for some reason the async driver for MySQL does not work), consider creating an Actor pool (using routing) using PinnedDispatcher.

PinnedDispatcher provides a thread for each actor and, setting up the router, will give you the opportunity to configure the number of threads that are strictly responsible for processing database calls. Simple scaling. In addition, with the help of Actors, you can structure messages between participants (for example, a message that has the results of a database call), a little easier.

+4
source

You can use Akka.future(f) and provide your own Akka configuration file to get more threads to handle database access. See this configuration file for example.

But you pointed it out: the real problem is using a database driver that blocks. I do not know which database you are using, but it is worth taking a look at MongoDB using ReactiveMongo . With ReactiveMongo, all MongoDB operations are completely non-blocking and asynchronous. There is a good introduction here . Also, it works very well with the Play Framework (check out ReactiveMongo Play Plugin ).

EDIT: you can also check " " Configuring the Akka internal system for Playframework "to configure the workflow number.

+2
source

All Articles