Search code examples
akkaakka-persistence

Which actors should be made Persistent?


Let's imagine a typical Akka application with a tree like structure of Actors i.e. a small (2-3) number of top level actors which manage potentially thousands of small actors at the bottom of the hierarchy. It seems there are two approaches to persistence:

  1. Make only the top level actors persistent. The child actors rely on their parents for any state recovery etc.
  2. Make all of the children actors persistent. We end up with potentially thousands of persistence ids and each actor can recover it's own state.

What considerations are there when choosing between these approaches? Are there any typical examples of use cases that fit well with one but not the other?

The question is supposed to be abstract but here's an example. Say we have an actor for every user of the application, and these actors share the same parent actor. If we want to persist each user's email address and any updates to it, we could make every user actor persistent or we could just make the top level actor persistent.


Solution

  • In that case, I would probably go with second approach. You would only recover events for user actor when it's "selected". What makes this approach even more convenient is the fact that behavior of user actor can change based on its state. E.g. if user registers, you certainly wouldn't like to persist that same "registered" event for multiple times. Or if user deactivates its account, you certainly wouldn't like to let him update his email etc. Plus, all changes are saved as a sequence of events. So no data loss.

    On the other hand, if you use first approach, you would have to recreate all child actors upon parent actor recovery in order to set their state so that you could avoid previously mentioned undesired behaviors.