Skip to main content
Architecturechapter 3 of 5 · 4 lessons

CQRS & Event Sourcing

Separate read and write models; store state as an ordered log of events instead of current values.

Your seller dashboard shows order counts, revenue this month, and top products. Building it takes a nine-table join that runs for 4 seconds, and sellers open it constantly.

The tables are not wrong. They are shaped for taking orders safely, one at a time, with the rules enforced. That is the opposite of what a dashboard wants.

Keep a second copy shaped like the dashboard, updated as orders happen, and the page becomes a single row lookup. That is one idea.

The other, larger idea is to stop storing where things stand and store what happened instead, as a list you only ever add to. These two get sold as a package and they are separate, and one of them roughly doubles the moving parts in your system.

Lessons

4 in this chapter
  1. Splitting Reads from WritesCommands change state through one model. Queries read from another, shaped for the screen.2 min
  2. The Event Log as the Source of TruthStore what happened, not what is. Current state becomes a computed result.2 min
  3. Projections and ReplayEvery read view is a fold over the log, which means every read view is rebuildable.3 min
  4. When This Is OverkillMost CRUD apps need none of this. Know the signals that say otherwise.2 min