Reputation: 103
I am new to stackoverflow, and I have been trying to wrap my head around how exactly "Akka Actors" and this asynchronous communication works. So basically, my webapp will (potentially) have thousands of users communicating with it, asking for and writing a database I have. Basic persistent webapp...
So basically, I have heard numerous times that Async IO is the way to go to build this kind of a solution, and of course Akka is the place to go. My question really is,
If an Akka actor can only process one message at a time, what exactly is the whole benefit here, am I missing something, does the Akka.ActorSystem automatically create and destroy actors based on # of threads in the computer?
On top of that, if some clients have rather database exhaustive tasks, that would essentially block up on of the actors until it can send a proper response right? Is this were futures come in, I am failing to really understand what futures are. If you have to call future.get() (which is a blocking operation) in the end anyways... what is the point?
I am currently using Scalatra 2.3, which is a bit annoying because all their tutorials say Akka is inbuilt meanwhile they removed it in Scalatra 2.3, in favor of scala.concurrent, which according to the Scala 2.11.x release notes, Actors have been removed in favor of Akka. So this is all just contradicting itself and I am finding myself increasingly confused as to what to use, how to use it, what it does, what is the advantage, etc. If anyone could please help clarify these questions, I would love to hear and learn. Thanks so much!
Upvotes: 1
Views: 1218
Reputation: 4359
Yes you are right that an actor can process a single message at a time but you can optimize this in a couple of ways which i'll try to explain. If an actor is busy processing a message then all the incoming messages will be queued in a buffer. But if the rate of the messages is too high then the messages would eventually tend to drop. You can use mailboxes for this purpose in order to avoid this and distribute your task to several actors to avoid congestion. This link would provide you more information on mailboxes.
About creating actor, you can design your system in such a way that it would spawn actors for every task you want to perform and when that task completes then the actor could send a kill signal to itself or a master actor can tell the actor to kill itself. For an example of such a system you can view this as it would clear a lot of things for you.
About making actors work asynchronously without blocking you can do that using futures by utilizing neat tricks like this one for example:
def myFuncInActor( num1: Int,num2: Int): Unit = {
Future { // actor doing a task asynchronously
val sum = num1 + num2
val avg = sum/2
// anything you want to do here
myCaseClass(avg)
} pipeTo self // actor sending the result to itself
}
Well if you are not satisfied with using akka actors, you can also do all this using zeroMQ. I think that you should take at it too. I hope that you find this information helpful.
Upvotes: 1