Lucas Batistussi
Lucas Batistussi

Reputation: 2343

Initialize an entity on startup on Akka Sharding

How could I prestart an entity at cluster startup? I have found a way to do so but I think it is not the right way to do. It consists of sending a StartEntity(entityId) message to the shard region on every node. Suppose I have 1000 entities to initialize. It seems very unperformant (explosion of messages in the cluster since every node tries to initialize the remote entity)!

    val shardRegion: ActorRef[ShardingEnvelope[Command]] =
    sharding.init(Entity(HelloServiceEntity)(createBehavior = ctx => HelloWorldService()))

    Seq("S0", "S1").foreach { id =>
      shardRegion ! StartEntity(id)
    }

Is there any efficient way to achieve what I want? I could not find an official post or documentation about it. Am I doing this wrong?

Upvotes: 0

Views: 151

Answers (1)

Lucas Batistussi
Lucas Batistussi

Reputation: 2343

I had an idea ! I could use a cluster Singleton whose job would be to initialize entities. That’s the most efficient way I came up with without getting into internals and having to propose a pull request :joy:

Upvotes: 1

Related Questions