Rehydrating huge persitent entities

As per Lagom documentation, Persistent Entity is rehydrated upon demand with all it’s state built from events.

Now let’s assume, we have a persistent entity that maintains a list of orders placed by customer: PersistentEntity_State(id: Int, orders: List[Order]) and with every new OrderPlaced event we add to this list of orders.

Evidently, this persistent entity could grow really huge in time.

My questions are:

  1. Wouldn’t it make rehydrating such huge entities slow in time?
  2. Isn’t there are possibilty that we may stumble upon OOM exceptions while rehydrating such entities?

TIA.

Hi @codingkapoor,

  1. Wouldn’t it make rehydrating such huge entities slow in time?

Yes, you can mitigate it with snapshots. By default, Lagom saves snapshots every 100 events, but if your state is huge it will also take time to load it from the database

  1. Isn’t there are possibilty that we may stumble upon OOM exceptions while rehydrating such entities?

Yes, of course. If you have lots of data and want to load it in memory, you need to have sufficient memory available.

But I would challenge the idea of saving all orders on an entity. Lagom PersistentEntity is designed to model CQRS Aggregates. So which consistency constraint are you enforcing with a list of Orders?

It sounds more like you are using the PersistentEntity as a read-model instead of a write-model. Instead of having an entity, you should have a read-side processor that consumes the stream of places orders and generated that huge list.

You can still have OOM issues, if you generate a read-model that’s huge, but then it’s much easier to optimize on the read-model because it gives you the flexibility of CRUD.

Kind regards,

Renato

1 Like