Event Sourcing & CQRS
Before this, it helps to know: Kafka Intro (Beginner).
Read a little, play a little. No scary maths, and no rush.
Scope: this is an overview, not a deep dive. It introduces both patterns and when each is worth the cost. Schema evolution, snapshotting and projection rebuilds are out of scope. For the canonical write-ups see Microsoft's CQRS pattern and Martin Fowler on Event Sourcing.
The receipt, not the balance
Most databases store the balance: 400, then 350 after a withdrawal. Convenient,
and it throws away everything. An event-sourced system stores the receipts:
AccountOpened 400, MoneyWithdrawn 50 — and computes 350 by folding them.
That single change buys you a complete audit trail, the ability to ask "how did this balance get here?", and the ability to build a new view of history without touching the source. It also costs you schema evolution forever, joins are harder, and every read becomes a replay unless you snapshot.
Event sourcing: the log is the database
writes: AccountOpened(400) → MoneyWithdrawn(50) → MoneyDeposited(20)
state: fold(events) = 370
The rules that follow from this are the ones that bite:
- Events are immutable. You never edit or delete one; that is what makes the history trustworthy.
- Events are facts in the past tense.
MoneyWithdrawn, notWithdrawMoney. An event that describes a wish rather than a fact cannot be replayed honestly. - Old events never change, so you upcast. When the payload needs a new field, you write a transformer that understands both shapes and apply it on read. This is the single biggest source of pain in the pattern and the reason it is usually a deliberate choice rather than a default.
- Snapshots are a cache, not a source of truth. They make replay fast; you must be able to rebuild them from the log.
CQRS: two models, not one
Event sourcing is about how state is stored. CQRS is about how you read and write it, and it is useful on its own.
The problem it solves: the shape that makes writes safe is rarely the shape a screen wants. A write model enforces invariants — an order cannot be empty, a refund cannot exceed the charge. A read model wants a denormalized, pre-joined row. Forcing one shape to do both jobs produces either unreadable queries or unenforceable writes.
command → write model (validates invariants) → event
↓ projection
read model (screen-shaped) → query
The cost is honest and worth naming: the read model lags the write model. After a write, a user can briefly see the old value. For a product page, invisible. For a payment confirmation or a seat booking, not acceptable — those paths read the write model directly or wait for the projection to catch up.
When not to do this
Both patterns add real machinery. Reach for event sourcing when you genuinely need the history — audit requirements, or "what happened to this order?" questions that the present state cannot answer. Reach for CQRS when read and write shapes have genuinely diverged. If neither applies, a single normalized schema with a good index will serve you better and cost a fraction of the complexity.
Remember this
- Event sourcing stores the history; current state is a fold over it.
- Events are immutable, so shape changes need upcasters on read.
- CQRS splits write and read models, and buys a read model that can lag.
- Neither is a default. Use them for audit trails and shape divergence, not by habit.
Check your understanding
5 questions · first-try answers earn the most XP
My notes
Saved in this browser. Highlight a line above and save it, or write it in your own words.
Nothing saved yet. Your highlights will live here.
References
Finished reading?
Ticking it here also ticks the chapter in the sidebar, the section count and your streak — it is all one number.
Practice this in an incident
Try a design drill
Related chapters
Spotted a mistake or want a topic covered? Report an issue