Search code examples
akkaactoractor-model

Can we have global state in actor-based systems?


One of the biggest advantages of the actor model is the removal of locking (actors operate independently and serially). Does this mean that we cannot have any shared/global state at all in an actor system (because accessing/updating that will bring in locks)?

In more practical terms, what happens to updates to an entity (e.g. DB) from multiple actors in such a system?


Solution

  • Actor model intended to solve issue with any mutable shared state in another way - actor should encapsulate it. So if you need something to be shared between actors - this should be an actor with this state and protocol to work with it. If you would like to update DB from different actors - extract an actor responsible for this, and provide API or protocol for other actors to update DB. Or make several actors to handle DB updates and route messages between them (Please, see for more details: https://doc.akka.io/docs/akka/current/typed/routers.html)

    General approach - think about shared state, as actor shared between actors (via ActorRef) and state API as messages for this actor.