Akka Router

Akka Router

Intro #

In Akka, Router routes messages to destination Akka Actors called routees that can process messages in parallel to improve throughput. The way routing is done may be configured to fit the use-case.

Routing Strategies #

Determines how router routes to its routees.

  • RandomRoutingLogic: Pure random and fast. May be unbalanced.
  • RoundRobinRoutingLogic: Take turns. Is more fair and distributed. But depending on the messages, some actors may have more work than others.
  • SmallestMailboxRoutingLogic: Tracks the size of the mailbox.
  • ConsistentHashingRoutingLogic: Messages go to a specific routee.
  • BroadcastRoutingLogic: Sends to all routees.
  • ScatterGatherFirstCompletedRoutingLogic: Similar to broadcast but looks for the first response. This optimizes speed as the picks the first one.
  • TailChoppingRoutingLogic: Similar to previous but delays a bit before sending to the next routee.

Types of Router #

How the routees are managed. In this case, the message delivery is optimized:

  • Messages are not enqueued into the router’s mailbox but are delivered directly to the routees.
  • but delive

Pool Router #

Creates and supervises a number of routees according to a configuration. E.g., create 5 routees. It can be configured to dynamically adjust the number of routees.

Group Router #

Router is configured to route to existing actors (routees). Each actor is supervised by the parents and not by the router as opposed to previous type. In this case we need to especify the routees and will find all that match.

Special Messages #

  • PoisonPill: Is never send to the routee. In this case, this only kills the router. The outcome of the routees will depend on the type of router. On Pool Router (routees are supervised by the router), they are also go with it. On Group Router the router will kill but the routees will not because are not supervised by it.
  • Kill: Ditto.
  • Broadcast: Delivers to all routees regardless of the routing logic. We can use it to stop all routees by wrapping a PoisonPill inside a Broadcast message.