A sharding entity is an actor managed by Akka Cluster Sharding. It is identified by an entity type key and a string ID; that pair is unique across the cluster, so at most one active instance represents it at a time. Sharding distributes entities across nodes to spread memory use and relocates them when membership changes or a node fails. section 9.1 section 9.1.2
Entities sit inside shards, while shard regions on cluster nodes route messages toward them. The shard coordinator tracks shard placement and helps regions find the destination before they cache routing information. section 9.1.1
An application addresses an entity through a shard region rather than relying on its underlying ActorRef. Send a message in a sharding envelope with the entity ID; if no instance exists, sharding creates one automatically. An EntityRef offers a similar messaging API, but it is not an ActorRef. section 9.1.1 section 9.1.2
Shipping container entity
A freight container can be modeled as one entity per container ID. Its behavior starts with no cargo, accepts AddCargo commands, and replies to GetCargos. Initialize ClusterSharding with the container type key and a behavior factory that receives the entity ID, then send ShardingEnvelope("id-1", AddCargo(cargo)) to the shard region. Different entity types may reuse the same ID without collision. section 9.1.2 section 9.1.3
Treat entity lifecycle as sharding infrastructure rather than ordinary actor ownership:
- Let the sharding module create, maintain, rebalance, and relocate entities; direct ActorRef interaction can bypass expected lifecycle behavior. section 9.1.3
- Route normal traffic through a shard region. Direct messages to the underlying ActorRef do not count as activity for passivation. section 9.1.3 section 9.1.5
- Expect a relocated entity to restart without its in-memory state. Use Akka Persistence when that state must survive movement or failure. section 9.1
- Passivate idle entities when their stored objects make memory costly. New routed messages are buffered until a passivated entity becomes active, subject to a buffer limit. section 9.1.5
