Event sourcing and CQRS
Two ideas that arrive together, get conflated constantly, and are worth separating — because one of them is a modest technique you are probably already using, and the other is a commitment that reshapes an entire codebase.
CQRS separates the model you write through from the model you read through. Event sourcing makes the log of what happened the source of truth, with current state derived from it. You can do either without the other. Most teams should do the first and think very hard about the second.
CQRS, the smaller and more useful idea
The shape that serves a write and the shape that serves a read are not the same, and 5.01 already told you why: the write model exists to enforce invariants, so it is normalised, narrow, and organised around one aggregate at a time. The read model exists to fill a screen, so it wants everything pre-joined, denormalised, and organised around a query.
You have been doing this since Tier 4 without the label. A cache is a read model that forgets. A search index is a read model with different indexing. A materialised view is a read model the database maintains for you. CQRS is the step of making it explicit — the read side gets its own store, its own shape, and its own consistency guarantee:
command ──▶ write model ──▶ events ──┬──▶ read model A (the list screen)
(invariants) ├──▶ read model B (search index)
└──▶ read model C (the warehouse — 7.01)
Two consequences fall out immediately. The good one: a new screen with a hostile query pattern is a new projection, not a schema negotiation with the write side. The read models can even live in different kinds of database, each chosen for its query — Tier 3's decision tree, now applied per-view instead of once for the whole system.
The bad one, and it is not small: the lag becomes visible to users. A user saves a thing and it is not in the list yet. Tier 4 gave this a name — read-your-own-writes — and there it was a database feature you could switch on. Here it is your problem, and there are three honest answers: read that one screen from the write model, have the client hold a version token and wait for the read model to reach it, or update the UI optimistically and reconcile. Pick deliberately, because the default is a bug report you will receive weekly.
Event sourcing: store the facts, derive the state
The ordinary approach stores the current state and mutates it. A balance goes from 500 to 300, and the 500 is gone. Event sourcing stores what happened:
AccountOpened(id, ₹0) Deposited(₹500) current state = fold(events) Withdrew(₹200) = ₹300
State is a function of the log, not a thing you keep. Four genuine gains follow, and they are the reason the pattern exists:
- The audit trail is free and complete. Not a log written alongside the truth and hoping to agree with it — the log is the truth. In regulated domains this alone can justify the whole approach.
- Temporal questions become answerable. "What was the balance on 3 March?" is a fold over a prefix. In the ordinary model that question is unanswerable, and you discover this the week someone asks it.
- You can build read models over history you did not anticipate. Product wants a view that needs data you never stored — but you stored the events, so the projection can be built over the past, not just the future. This is the capability people underrate most.
- Debugging becomes replay. "How did it get into this state" has an exact answer, in order, with timestamps.
The read-model rebuild
This is the payoff worth understanding properly, because it changes what a schema change is.
A read model is derived, so it is disposable. Need a new column, a fixed projection bug, a different grouping? Do not migrate it — delete it and rebuild from event one. No ALTER TABLE against a live table, no backfill script, no expand–migrate–contract. Build the new projection alongside the old one, let it catch up, then switch reads across: Tier 14's blue-green deploy, applied to data.
Now the operational reality, because "just rebuild it" hides three things:
- It takes as long as it takes. Ten years of events is hours of replay. Plan the switchover, do not discover the duration during an incident.
- Snapshots bound the cost. Periodically persist the folded state so replay starts from the snapshot rather than from event one. Necessary for aggregates with long histories — and a snapshot is a cache, so it can be wrong and must be rebuildable.
- The rebuild path decays unless it is used. A rebuild you have not run in a year will not work when you need it. Run it in CI over a sample; rebuild something in production on a schedule. An untested recovery path is not a recovery path.
The costs, stated plainly
- Events are permanent, and so are their meanings. You cannot deprecate history. When an event's shape must change you keep the old one forever and write an upcaster that translates v1 into v2 on read. Tier 1's schema evolution, with the deprecation option removed.
- Deletion collides with immutability. An append-only log and a legal right to erasure are in direct conflict. The working answer is crypto-shredding: encrypt each subject's personal data with a per-subject key, store only ciphertext in the log, and delete the key. The events remain; their contents become permanently unreadable. Decide this at design time, because retrofitting it means rewriting a log you promised never to rewrite.
- You cannot query a log. "How many accounts are overdrawn" has no answer in an event store. Every question needs a projection, which means CQRS is not optional here — it is the only way to read anything.
- It reshapes the whole team's model. Every new engineer must learn it, every debugging session goes through it, and half-adoption is worse than either extreme.
When it earns its keep: domains where the history is the product — ledgers, trading, insurance claims, medical records, anything where "why is it in this state" is a question people ask weekly and auditors ask annually. Lab 13.07's payment ledger and the Splitwise dojo are both this shape; a double-entry ledger is event sourcing that predates the term by five centuries.
When it does not: CRUD with a nice interface. If nobody has ever asked what a record looked like last month, you are paying a large tax for an answer nobody wants.
Adopting event sourcing for the whole system rather than for the one aggregate that needs it. It is an aggregate-level decision — the ledger can be event-sourced while user profiles, preferences and the product catalogue stay ordinary tables that you update in place. Teams that apply it uniformly spend their complexity budget on a settings screen, then conclude the pattern was a mistake. The pattern was fine; the blast radius was chosen wrong.
The tradeoff sheet
- Problem solved
- Domains where the history is the product and an overwritten row destroys the answer to "how did it get into this state".
- Rejected, and why
- An audit table alongside normal mutable rows captures most of the value at a fraction of the cost. Temporal tables give you time travel with none of the rebuild machinery.
- The trade
- Queries become projections that must be built and maintained, the read model is eventually consistent with the write model, and every schema change is a replay concern.
- First to break at 10x
- Replay time. At 10x the event count, rebuilding a projection from scratch stops fitting in a maintenance window, and snapshots become mandatory rather than an optimisation.
- What it does to the bill
- Storage grows monotonically by design and is rarely the problem. The cost is the projection infrastructure and the engineers who understand it.
- Day-2 operations
- You need projection lag per read model and a rebuild procedure that has been run recently. A projection nobody can rebuild is a database with no backup.