# How is entityId past to entity in cluster sharding?

**URL:** <https://discuss.akka.io/t/how-is-entityid-past-to-entity-in-cluster-sharding/10015>\
**Category:** Akka Cluster\
**Created:** [October 11, 2022, 6:18pm UTC](https://discuss.akka.io/t/how-is-entityid-past-to-entity-in-cluster-sharding/10015 "2022-10-11T18:18:27Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Casperlet](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.akka.io/casperlet/32/2261_2.png) [@Casperlet](https://discuss.akka.io/u/Casperlet)\
**Post date:** [October 11, 2022, 6:18pm UTC](https://discuss.akka.io/t/how-is-entityid-past-to-entity-in-cluster-sharding/10015/1 "2022-10-11T18:18:27Z")

</div>

It’s piece picked from the document of Akka cluster sharding. I get confused with how is the actual value of parameter `entityId` past to context.

```auto
class HelloWorldService(system: ActorSystem[_]) {
  private val sharding = ClusterSharding(system)

  sharding.init(Entity(typeKey = HelloWorld.TypeKey) { entityContext =>
    HelloWorld(entityContext.entityId, PersistenceId(entityContext.entityTypeKey.name, entityContext.entityId))
  })

  def greet(worldId: String, whom: String): Future[Int] = {
    val entityRef = sharding.entityRefFor(HelloWorld.TypeKey, worldId)
    val greeting = entityRef ? HelloWorld.Greet(whom)
    greeting.map(_.numberOfPeople)
  }
}

object HelloWorld {
  sealed trait Command extends CborSerializable

  val TypeKey: EntityTypeKey[Command] = EntityTypeKey[Command]("HelloWorld")

  def apply(entityId: String, persistenceId: PersistenceId): Behavior[Command] = {
    Behaviors.setup { context =>
      context.log.info("Starting HelloWorld {}", entityId)
      EventSourcedBehavior(persistenceId, emptyState = KnownPeople(Set.empty), commandHandler, eventHandler)
    }
  }
}

```

The `sharding.init(Entity(entityKey)(createBehavior))` will create entities dynamically. I suppose that `sharding.init()` is a registering method, the `createBehavior` will be called once whenever a node creating. The `createBehavior` is a factory, it picks `entityId` from the entityContext. And the `sharding.entityRefFor(entityKey, entityId)` will trigger the call to `createBehavior` and pass the `worldId` as `entityId` to entityContext.

Is it right? Thanks.

---

<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 12, 2022, 9:15am UTC](https://discuss.akka.io/t/how-is-entityid-past-to-entity-in-cluster-sharding/10015/2 "2022-10-12T09:15:02Z")

</div>

`sharding.init` sets up sharding on a node for the given type of entity, it is run on each cluster node as bootstrapping.

Sharding will start (and passivate) entities on some node of the cluster, when needed, making sure there is at most one entity for a given persistence id at any point in time. By default the creation/start of the entity actor is only triggered by an incoming message for the entity.

Messaging entities running in sharding is done through a sharding proxy actor (usually the “shard region” actor) on the sending node, this proxy actor knows which node the shard the entity belongs to entity is on and forwards messages to the right node. On the right node, sharding keeps track of what shards and in those what entities are alive, if the entity is not yet started, your behavior factory method is invoked to get a `Behavior` to start.

The `EntityRef` is a thin facade for this proxy, making it look like a bit like an `ActorRef` but it does in fact only track the entity id and how to construct an envelope containing the message and the persistence id before handing that to the proxy. This envelope is unwrapped on the receiving side before the message is passed to the entity itself.

The `PersistenceId` will usually contain both the unique id of the entity and the type of entity, so that multiple types of entities can live in the same journal without entity id conflicts.

Edit: added some clarifications about delivery and the envelope

---

<div class="post-metadata">

**Author:** ![Casperlet](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.akka.io/casperlet/32/2261_2.png) [@Casperlet](https://discuss.akka.io/u/Casperlet)\
**Post date:** [October 24, 2022, 5:58am UTC](https://discuss.akka.io/t/how-is-entityid-past-to-entity-in-cluster-sharding/10015/3 "2022-10-24T05:58:41Z")

</div>

@johanandren Thank you.

According to the example offered by Lightbend on [Github](https://github.com/akka/akka/blob/v2.6.20/akka-cluster-sharding-typed/src/test/scala/docs/akka/cluster/sharding/typed/HelloWorldPersistentEntityExample.scala#L20-L42), I suppose the call stack in the simple way as below.

1. The `HelloWorldService` registers factory method of entity `HelloWorld` by `sharding.init()`, and shows API `greet(worldId, whom)`.
2. At the beginning, the call to `greet(worldId = "mars", whom)` will refer to `sharding.entityRefFor(HelloWorld.TypeKey, worldId = "mars")`.
3. Because no entity corresponding to `worldId = "mars"` existed, then sharding will pass the `"mars"` to the factory method in step-1.
4. The factory passes `"mars"` to `HelloWorld.apply(entityId = "mars", persistenceId)`, to construct a `HelloWorld` entity object corresponding to `"mars"`, and shard it in the cluster.
5. When `sharding.entityRefFor("mars")` returns `entityRef`, the `entityRef ? HelloWorld.Greet(whom)` will ask the entity object with command `Greet(whom)`.
6. After then, any call to `greet(worldId = "mars", whom)` will locate the entity object sharded in step-4 by `sharding.entityRefFor()` directly, but no construction any more.

How about this? Thanks.

---

<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 24, 2022, 6:44am UTC](https://discuss.akka.io/t/how-is-entityid-past-to-entity-in-cluster-sharding/10015/4 "2022-10-24T06:44:46Z")

</div>

Almost correct. What is missing is that the start of the actor is only triggered once the shard receives the message for it, not when the sending side creates the `EntityRef`. On message send the `EntityRef` wraps the message in an envelope together with the entity id, and then hands it to the local shard region (or proxy).

The local shard region already knows, or if it doesn’t, communicates with the shard coordinator to find out, which node in the cluster the shard of the recipient belongs to, and forwards the message there.

When the shard receives a message for an entity id that is not currently running, it is started using the factory provided when initialising cluster sharding.

---

<div class="post-metadata">

**Author:** ![Casperlet](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.akka.io/casperlet/32/2261_2.png) [@Casperlet](https://discuss.akka.io/u/Casperlet)\
**Post date:** [October 24, 2022, 1:12pm UTC](https://discuss.akka.io/t/how-is-entityid-past-to-entity-in-cluster-sharding/10015/5 "2022-10-24T13:12:11Z")

</div>

@johanandren Thank you again.

I focus on the deploying micro-services with Docker and Kubernetes which is developed with Akka framework.

Basically, I use the `EventSourcedBehavior` and an SQL database to implement CQRS pattern in the write side, and publish the entities to the cluster. In the read side, I pull events by the projection with Kafka or any else, and materialize them with a NoSQL database.

The Akka framework reduces the cost of maintaining message queue, concurrent conflict and persistence, etc. I really like it, but I didn’t find any book about Akka Typed except the online official document. The document seems to be a reference, it takes me too much time, maybe more later.

Best regards

---

<div class="post-metadata">

**Author:** ![franciscolopezsancho](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.akka.io/franciscolopezsancho/32/2587_2.png) [@franciscolopezsancho](https://discuss.akka.io/u/franciscolopezsancho)\
**Post date:** [October 25, 2022, 6:50am UTC](https://discuss.akka.io/t/how-is-entityid-past-to-entity-in-cluster-sharding/10015/6 "2022-10-25T06:50:26Z")

</div>

Hi @Casperlet  
I’m writing Akka in Action 2nd edition with Manning. It’s close to being finished and it has a section on Akka Sharding. We have released that chapter in an early [release](https://www.linkedin.com/feed/update/urn%3Ali%3Aactivity%3A6978044240526237696/?midToken=AQE__dfhlOuBSg&midSig=1v2TmG_SG06qs1&trk=eml-email_notification_single_mentioned_you_in_this_01-notifications-1-hero%7Ecard%7Efeed&trkEmail=eml-email_notification_single_mentioned_you_in_this_01-notifications-1-hero%7Ecard%7Efeed-null-4vgb3v%7El8aha9dx%7Et6-null-voyagerOffline) in case you are interested.

Kind regards,

* * *

Fran

---

<div class="post-metadata">

**Author:** ![Casperlet](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.akka.io/casperlet/32/2261_2.png) [@Casperlet](https://discuss.akka.io/u/Casperlet)\
**Post date:** [October 25, 2022, 9:02am UTC](https://discuss.akka.io/t/how-is-entityid-past-to-entity-in-cluster-sharding/10015/7 "2022-10-25T09:02:18Z")

</div>

@franciscolopezsancho It’s a great message, thank you!

The _Akka in Action_ and _Reactive Messaging Patterns with the Actor Model_ written by Vaughn are my favorites about Akka.

I suppose the CQRS and event sourcing are great solutions for micro-services. I wish we can get more advice about them with Akka in your book, maybe the best practice, ah ha.

By the way, the docker is popular and helpful deployment tool. I wish to get more stories about it and Akka too.

See you.
