How it works · 19 of 49 Advanced
What is CQRS (command query responsibility segregation)?
One model for writing, another for reading
CQRS: CQRS (command query responsibility segregation) splits a data model in two: the write side takes commands and checks the business rules; the read side holds copies shaped for each screen, kept up to date from the write side's changes. Reads stop queueing behind writes and each side scales and changes on its own. The price is more parts and a lag: the read side is a moment behind, so a reader can see the old value just after a change. Use it where reads far outnumber writes or the two shapes really differ, not across a whole system.
Loading the animation…
Say it like this CQRS gives writes and reads separate models: commands go to a model that checks the rules, queries go to copies shaped for reading, so reads stop waiting on writes, at the price of more parts and a read side that lags a moment behind.
Under the hood — for builders
- The sources, in their words
- Microsoft ("CQRS pattern"): "Segregate the read and write operations for a data store into separate data models"; "the read data might not show the most recent changes immediately." Martin Fowler ("CQRS", 2011): "beware that for most systems CQRS adds risky complexity."
- Two models, side by side
- Command: a request to change something, named for the business task. Query: a request that only reads. Projection: the code that turns each change into the read model's shape.
- Simulated here: the queue
- One clerk; questions arrive 0.8 a second and take 1 s; changes arrive once a minute and take 6 s (assumptions). Wait before being served, seconds:
- Run here: the lag
- SQLite ledger; a projector reads new rows every 0.8 s and updates a board table. Time from a change in the ledger to the board:
- Read your own write
- After a user changes something, read that one item from the write side, or show the change locally, so they never see the board's old value for their own action.
- Measure it
- Read-to-write ratio · read latency with and without the split · projection lag, median and worst · reads served stale after a write.
- Watch out
- Microsoft: not suitable when "The domain or the business rules are simple." Fowler: "CQRS should only be used on specific portions of a system." Publishing the change reliably is D5.
- Sources
- Microsoft, "CQRS pattern", Azure Architecture Center, 2025 · Martin Fowler, "CQRS", 2011 · both read in session · simulation and SQLite run, Python, 19 Nov 2026.
What happens in the CQRS animation, step by step?
CQRS split off or on: Split off
- One model for everything: one window records every change in the ledger and answers every question from it.
- Questions wait behind changes, and behind each other. The queue reaches the door.
- Now switch the split on, above the picture.
CQRS split off or on: Split on
- Split on: the clerk only records changes. Each change is copied to a board built for reading.
- Travellers read the board. The queue at the window is gone.
- The board is a moment behind: 0.37 s here. Fine for a gate number, not for a balance you just paid in.
- Two models, two shapes, at the price of more parts and a lag. Use it where reads swamp writes. Next: keep every change, not just the latest.
What everyday example explains CQRS?
The back-office ledger vs the departures board. One clerk records every change; the hall reads the board, a moment behind.
Why should a team care about CQRS?
A slow product page or dashboard is often reads fighting writes over one model. CQRS fixes that, and costs a second model, a feed between them and a visible lag; decide per screen whether that lag is acceptable.