A shard region is the message-routing boundary for sharded entities on a cluster node. It contains shards, and each shard groups entities according to the configured sharding function. Applications send entity messages through a shard region rather than needing to locate or spawn the target actor themselves. section 9.1.1 section 9.1.2
Use this diagram to distinguish the containment hierarchy: nodes host shard regions, shard regions contain shards, and shards contain distributed entities. section 9.1.1
A shard region makes several distributed behaviors practical:
- Routes a local message to the entity’s hosting region when that entity lives on another node. section 9.1.1
- Consults the shard coordinator when it does not know an entity location; the coordinator then shares the discovered region information with all shard regions. section 9.1.1
- Lets an entity be addressed by an EntityTypeKey and string ID, with different entity types allowed to reuse the same ID. section 9.1.2
- Causes a missing entity to be created when a message is sent to it, while exposing an EntityRef-style API rather than a direct ActorRef. section 9.1.2
Route cargo commands to a container
Initialize ClusterSharding with a Container entity definition, deriving the behavior’s containerId from EntityContext.entityId. The returned shard-region ActorRef accepts ShardingEnvelope values: wrapping "id-1" with AddCargo routes the command and creates that container if needed. EntityContext supplies the entity type key and ID, not ActorContext features such as watching, logging, spawning, or scheduling. section 9.1.2
Expose a sharded entity through gRPC
A market service initializes a shard region, obtains an EntityRef with entityRefFor using the market ID, and calls entityRef.ask to turn a GetState request into a response. This is appropriate outside an actor context; actor-internal requests instead use context.ask to preserve actor-state consistency. section 12.2.1
A shard region also distinguishes Akka Cluster Sharding from ShardedDaemonProcess. Both distribute behaviors, but the region acts as a proxy for direct actor messaging. ShardedDaemonProcess has a fixed instance count at creation and no equivalent direct-message proxy. section 10.3.3
