3
votes

If you want to execute long running computations concurrently (on a single machine), Akka actors can help.

One approach is to spawn a new actor for each piece of work. Something like

while(true) {
    val actor = system.actorOf(Props[ProcessingActor])
    (actor ? msg).map { 
        ... 
        system.stop(actor)
    }
}

A second idea is to configure a set number of actors behind a router. And then send all messages to the router.

val router = system.actorOf(Props[ProcessingActor].withRouter(RoundRobinRouter(nrOfInstances = 5)))
while(true) {
    (router ? msg).map { ... }
}

I wonder, which is better if the system is overloaded (rate of incoming messages is higher than processing rate)?

Which will last longer? And will both eventually blow up the system with an OOMError?

1

1 Answers

4
votes

Before you create a new Actor for each task you could also just use a Future. It really depends on what you want to achieve. To get as much work done with the least memory usage, you should use the actor/router approach. Futures are more expensive, because for each task would create a new instance of Future and Promise. But it really depends on your use case, which approach is the better. I just wouldn't create a lot of actors, when there really is no need for them. Especially as system.actorOf always creates a new error kernel.