Stephen Stephen - 2 months ago 17
Scala Question

Does Akka Actor run in parallel if we do not define a Router?

I am not quite clear about the Router, Dispatcher, and Executor in Akka system.

If I do not define a router and therefore do not give it a group actors, the actor runs in parallel or not?

If I do not define a router, but define and use a dispatcher as:

my-dispatcher {
type = Dispatcher
executor = "fork-join-executor"
fork-join-executor {
parallelism-min = 2
parallelism-factor = 2.0
parallelism-max = 10
}
throughput = 100
}


The actor runs in parallel or not?

Answer

If I do not define a router and therefore do not give it a group actors, the actor runs in parallel or not?

The Dispatcher in an Actor system is a wrapper over an ExecutionContext which has an underlying pool of threads it executes the actors on. If all you create is a single actor and pass the messages to it, then all messages will arrive to the same actor mailbox which he will process one by one. Execution inside the actor happens synchronously.

If you want multiple actors to treat a request then that is exactly what a Router is for. If you want to have a group of actors which can treat requests the same, you allocate a router and it will manage the group of actors for you, taking care if which actor gets which amount of work.

One example is when you have a router and you define an internal pool of actors, i.e. a RoundRobinPool which simply assigns messages to actor mailboxes in a round robin fashion:

context.actorOf(
  Props(new MyActor())
    .withRouter(RoundRobinPool(nrOfInstances = 10)), name = "MyRouter")

This will create a Router with a RoundRobinPool which contains 10 actors.

Comments