Akka Cluster Sharding - How Akka Automates Cross-System Actor Distribution

Cluster Sharding is Akka’s product that abstracts the use of actors in a cluster. For more information about actors, see our article on the Actor Model and Akka Actors. A cluster refers to computer systems that operate as a network to collectively handle complex tasks. The location of an individual computer system, also called a node in the cluster, is irrelevant. The advantage of this concept lies in its flexibility and scalability. A cluster can be scaled as needed by adding or removing nodes.
!The diagram shows an example architecture of Cluster Sharding. Starting from the nodes of a cluster, Akka’s Cluster Sharding introduces the components ShardRegion, ShardCoordinator, Shard, and Entity. Each of these components is implemented as an actor in the background.
ShardRegion
ShardRegion actors are started for each node in the cluster. This actor extracts the Shard and Entity actor IDs from a message to be processed and forwards the message to the appropriate location, provided the ShardRegion actor already knows the location of the Shard actor in the cluster. If the location is still unknown, the ShardRegion actor determines it by querying the ShardCoordinator actor.
ShardCoordinator
The ShardCoordinator actor serves as the central management of Cluster Sharding, which is why it operates as a singleton in the cluster. This actor determines which Shard actor belongs to which ShardRegion actor, making it the point of contact for location queries. The location of individual Shard actors is persistently stored by the ShardCoordinator actor in the form of distributed data in the cluster to protect against failures. As part of Shard actor management, the ShardCoordinator actor also handles the redistribution of Shard actors when changes occur in the cluster. Such changes can occur, for example, due to a node failure or a cluster scaling operation.
Shard
The Shard actor is created by the ShardRegion actor and manages the Entity actors.
Entity
An Entity actor is created by the Shard actor. This actor represents the program logic defined using Akka Actors for processing messages.



