What is the Logic Behind Synchronization in CQRS with Separate Storages?

69 views Asked by At

When using CQRS, we operate with two separate storages. How exactly is the sync operation performed in this context?

Let's say I used an event store database for the Write storage and Elasticsearch for the Read side. I introduce a message broker in between (as the event store database can stream events on its own, as far as I know).

Currently, I have a single event, and to make changes in the Read storage, do I need to write a separate component? Assuming I did, as I mentioned, I now have the latest incoming event. How can I reach the last state using this single event, and how do I perform the sync?

I haven't fully grasped this topic. How and where do we obtain the last state? Does it flow through a stream instead of a single event, and does the component I mentioned find the last state through the stream? Perhaps we use snapshots or other mechanisms for performance?

1

There are 1 answers

0
VoiceOfUnreason On

Typically, between your "write storage" and your "read side" will be some automatic process for copying and transforming the information from one to the other.

Assuming that correct construction of a read model requires events in their proper sequence, your process will fetch sequences of events from the event store.

Using a message broker is not (generally) suitable for automatic processes that require correctly sequenced events.

It can make sense in some cases to use the message broker to trigger the reads of sequences of events from the event store; in common cases a polling loop (bypassing the message broker entirely) is sufficient.

(Recommended viewing: Greg Young, Polyglot Data 2014).