Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Event-sourcing with akka-persistance: growing state as list?

I am designing a backend using CQRS + Event sourcing, using Akka + Scala. I am not sure about how to handle a growing state. For instance, I will have a growing list of users. To my understanding, each user will be created following a UserCreated event, such events will be replayed by the PersistentActor, and the users will be stored in a collection. Something like:

class UsersActor extends PersistentActor {

    override def persistenceId = ....

    private case class UsersState(users: List[User])

    private var state = UsersState()

    ....
}

Obviously such state would eventually grow too big to be held in memory by this actor, so I guess I'm doing something wrong.

I found this example project: the idea seems that each user should be held by a different actor, and loaded (from the event history) as needed.

What is the right way to do this? Thanks a lot.

like image 668
ticofab Avatar asked Oct 30 '15 08:10

ticofab


1 Answers

The answer is: each aggregate/entity (in my example, each User) gets its own actor, which embeds the state for that particular entity and that one only.

like image 94
ticofab Avatar answered Sep 19 '22 06:09

ticofab