Click or drag to resize
Akka.NETClusterSharding Class

This extension provides sharding functionality of actors in a cluster. The typical use case is when you have many stateful actors that together consume more resources (e.g. memory) than fit on one machine. You need to distribute them across several nodes in the cluster and you want to be able to interact with them using their logical identifier, but without having to care about their physical location in the cluster, which might also change over time. It could for example be actors representing Aggregate Roots in Domain-Driven Design terminology. Here we call these actors "entities". These actors typically have persistent (durable) state, but this feature is not limited to actors with persistent state.

In this context sharding means that actors with an identifier, so called entities, can be automatically distributed across multiple nodes in the cluster. Each entity actor runs only at one place, and messages can be sent to the entity without requiring the sender to know the location of the destination actor. This is achieved by sending the messages via a ShardRegion actor provided by this extension, which knows how to route the message with the entity id to the final destination.

This extension is supposed to be used by first, typically at system startup on each node in the cluster, registering the supported entity types with the Start method and then the ShardRegion actor for a named entity type can be retrieved with ShardRegion(String). Messages to the entities are always sent via the local ShardRegion. Some settings can be configured as described in the `akka.contrib.cluster.sharding` section of the `reference.conf`.

The ShardRegion actor is started on each node in the cluster, or group of nodes tagged with a specific role. The ShardRegion is created with two application specific functions to extract the entity identifier and the shard identifier from incoming messages. A shard is a group of entities that will be managed together. For the first message in a specific shard the ShardRegion request the location of the shard from a central coordinator, the PersistentShardCoordinator. The PersistentShardCoordinator decides which ShardRegion that owns the shard. The ShardRegion receives the decided home of the shard and if that is the ShardRegion instance itself it will create a local child actor representing the entity and direct all messages for that entity to it. If the shard home is another ShardRegion instance messages will be forwarded to that ShardRegion instance instead. While resolving the location of a shard incoming messages for that shard are buffered and later delivered when the shard home is known. Subsequent messages to the resolved shard can be delivered to the target destination immediately without involving the PersistentShardCoordinator.

To make sure that at most one instance of a specific entity actor is running somewhere in the cluster it is important that all nodes have the same view of where the shards are located. Therefore the shard allocation decisions are taken by the central PersistentShardCoordinator, which is running as a cluster singleton, i.e. one instance on the oldest member among all cluster nodes or a group of nodes tagged with a specific role. The oldest member can be determined by IsOlderThan(Member).

The logic that decides where a shard is to be located is defined in a pluggable shard allocation strategy. The default implementation LeastShardAllocationStrategy allocates new shards to the ShardRegion with least number of previously allocated shards. This strategy can be replaced by an application specific implementation.

To be able to use newly added members in the cluster the coordinator facilitates rebalancing of shards, i.e. migrate entities from one node to another. In the rebalance process the coordinator first notifies all ShardRegion actors that a handoff for a shard has started. That means they will start buffering incoming messages for that shard, in the same way as if the shard location is unknown. During the rebalance process the coordinator will not answer any requests for the location of shards that are being rebalanced, i.e. local buffering will continue until the handoff is completed. The ShardRegion responsible for the rebalanced shard will stop all entities in that shard by sending `PoisonPill` to them. When all entities have been terminated the ShardRegion owning the entities will acknowledge the handoff as completed to the coordinator. Thereafter the coordinator will reply to requests for the location of the shard and thereby allocate a new home for the shard and then buffered messages in the ShardRegion actors are delivered to the new location. This means that the state of the entities are not transferred or migrated. If the state of the entities are of importance it should be persistent (durable), e.g. with `Akka.Persistence`, so that it can be recovered at the new location.

The logic that decides which shards to rebalance is defined in a pluggable shard allocation strategy. The default implementation LeastShardAllocationStrategy picks shards for handoff from the ShardRegion with most number of previously allocated shards. They will then be allocated to the ShardRegion with least number of previously allocated shards, i.e. new members in the cluster. There is a configurable threshold of how large the difference must be to begin the rebalancing. This strategy can be replaced by an application specific implementation.

The state of shard locations in the PersistentShardCoordinator is persistent (durable) with `Akka.Persistence` to survive failures. Since it is running in a cluster `Akka.Persistence` must be configured with a distributed journal. When a crashed or unreachable coordinator node has been removed (via down) from the cluster a new PersistentShardCoordinator singleton actor will take over and the state is recovered. During such a failure period shards with known location are still available, while messages for new (unknown) shards are buffered until the new PersistentShardCoordinator becomes available.

As long as a sender uses the same ShardRegion actor to deliver messages to an entity actor the order of the messages is preserved. As long as the buffer limit is not reached messages are delivered on a best effort basis, with at-most once delivery semantics, in the same way as ordinary message sending. Reliable end-to-end messaging, with at-least-once semantics can be added by using AtLeastOnceDeliveryActor in `Akka.Persistence`.

Some additional latency is introduced for messages targeted to new or previously unused shards due to the round-trip to the coordinator. Rebalancing of shards may also add latency. This should be considered when designing the application specific shard resolution, e.g. to avoid too fine grained shards.

The ShardRegion actor can also be started in proxy only mode, i.e. it will not host any entities itself, but knows how to delegate messages to the right location. A ShardRegion starts in proxy only mode if the roles of the node does not include the node role specified in `akka.contrib.cluster.sharding.role` config property or if the specified `EntityProps` is .

