Topic 120 / Advanced
Event sourcing
Chapter 7.4 · Data Consistency Across Systems
What and why
A normal application stores only current state, overwriting history on every update — but some domains (financial ledgers, audit-heavy systems) need every change that ever happened, not just the latest snapshot. Event sourcing makes the append-only sequence of events the source of truth; current state is never stored directly but derived by replaying the events for an entity from the beginning, or from a snapshot forward. Builds on: → 113 (event-driven architecture — event sourcing takes this further by making events, not a side effect, the only record).
Diagram
Traditional: row [id, status, balance] ← overwritten on each update
Event-sourced: Opened(balance=0)
Deposited(100)
Withdrawn(30)
Deposited(50)
───────────────────────
current state = replay all events = balance 120
How it works
- Every state change is captured as an immutable event, appended to a per-entity event log — never updated or deleted, only appended to.
- Current state is derived by replaying an entity's events in order from the start (a fold over the sequence) — there is no "current row" stored as the primary record.
- For long-lived entities, periodic snapshots of derived state avoid replaying the entire history on every read; a snapshot plus events after it reconstructs current state.
- Reads that need current state efficiently are typically served from a separately maintained read model built by consuming the event stream — pairing event sourcing with CQRS (→ 155) is the common combination, since writes (events) and reads (queryable state) have very different shapes.
- Correcting a mistake never edits history; a compensating event is appended instead ("Reversed(deposit=50)"), preserving the full, honest record of what happened, including the correction.
Practical example
(illustrative). A bank account event-sourced as Opened(0),
Deposited(100), Withdrawn(30), Deposited(50) replays to a balance of
100 − 30 + 50 = 120. "What was the balance between the second and third event" is a
direct query against the log — replay up to that point gives 100. A traditional
current-state-only table could not answer that question at all, since it never stored
intermediate balances.
When to use it
- Use for domains that genuinely need a perfect audit trail and the ability to answer "what did this look like at time T" — ledgers, regulatory-heavy workflows, systems where disputes require reconstructing history.
- Avoid for most ordinary CRUD systems, where the business never asks "what was it then," only "what is it now."
- Failure scenarios: evolving the event schema over years — a field's meaning changes, an event type is renamed — without a versioning and upcasting strategy makes old events unreadable by new replay code; skipping the read model and replaying full histories on every request for "what is the current state."
- Simpler alternative: a current-state table plus a separate audit log (→ 171) gives most teams the traceability they actually need, with ordinary CRUD querying intact and none of event sourcing's replay or schema-evolution cost.
Practice
(pattern — implement minimal, spot the misuse; 25–30 min, lab Postgres or
Kafka as the event log). Model a simple account as an append-only events table
(account_id, seq, type, amount, timestamp); write a replay(account_id) function that
folds the events into a current balance. Append a correcting event for a mistaken
withdrawal instead of editing the original row, and confirm replay produces the corrected
balance while the original event remains visible in history. Done when replay from scratch
and replay from a snapshot-plus-recent-events produce the same balance.
Key takeaway
Event sourcing makes history the primary record and current state a derived, replayable view — powerful for audit and point-in-time queries, costly for ordinary querying and schema change. Done when you can: replay a small event log to a current state by hand, and name the simpler current-state-plus-audit-log alternative. Used next in → 155 (CQRS read models are the usual pairing), → 139 (Saga's compensating transactions follow the same append-never-edit discipline).
References
Designing Data-Intensive Applications — see references.md.
Source directory