Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

scala

akka

actor

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?

like image 727
Stephen Kuo Avatar asked Feb 07 '23 02:02

Stephen Kuo


2 Answers

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.

like image 59
Yuval Itzchakov Avatar answered Feb 08 '23 16:02

Yuval Itzchakov


Messages are executed serially inside an Actor

thats why it is said "one actor is no actor"

When a message is sent to the actor. It lands inside the actor mail box. Messages in the mail box are processed one after the other in a serial manner. One important thing to note here is that actor ensures that each message behaviour is executed in a single thread it could be different thread.

So routers creates groups of actors and routes the messages to group of actors so that each actor processes it simultaneously and in parallel fashion

like image 35
pamu Avatar answered Feb 08 '23 15:02

pamu