In memory read model

For smaller applications, I’d really like to have my read model in memory. And have the state loaded by replaying the events.
And for even simpler cases I’d like to just query the aggregate.

I’ll probably iterate away from those patterns as my application evolve, but they would be nice to use for proof of concepts and stuff like that.

I have only found examples on how to do JPA read models.

Can you point me to an example of how to build a read model, that is pure memory and replays when the ES aggregate a loaded?

Or how to query the actual aggregate?

Hi Lasse,

Let’s tackle in-memory read models first. If you are building a CQRS application, read models are updated in event handlers as reaction to facts that something happened in the system (events are usually published during command processing). The way this models are persisted is up to the application developer - you can use in-memory databases, relational databases, NoSql databases, files, caches, or even Java data structures.

Querying the aggregates - this is something which does not comply with CQRS, but it can be achieved, as the matter a fact I was working on a project where we didn’t have dedicated read models but were querying aggregates which were stored using JPA repositories. So, that’s one way.
On the other hand if your aggregates are event sourced, you could use EventSourcingRepository to load the aggregate state based on historic events. Of course, this queries can be only by Id and you cannot query based on the aggregate state.

Hope this helps you!


This is interesting, did not see this post before asking my “queryHandler on aggregate” question. Would this be an option to “read” from aggregates?

Ah, forget it, typical “shower” idea … on an aggregate I would only know this one aggregate of course … so it’s not able to answer queries … I withdraw my question.