Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Importance of Akka Routers

Tags:

scala

akka

actor

I have this lingering doubt in my mind about the importance of Akka Routers. I have used Akka Routers in the current project I am working on. However, I am a little confused about the importance of it. Out of the two below methods, which is more beneficial.

  1. having routers and routees.
  2. Creating as many actors as needed.

I understood that router will assign the incoming messages among its routees based on the strategy. Also, we can have supervisor strategy based on the router. I have also understood that actors are also lightweight and it is not an overhead to create as many actors as possible. So, we can create actors for each of the incoming messages and kill it if necessary after the processing si completed.

So I want to understand which one of the above design is better? Or in other words, in which case (1) has advantage over (2) OR vice versa.

like image 747
Yadu Krishnan Avatar asked May 05 '15 04:05

Yadu Krishnan


1 Answers

Good question. I had similar doubts before I read Akka documentation. Here are the reasons:

  1. Efficiency. From docs:

    On the surface routers look like normal actors, but they are actually implemented differently. Routers are designed to be extremely efficient at receiving messages and passing them quickly on to routees.

    A normal actor can be used for routing messages, but an actor's single-threaded processing can become a bottleneck. Routers can achieve much higher throughput with an optimization to the usual message-processing pipeline that allows concurrent routing. This is achieved by embedding routers' routing logic directly in their ActorRef rather than in the router actor. Messages sent to a router's ActorRef can be immediately routed to the routee, bypassing the single-threaded router actor entirely.

    The cost to this is, of course, that the internals of routing code are more complicated than if routers were implemented with normal actors. Fortunately all of this complexity is invisible to consumers of the routing API. However, it is something to be aware of when implementing your own routers.

  2. Default implementation of multiple routing strategies. You can always write your own, but it might get tricky. You have to take into account supervision, recovery, load balancing, remote deployment, etc.

  3. Akka Router patterns will be familiar to Akka users. If you roll-out your custom routing then everyone will have to spend time understanding all corner cases and implications (+ testing? :)).

TL;DR If you don't care about efficiency too much and if it's easier for you to spawn new actors then go for it. Otherwise use Routers.

like image 128
yǝsʞǝla Avatar answered Oct 19 '22 23:10

yǝsʞǝla