Multiple sharding in a Cluster

Hi,

By default cluster sharding will distribute actors across the nodes in a cluster.

But I want to distribute actors to only few nodes among all in the cluster.

Here I created two shard regions according to incoming ports and I ran this codebase, it created actor within the specific nodes in a cluster.

if(port.equals(“2553”)||port.equals(“2554”)){
ActorRef shardingRegion = setupClusterSharding(actorSystem,“region1”);
actorSystem.actorOf(EntityCommandActor.props(shardingRegion), “entityCommand”);
actorSystem.actorOf(EntityQueryActor.props(shardingRegion), “entityQuery”);
}
else {
ActorRef shardingRegion = setupClusterSharding(actorSystem,“region2”);
actorSystem.actorOf(EntityCommandActor.props(shardingRegion), “entityCommand”);
actorSystem.actorOf(EntityQueryActor.props(shardingRegion), “entityQuery”);
}

private static ActorRef setupClusterSharding(ActorSystem actorSystem, String entityName) {
ClusterShardingSettings settings = ClusterShardingSettings.create(actorSystem);
return ClusterSharding.get(actorSystem).start(
entityName,
EntityActor.props(),
settings,
EntityMessage.messageExtractor()
);
}

Is it correct way of implementation ?

Please advice me.

Thanks

I’d recommend looking into Akka Cluster Roles:

https://doc.akka.io/docs/akka/current/cluster-usage.html#node-roles

The documentation on Cluster Sharding covers how to run entities only on nodes with a specific role:

https://doc.akka.io/docs/akka/current/cluster-sharding.html

You still need to start sharding on the other cluster nodes that will communicate with the entities. These will automatically start in proxy-only mode when the roles don’t match.

Thanks @TimMoore