Reputation: 7717
Imagine you have a service that returns restaurant ratings in four European capitals:
Using Akka, imagine you assign each city to an Actor, and then divide these actors across two nodes (think Akka Cluster). If we naively shard our actors in alphabetical order, the resulting division is:
Obviously the traffic will be very unbalanced. If you don't know the population of the cities beforehand, does Akka provide a tool to dynamically reconfigure how you shard these cities?
I appreciate any pointer or reference. Thanks for your help!
Upvotes: 0
Views: 472
Reputation: 631
As you mentioned, Cluster Sharding allows you to easily distribute your actors across the cluster. By default, a new shard is created on the region (node) with the least amount of allocated shards (LeastShardAllocationStrategy
), but you can easily create your own custom ShardAllocationStrategy
with different rules.
To do so, extend ShardAllocationStrategy
and implement those 2 methods:
def allocateShard(requester: ActorRef, shardId: ShardId,
currentShardAllocations: Map[ActorRef, immutable.IndexedSeq[ShardId]])
: Future[ActorRef]
def rebalance(currentShardAllocations: Map[ActorRef,
immutable.IndexedSeq[ShardId]], rebalanceInProgress: Set[ShardId])
: Future[Set[ShardId]]
The first one determines which region will be chosen when allocating a new shard, and provides you the shards already allocated. The second one is called regularly and lets you control which shards to rebalance to another region (for example, if they became too unbalanced).
Both functions return a Future
, which means that you can even query another actor to get the information you need (in your case, for example, get the "weight" of each city to choose the region with the smallest weight).
Upvotes: 1