Concept
A router is an actor that fans out messages to a pool of routees using a routing strategy. You send messages to the router; it forwards them to one or more routees according to the configured strategy. Routers are not cluster-relocatable. If the host node leaves or crashes, the router and its routees are not automatically re-spawned elsewhere.Sending Messages
Messages to a router must be wrapped in aBroadcast envelope. Use Tell to send:
Routing Strategies
Standard Strategies (Fire-and-Forget)
These strategies never wait for replies. They are fire-and-forget; routees process messages asynchronously.| Strategy | Behavior |
|---|---|
| RoundRobin | Rotate through routees in order |
| Random | Pick a random routee |
| FanOut | Send to all routees concurrently (default) |
Reply-Based Strategies
When you need the router to observe replies and relay the first successful response back to the sender, use one of these. Outcomes are delivered asynchronously to the sender as ordinary messages (not viaAsk).
| Strategy | Behavior |
|---|---|
| Scatter-Gather First | Sends the request to all routees concurrently. The first successful reply within the time budget is forwarded to the original sender. Late replies are ignored. If no routee replies in time, the sender receives a StatusFailure. |
| Tail-Chopping | Probes one routee at a time (in random order). If no response arrives before the interval, sends to the next routee. Repeats until a routee replies, all routees are tried, or the global deadline expires. First success wins; otherwise the sender receives a StatusFailure. |
Spawning a Router
UseSpawnRouter on the actor system:
nameβ Router actor namepoolSizeβ Number of routees to spawn (must be > 0)routeesKindβ Actor type for routees (a pointer to a struct implementingActor)optsβ Router options (see below)
Router Options
| Option | Description |
|---|---|
WithRoutingStrategy(strategy) | Set RoundRobin, Random, or FanOut. Default is FanOut. |
AsScatterGatherFirst(within) | Use scatter-gather-first. within is the total time budget for any reply. |
AsTailChopping(within, interval) | Use tail-chopping. within is the global deadline; interval is the delay between successive attempts. Requires interval > 0 and within > 0; typically interval < within. |
WithRestartRouteeOnFailure(maxRetries, timeout) | On routee panic: restart the routee (up to maxRetries within timeout). |
WithStopRouteeOnFailure() | On routee panic: stop and remove the routee from the pool. Default. |
WithResumeRouteeOnFailure() | On routee panic: resume the routee (keep state, continue processing). |
Examples
Control Messages
Routers accept these control messages viaTell or Ask:
| Message | Usage | Response |
|---|---|---|
GetRoutees | Query current routee names | Routees (use Ask) |
AdjustRouterPoolSize | Scale pool at runtime | None (fire-and-forget) |
GetRoutees
AdjustRouterPoolSize
poolSize > 0: Add that many routeespoolSize < 0: Remove that many routeespoolSize == 0: No-op
Handling Replies (Scatter-Gather / Tail-Chopping)
For reply-based strategies, the sender receives either:- The first successful reply from a routee (as a normal message)
- A
StatusFailureif the deadline expires or all routees fail
Receive:
When to Use
- Load balancing β RoundRobin or Random across a pool of workers
- Parallel processing β FanOut for pub/sub, cache invalidation, multi-sink processing
- Fastest responder β Scatter-Gather First when you want the first successful reply
- Controlled probing β Tail-Chopping when you prefer sequential probing with bounded fan-out
Routee Supervision
When a routee panics, the router receives aPanicSignal and applies the configured directive:
- Stop (default): Terminate the routee and remove it from the pool
- Restart: Restart the routee (with retry limits)
- Resume: Keep the routee and continue processing