# Typed group route: how to add routees

**URL:** <https://discuss.akka.io/t/typed-group-route-how-to-add-routees/7267>\
**Category:** Akka Cluster\
**Created:** [September 29, 2020, 1:51pm UTC](https://discuss.akka.io/t/typed-group-route-how-to-add-routees/7267 "2020-09-29T13:51:58Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![hmf](https://avatars.discourse-cdn.com/v4/letter/h/91b2a8/32.png) [@hmf](https://discuss.akka.io/u/hmf)\
**Post date:** [September 29, 2020, 1:51pm UTC](https://discuss.akka.io/t/typed-group-route-how-to-add-routees/7267/1 "2020-09-29T13:51:58Z")

</div>

I have tried [this example](https://github.com/akka/akka-samples/tree/2.6/akka-sample-cluster-scala/src/main/scala/sample/cluster/stats) but think that I misunderstand both the example and the documentation. I have tried to adapt the example but get:

```auto
Message [concurrency.StatsWorker$Available] to Actor[akka://WorkStealSystem/user/WorkerRouter#530679135] was dropped. No routees in group router for [ServiceKey[concurrency.StatsWorker$Command](StatsService)]. [1] dead letters encountered. This logging can be turned off or adjusted with configuration settings 'akka.log-dead-letters' and 'akka.log-dead-letters-during-shutdown'.

```

My question is: do I need to use `Routers.pool()` to add _routees_ to the group router or is their some other way to do this? I have the following:

```auto
    val groupRouter: GroupRouter[StatsWorker.Command] = Routers.group(App.StatsServiceKey)
    val serviceRouter: ActorRef[StatsWorker.Command] = ctx.spawn( groupRouter, "WorkerRouter")

    val worker = StatsWorker(serviceRouter)
    val service = ctx.spawn(worker, "WorkerService")

    // published through the receptionist to the other nodes in the cluster
    ctx.system.receptionist ! Receptionist.Register(StatsServiceKey, service)

```

And in the `StatsWorker`I have:

```auto
  def apply(serviceRouter: ActorRef[StatsWorker.Command]): Behavior[Command] =
    Behaviors.setup { ctx =>
      ctx.log.info("Worker starting up")
      ctx.log.info("Sending available")
      serviceRouter ! Available(ctx.self)
      ctx.log.info("Waiting for job")
      waitJob(ctx)
  }

```

which results on the above log message.

My objective is to have a set of actors register themselves so that they be contacted by another set of actors. More concretely can I assume that if I use:

```auto
    val groupRouter: GroupRouter[StatsWorker.Command] = Routers.group(App.StatsServiceKey)
    val serviceRouter: ActorRef[StatsWorker.Command] = ctx.spawn( groupRouter, "WorkerRouter")

    val worker = AnotherWorker(serviceRouter)
    val service = ctx.spawn(worker, "WorkerService")

```

the `AnotherWorker` actor can send messages to the group?

My apologies if I am completely off the mark, but I am a beginner.  
TIA

---

<div class="post-metadata">

**Author:** ![johanandren](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.akka.io/johanandren/32/85_2.png) [@johanandren](https://discuss.akka.io/u/johanandren)\
**Post date:** [September 30, 2020, 9:19am UTC](https://discuss.akka.io/t/typed-group-route-how-to-add-routees/7267/2 "2020-09-30T09:19:38Z")

</div>

Registration to the receptionist (actor messaging) and spawning actors is async, in addition to this also the group router picking up what actors are registered for the `ServiceKey` is async. This means you cannot make assumptions about in which order these things happen. When the `StatsWorker` is started, the router may or may not know of any routees yet, as you noticed with the log entry. To build a robust application your worker needs to handle both scenarios, for example by retrying that `Available` request periodically if it does not get any work.

In addition to this there is something strange with your logic, the `StatsWorker` is both handed the router and registered with the service key the router is created with, which means the `Available` message would be sent to itself through the router? Isn’t that intended to go to some form of work manager rather?

---

<div class="post-metadata">

**Author:** ![hmf](https://avatars.discourse-cdn.com/v4/letter/h/91b2a8/32.png) [@hmf](https://discuss.akka.io/u/hmf)\
**Post date:** [September 30, 2020, 5:09pm UTC](https://discuss.akka.io/t/typed-group-route-how-to-add-routees/7267/3 "2020-09-30T17:09:09Z")

</div>

Appreciate the help.

> [@johanandren](#):
>
> In addition to this there is something strange with your logic, the `StatsWorker` is both handed the router and registered with the service key the router is created with, which means the `Available` message would be sent to itself through the router?

Just testing - I would use `AnotherWorker`. My problem is spawning the routees. Hence the question about the `Routers.pool()`. I was under the impression that I could spawn various `StatsWorker` as above in separate VMs and the send would be automatically routed to these.

---

<div class="post-metadata">

**Author:** ![johanandren](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.akka.io/johanandren/32/85_2.png) [@johanandren](https://discuss.akka.io/u/johanandren)\
**Post date:** [October 1, 2020, 1:54pm UTC](https://discuss.akka.io/t/typed-group-route-how-to-add-routees/7267/4 "2020-10-01T13:54:59Z")

</div>

Adding actors for a service key on any node in the cluster _eventually_ leads to a group router for that service key routing to them so you have understood that correctly.

Note that there is no delivery guarantee, the group router list state can be empty because it did not yet see any registration from the receptionist and in that case messages will end up in dead letters, which is what you see in your log.

If the chosen routee is on another cluster node the message can also get lost on the way to the remote node, or the routee could crash with the message still in its inbox. This is an important aspect of Akka to understand. You can read more about it in the docs under [Message Delivery Reilability](https://doc.akka.io/docs/akka/current/general/message-delivery-reliability.html)

---

<div class="post-metadata">

**Author:** ![hmf](https://avatars.discourse-cdn.com/v4/letter/h/91b2a8/32.png) [@hmf](https://discuss.akka.io/u/hmf)\
**Post date:** [October 1, 2020, 2:15pm UTC](https://discuss.akka.io/t/typed-group-route-how-to-add-routees/7267/5 "2020-10-01T14:15:45Z")

</div>

> [@johanandren](#):
>
> Adding actors for a service key on any node in the cluster _eventually_ leads to a group router for that service key routing to them so you have understood that correctly.

Ah, ok.

> [@johanandren](#):
>
> Note that there is no delivery guarantee, the group router list state can be empty because it did not yet see any registration from the receptionist and in that case messages will end up in dead letters, which is what you see in your log.

Ok, makes sense now.

Thank you.
