Backend / CQRS

CQRS

A shop’s order system is split in two: one side accepts changes, the other answers questions. We follow one order across the gap, including the moment the two sides disagree.

Advanced7 steps~7 min
Command — asks for a change
Query — asks for data, changes nothing
Write model — enforces the rules, saves changes
Read model — serves data shaped for screens
// THE SHORT VERSION

In 30 seconds

CQRS means handling changes and questions separately. Operations that change data, called commands, go to a write model that enforces the rules. Operations that fetch data, called queries, go to a read model shaped for the screens.

Often each side also gets its own store. The write side announces every change as an event, and the read store is updated from those events a moment later, so reads can briefly be out of date.

That delay and the extra moving parts are the price. It is worth paying only where reads and writes have very different needs.

CQRS, step by step

STEPS01 / 07

CQRS is often combined with event sourcing but does not require it. The 200 ms lag is an invented figure; real lag depends on how the events are delivered.

STEP 01 / 07 One model does everything
THE MATH
write: check rules, save read: join 4 tables
  1. Step 1: One model does everything. A shop stores orders in four tidy tables and uses one model for all of it. Placing an order has to enforce rules, such as enough stock. Showing “my orders” has to join all four tables and add up totals on every page view. The same code and the same tables are pulled in two directions.
  2. Step 2: Commands and queries. Sort every operation into one of two kinds. A command asks the system to change something and reports only whether it was accepted. A query asks for information and changes nothing. No operation does both. CQRS, Command Query Responsibility Segregation, starts from this split.
  3. Step 3: A model for each side. Give each kind its own code. The write model knows the business rules and decides whether a command may happen. The read model knows nothing about rules; it only fetches data in the shape a screen needs. This is already CQRS, even while both sides still share one database.
  4. Step 4: A store for each side. The next step, which is optional, gives each model its own store. The write store keeps every fact once, so the rules are easy to enforce. The read store keeps answers ready-made: one row per order with the name, item count and total already worked out. Ana’s order 42 is 2 teas at €6 and a €45 teapot: 3 items, €57.
  5. Step 5: Keeping the two in sync. Now the read store must hear about every change. After saving order 42, the write side announces an event, OrderPlaced. A small piece of code called a projector receives it and updates the matching rows in the read store. The read store is never edited directly; it is rebuilt from what the write side reports.
  6. Step 6: Eventual consistency. The projector needs a moment, here 200 ms. In that gap the two stores disagree: the write store already holds Ana’s third order while the read store still lists two. A query during the gap gets an old answer. The stores do agree in the end, which is called eventual consistency, and screens must be designed for the delay.
  7. Step 7: When it pays off. CQRS always costs something: two models, sync code that can fail, and screens that must tolerate stale data. It earns that back when reads far outnumber writes, when the write rules are complex, or when screens need the same data in many shapes. Use it for the part of a system that needs it, not for a simple form over a table.
VALUES SO FAR
join4 tablesper read
command····write
query····read
models····code
stores····data
read row····ready
event····sync
lag····stale
worth it····when
// BEFORE

Learn this first

Nothing. This concept starts from scratch.

// AFTER

Learn next

Nothing builds on this one yet. See every backend concept.

Spotted a mistake on this page?