Akka Event Sourcing – how to create Actor per Entity

akkadesign-patternsdomain-driven-designevent-sourcing

I was reading Akka Persistance documentation and it says that Akka is good for Event Sourcing.

Imagine I have a User entity. Each user is defined uniquely with ID and has some properties and some business rules around how they can be altered. Also, some modifications of User properties can have some async side effects, like notifying another system that a User has been created. Very basic DDD setup.

So from what I understand Akka PersistenceActor is good for keeping the current state of the User, accepting modifications 1 by 1, so nothing can slip inbeetween.

So if I model these users with PersistenceActors where I create an actor per user, but the number of users is way too big to keep them all (or even load them all) into the memory, how do I deal with creating/stopping actors, their persistence/etc..

It seems like quite a complicated task to bear manage. But if I do not do that, what else can PersisteceActors be good for?

Best Answer

You want to look at Akka Cluster Sharding. This complements Akka Persistence to distribute the instances that are currently kept in memory in the cluster and manage their lifecycle.

Also keep in mind that Akka is - by design - a rather low-level toolkit. For an easier getting started experience with event sourcing and cqrs based on Akka, you might want to look at higher-level framework, like Lagom Persistence (Java & Scala) or Fun.CQRS (Scala).

Related Topic