How to find cluster sharded entity Actors by entity id

Hi ,

We have a requirement where a kafka message from external system is suppose to update one unique Sharded entity. The message has information on the unique entity id key.
what is the recommended pattern to find a sharded entity actor by entity id ? I understand Receptionist can always give a list of actorRef, but how to find a particular entity with actorRef. Request<–>Response on the whole list will be too heavy.

Hi @Dexter,

you have two options (I’m assuming you are using the typed API):

  1. when initializing the sharding you can define an extractor function so given a message entering the shard region on the cluster, the function can extract the entity ID and the shard region can forward the message to the appropriate entity
  2. you can eagerly locate once and keep a reference to the EntityRef

See more details in the docs:

Messages to a specific entity are then sent via an EntityRef . It is also possible to wrap methods in a ShardingEnvelope or define extractor functions and send messages directly to the shard region.


Thanks for the reply, I have one follow up question: I also need to check if this actor is already created, is there a way to check if the actor exists before sending the message using shard?

When you send a message to the EntityRef, If the entity actor doesn’t exist, the actor system will create one and the create method will be called on that actor