Search code examples
domain-driven-designcqrsread-write

What persistence problems are solved with CQRS?


I've read a few posts relating to this, but i still can't quite grasp how it all works.

Let's say for example i was building a site like Stack Overflow, with two pages => one listing all the questions, another where you ask/edit a question. A simple, CRUD-based web application.

If i used CQRS, i would have a seperate system for the read/writes, seperate DB's, etc..great.

Now, my issue comes to how to update the read state (which is, after all in a DB of it's own).

Flow i assume is something like this:

WebApp => User submits question WebApp => System raises 'Write' event WriteSystem => 'Write' event is picked up and saves to 'WriteDb' WriteSystem => 'UpdateState' event raised ReadSystem => 'UpdateState' event is picked up ReadSystem => System updates it's own state ('ReadDb') WebApp => Index page reads data from 'Read' system

Assuming this is correct, how is this significantly different to a CRUD system read/writing from same DB? Putting aside CQRS advantages like seperate read/write system scaling, rebuilding state, seperation of domain boundaries etc, what problems are solved from a persistence standpoint? Lock contention avoided?

I could achieve a similar advantage by either using queues to achieve single-threaded saves in a multi-threaded web app, or simply replicate data between a read/write DB, could i not?

Basically, I'm just trying to understand if i was building a CRUD-based web application, why i would care about CQRS, from a pragmatic standpoint.

Thanks!


Solution

  • Assuming this is correct, how is this significantly different to a CRUD system read/writing from same DB? Putting aside CQRS advantages like seperate read/write system scaling, rebuilding state, seperation of domain boundaries etc, what problems are solved from a persistence standpoint? Lock contention avoided?

    The problem here is:

    "Putting aside CQRS advantages …"

    If you take away its advantages, it's a little bit difficult to argue what problems it solves ;-)

    The key in understanding CQRS is that you separate reading data from writing data. This way you can optimize the databases as needed: Your write database is highly normalized, and hence you can easily ensure consistency. Your read database in contrast is denormalized, which makes your reads extremely simple and fast: They all become SELECT * FROM … effectively.

    Under the assumption that a website as StackOverflow is way more read from than written to, this makes a lot of sense, as it allows you to optimize the system for fast responses and a great user experience, without sacrificing consistency at the same time.

    Additionally, if combined with event-sourcing, this approach has other benefits, but for CQRS alone, that's it.

    Shameless plug: My team and I have created a comprehensive introduction to CQRS, DDD and event-sourcing, maybe this helps to improve understanding as well. See this website for details.