</> MAANG.io
system design · 201

Intermediate

Master system design interviews with scalable architecture patterns, distributed systems, and real-world design challenges.

0/124 solved 0% complete

CQRS

maang.io System Design Series


CQRS (Command Query Responsibility Segregation) splits a system's write path (commands) from its read path (queries), letting each scale and be modeled independently — writes handle complex domain logic, reads serve denormalized projections. Often paired with event sourcing, it's powerful but introduces an eventual-consistency gap between the two sides. Knowing when it's worth that complexity is the real skill.

This deep dive comes in three parts:

  • Foundations & Core Concepts — separating commands from queries and read models.
  • Internals — projections, pairing with event sourcing, and the consistency gap, with worked examples.
  • Interview — Staff/Principal Q&A: when CQRS helps, when it's overkill, and handling staleness.

CQRS (Command Query Responsibility Segregation) — Part 2: Internals & Event Sourcing

maang.io System Design Series


What is this?

Back to the restaurant. In Part 1 we stood at the door and saw the two machines — the kitchen (commands) and the order board (queries) — with a channel running between them. Now we walk into the back and watch the actual machinery: how a ticket becomes cooked food and then becomes a line on the board, what the "channel" really is, where the kitchen keeps its authoritative record, and every place a real kitchen catches fire on a Friday night.

The single most important upgrade in this chapter is what the kitchen writes down. A naive kitchen stores only the current state ("Table 12: salmon, ready"). A great kitchen keeps the ticket spike — the metal spike where every ticket is impaled in order, never erased: "order placed," "salmon fired," "order ready," "order paid." That append-only spike is Event Sourcing, and once the kitchen has it, every board on the wall becomes a disposable replay of the spike. This is the pairing that makes CQRS truly powerful, so we'll build up to it deliberately.

The one-line idea: The write side turns a command into one or more events (facts about what happened) and durably records them; a projector consumes those events and folds them into each read model. Get the recording atomic, the projectors idempotent, and the ordering per-aggregate, and CQRS is rock-solid. Miss any of the three and you get lost writes, double counts, or corrupt views.


Sign in to continue reading

The rest of this lesson is available with a free account. Signing in with Google or Microsoft is free.

Sign in to read the full lesson

Sign in to MAANG.io

Use your Google or Microsoft account — no password to remember.

Continue with Google Continue with Microsoft

Please accept the terms above to continue.