If the state of the entities are persistent you may stop entities that are not used to reduce memory consumption. This is done by the application specific implementation of the entity actors for example by defining receive timeout (SetReceiveTimeout(NullableTimeSpan)). If a message is already enqueued to the entity when it stops itself the enqueued message in the mailbox will be dropped. To support graceful passivation without loosing such messages the entity actor can send Passivate to its parent ShardRegion. The specified wrapped message in Passivate will be sent back to the entity, which is then supposed to stop itself. Incoming messages will be buffered by the ShardRegion between reception of Passivate and termination of the entity. Such buffered messages are thereafter delivered to a new incarnation of the entity.

Inheritance Hierarchy
SystemObject
  Akka.Cluster.ShardingClusterSharding

Namespace:  Akka.Cluster.Sharding
Assembly:  Akka.Cluster.Sharding (in Akka.Cluster.Sharding.dll) Version: 1.2.3.42 (1.2.3.42)
Syntax
public class ClusterSharding : IExtension

The ClusterSharding type exposes the following members.

Constructors
  NameDescription
Public methodClusterSharding
TBD
Top
Properties
  NameDescription
Public propertySettings
Gets object representing settings for the current cluster sharding plugin.
Top
Methods
  NameDescription
Public methodStatic memberDefaultConfig
Default HOCON settings for cluster sharding.
Public methodEquals
Determines whether the specified object is equal to the current object.
(Inherited from Object.)
Protected methodFinalize
Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection.
(Inherited from Object.)
Public methodStatic memberGet
TBD
Public methodGetHashCode
Serves as the default hash function.
(Inherited from Object.)
Public methodGetType
Gets the Type of the current instance.
(Inherited from Object.)
Protected methodMemberwiseClone
Creates a shallow copy of the current Object.
(Inherited from Object.)
Public methodShardRegion
Retrieve the actor reference of the ShardRegion actor responsible for the named entity type. The entity type must be registered with the Start method before it can be used here. Messages to the entity is always sent via the ShardRegion.
Public methodStart(String, Props, ClusterShardingSettings, IMessageExtractor)
Register a named entity type by defining the Props of the entity actor and functions to extract entity and shard identifier from messages. The ShardRegion actor for this type can later be retrieved with the ShardRegion(String) method.
Public methodStart(String, Props, ClusterShardingSettings, IdExtractor, ShardResolver)
Register a named entity type by defining the Props of the entity actor and functions to extract entity and shard identifier from messages. The ShardRegion actor for this type can later be retrieved with the ShardRegion(String) method.
Public methodStart(String, Props, ClusterShardingSettings, IMessageExtractor, IShardAllocationStrategy, Object)
Register a named entity type by defining the Props of the entity actor and functions to extract entity and shard identifier from messages. The ShardRegion actor for this type can later be retrieved with the ShardRegion(String) method.
Public methodStart(String, Props, ClusterShardingSettings, IdExtractor, ShardResolver, IShardAllocationStrategy, Object)
Register a named entity type by defining the Props of the entity actor and functions to extract entity and shard identifier from messages. The ShardRegion actor for this type can later be retrieved with the ShardRegion(String) method.
Public methodStartAsync(String, Props, ClusterShardingSettings, IMessageExtractor)
Register a named entity type by defining the Props of the entity actor and functions to extract entity and shard identifier from messages. The ShardRegion actor for this type can later be retrieved with the ShardRegion(String) method.
Public methodStartAsync(String, Props, ClusterShardingSettings, IdExtractor, ShardResolver)
Register a named entity type by defining the Props of the entity actor and functions to extract entity and shard identifier from messages. The ShardRegion actor for this type can later be retrieved with the ShardRegion(String) method.
Public methodStartAsync(String, Props, ClusterShardingSettings, IMessageExtractor, IShardAllocationStrategy, Object)
Register a named entity type by defining the Props of the entity actor and functions to extract entity and shard identifier from messages. The ShardRegion actor for this type can later be retrieved with the ShardRegion(String) method.
Public methodStartAsync(String, Props, ClusterShardingSettings, IdExtractor, ShardResolver, IShardAllocationStrategy, Object)
Register a named entity type by defining the Props of the entity actor and functions to extract entity and shard identifier from messages. The ShardRegion actor for this type can later be retrieved with the ShardRegion(String) method.
Public methodStartProxy(String, String, IMessageExtractor)
Register a named entity type `ShardRegion` on this node that will run in proxy only mode, i.e.it will delegate messages to other `ShardRegion` actors on other nodes, but not host any entity actors itself. The ShardRegion actor for this type can later be retrieved with the ShardRegion(String) method.
Public methodStartProxy(String, String, IdExtractor, ShardResolver)
Register a named entity type `ShardRegion` on this node that will run in proxy only mode, i.e.it will delegate messages to other `ShardRegion` actors on other nodes, but not host any entity actors itself. The ShardRegion actor for this type can later be retrieved with the ShardRegion(String) method.
Public methodStartProxyAsync(String, String, IMessageExtractor)
Register a named entity type `ShardRegion` on this node that will run in proxy only mode, i.e.it will delegate messages to other `ShardRegion` actors on other nodes, but not host any entity actors itself. The ShardRegion actor for this type can later be retrieved with the ShardRegion(String) method.
Public methodStartProxyAsync(String, String, IdExtractor, ShardResolver)
Register a named entity type `ShardRegion` on this node that will run in proxy only mode, i.e.it will delegate messages to other `ShardRegion` actors on other nodes, but not host any entity actors itself. The ShardRegion actor for this type can later be retrieved with the ShardRegion(String) method.
Public methodToString
Returns a string that represents the current object.
(Inherited from Object.)
Top
Extension Methods
See Also