CQRS - Event replay for read side

2019-07-09 04:57发布

问题:

I have read several blogs on CQRS and all of them explain that at write-side events are persisted on event store and upon a request, events are retrieved and replayed on aggregate.

My question is why doesn't event replay on an aggregate is required at read side?

回答1:

Because your read side doesn't use aggregates.

Read side is implemented as projections which calculate the current state from the stream of events emited by aggregates and persist the current state in some pesistent store or in memory. The while point on the read side is to have a current state readily available for clients.



回答2:

I wanna add example to Jakub Konecki explanation.

Let's imagine that you model a bank account using event sourcing. Every operation on that account causes event(s) to be persisted. After few years you have hundreds of events that are connected with that bank account. Now, if you want to display balance of that account you would replay all events to calculate the balance? And if there are many accounts, replaying events only to calculate balance would be performance bottleneck for application. We don't even mention other information that are needed to display from bank account and describe current state of account.

That is why we store snapshots of aggregate state on read side, because mainly read side is used for presentation purposes. And we want to keep that part of our system simple.