concept router in category akka

This is an excerpt from Manning's book Reactive Applications with Akka .NET.
The introduction of a router allows you to abstract away multiple individual actor instances and treat them all as a single actor, given that you direct messages to the router rather than to each actor sitting behind the router. The use of routers as a means of parallelizing trivial workloads is a common pattern, and you’ll see how to use one in chapter 6, when we look at using routers to build scalable applications that respond to an increased number of messages by forwarding messages to other targets.
You need a way to distribute work evenly across a number of worker actors. Akka.NET takes care of this with routers. The concept of a router is quite simple: it wraps a number of actors to use as a single target to which messages can be sent. Messages can then be distributed to all workers, as specified in the routing strategy. Routers can be configured either mostly through configuration or entirely in code. Distributing work to all actors referenced by the router is referred to as broadcast in Akka.NET, and the broadcast pool provides the infrastructure to distribute work in this way (see figure 7.3).
You can use HOCON configuration to configure the router. In chapter 5, you saw the HOCON format and how it’s used in Akka.NET to allow for the configuration of core Akka.NET features. To define a router in HOCON, you create a HOCON element, with the element key being the path to the router. This path has the same syntax as actor addresses shown in figure 3.3, so you can use wildcards in the address if the router is nested under multiple other actors. Every router configuration needs at least the type of router; but routers may also need additional configuration.
The router broadcast-pool defines that when the router is created, it should use the specified routing logic. You also need to specify the number of worker actors that are deployed along with the router, in this case, the number of workers that will be processing messages simultaneously. In the following code, you create a router at the /LoadGenerator route and specify that it should create 10 workers. The terminology used in Akka.NET for the workers in a router is routes, and that terminology is used in the router definition:
akka.actor.deployment { /LoadGenerator { router = broadcast-pool nr-of-instances = 10 } }It’s not enough to define the router in HOCON; you also need to deploy it in the actor system. To do that, you create the Props for the given actor you want to deploy, as you saw in chapter 5.
One use for the broadcast router is as a tool for load-testing services, because with it you can execute a number of requests to the service simultaneously. You create a load-testing actor and the Props to deploy it. Once you’ve got the basic actor Props, you need to specify the router to use. You use the WithRouter method to achieve this and create a new Props object containing the extra routing information. In this case, because the routing information is referenced in the configuration, you need to tell Akka.NET that it should look in the configuration file for the specified definition, by passing an instance of the FromConfig class. Akka.NET looks up the definition based on the address to which the router is being deployed:
var loadTestingProps = Props.Create<LoadTestingActor>() .WithRouter(FromConfig.Instance);It’s also possible to create the router entirely in code without using a configuration file. When creating the Props for the actor and specifying the router, you can provide an instance of the routing logic to use. In the case of the broadcast pool, you can create an instance of the BroadcastPool router with the number of routes to use. You then deploy that router in the same way as the configuration approach. The following code creates a router instance and uses that directly in the Props. The router is then deployed, with 10 routees to which it can distribute work:
var loadTestingProps = Props.Create<LoadTestingActor>() .WithRouter(new BroadcastPool(10));A router is itself a generalized actor and exists in the actor system exactly the same as any actor that you might write. This means that an actor can send it a message, and it will forward the message to the routes dependent on the routing logic. In the broadcast example, it will distribute the message to each routee. The router can be referenced in the same way as any other actor; also, messages are sent to it same way—with the Tell method:
loadTestingActor.Tell(new LoadTestingActor.WebsiteStressTestMessage)This is all that’s required to instantiate a router that creates a number of routee actors to which messages are distributed. The broadcast logic is just one example of such a router, and there are many others included in Akka.NET, which we’ll come to later in the chapter. But, for now, you’re able to deploy a router into the actor system and send messages to routees.

This is an excerpt from Manning's book Akka in Action.
This chapter will just get you acquainted with scaling out your apps; later chapters will round out your knowledge. For example, in chapter 9 we’ll use routers to distribute the load over several actors that can be remote actors, and chapter 13 will introduce you to clustering. Chapter 13 will dive into the details of scaling out once you’re more familiar with how to build a real-world Akka application.
Routers are essential when you want to scale up or out. For example, when you want to scale up, you need multiple instances of the same task, and routers will decide which instance will process the received message. We’ll start this chapter by describing the enterprise router pattern and examine the three reasons for using routing to control message flow:
One of the reasons to use a router is to balance the load over different actors, to improve the performance of the system when processing a lot of messages. This can be local actors (scale up) or even actors on remote servers (scale out). Part of the core Akka argument for using scaling is easy routing.
In our camera example, the recognize step takes a relatively long time to process. To be able to parallelize this task, we use a router.
In figure 9.2 you see that the router is able to send the message to one of the GetLicense instances. When a message is received by the router, the router picks one of the available processes and sends the message only to that process. When the next message is received, the router picks another process to handle it.
To implement this router, we’ll use the built-in router functionality of Akka. In Akka a separation is made between the router, which contains the routing logic, and the actor that represents the router. The router logic decides which routee is selected and can be used within an actor to make a selection. The router actor is a self-contained actor that loads the routing logic and other settings from configuration and is able to manage the routees itself.
Using a pool router is simple, and is the same for all the different pool routers. There are two different ways to use the pool: using the configuration, or configuring it within the code. We’ll start with the configuration, because using it allows you to change the logic used by the router, which isn’t possible when configuring the router in code. Let’s use a BalancingPool for our GetLicense actor.
We have to create the router in the code. The router is also an ActorRef, which we can use to send our messages.
Listing 9.1. Creating a router using the configuration file
val router = system.actorOf( #1 FromConfig.props(Props(new GetLicense(endProbe.ref))), #2 "poolRouter" #3 ) #1 - Defines router using configuration #2 - How router should create routees #3 - Name of router !@%STYLE%@! {"css":"{\"css\": \"font-weight: bold;\"}","target":"[]"} !@%STYLE%@!