05Design patternsEvent sourcing and CQRS
05 · Reusable pattern
Event sourcing and CQRS
Separate commands from query projections and understand replay, evolution, and consistency costs.Lesson spine
What you need to understand.
Event sourcing keeps immutable facts as truth; CQRS separates command validation from read-optimized projections. They can be used together or independently.
Events instead of overwritten state
Append domain facts with stable meaning so history, audit, and temporal reconstruction remain possible.
Aggregate stream
Load a stream or snapshot, validate a command against current invariants, then append events at an expected version.
Snapshots
Persist periodic derived state to bound replay time while keeping the event log authoritative.
CQRS
Keep the write model focused on invariants and build one or more projections shaped for reads, search, or analytics.
Projection safety
Track offsets, apply events idempotently, version schemas, and keep external side effects out of blind replay.
Trade-offs
Permanent event evolution, eventual read models, debugging across projections, and storage growth are real costs.
Before the boxes
Frame the decision.
What must work
Separate commands from query projections and understand replay, evolution, and consistency costs.
What changes the design
Event rate · stream length · projection count · replay time · schema lifetime
What owns the truth
Identify the component that commits authoritative state, then separate synchronous confirmation from derived work.
What stays simple
Do not add global coordination, multi-region writes, or a specialized store until a requirement earns the complexity.
Architecture map
Trace ownership, not just traffic.
Follow the decision from left to right. Every arrow should have a reason.
Expresses intent
Checks invariants
Appends facts
Streams events
Build read models
Bounds replay
Upcasts schemas
Walk one representative request across every arrow. Say whether the handoff is synchronous or asynchronous, what identity makes a retry safe, and which step changes authoritative state.
- 01
Command — Expresses intent Define the output contract before moving to the next owner.
- 02
Aggregate — Checks invariants Define the output contract before moving to the next owner.
- 03
Event store — Appends facts Define the output contract before moving to the next owner.
- 04
Publisher — Streams events Define the output contract before moving to the next owner.
- 05
Projectors — Build read models Define the output contract before moving to the next owner.
- 06
Snapshot — Bounds replay Define the output contract before moving to the next owner.
- 07
Migration — Upcasts schemas Confirm the result and emit the evidence needed to reconcile it.
Decision table
Make the trade-offs explicit.
| Decision | Defensible position | Cost to acknowledge |
|---|---|---|
| Primary mechanism | Event sourcing earns its cost when replay and audit matter; CQRS alone may be enough. | The stronger guarantee usually adds coordination, latency, state, or operational work. |
| Sync vs. async | Keep only correctness-critical confirmation synchronous. Move derived views, notifications, analytics, and cleanup behind a durable boundary. | Async work needs idempotency, lag monitoring, replay, and a product definition for partial completion. |
| Simple vs. scaled | Begin with one logical owner and a clear API. Partition or replicate only the resource proven to be the first bottleneck. | Migration requires stable identities, versioned contracts, backfill, and a rollback path. |
Failure review
Design the recovery path.
Topic-specific risk
Permanent event schemas and unsafe replay can repeat external side effects.
ResponsePersist enough identity and state to distinguish retry, resume, compensation, and operator repair.
Dependency timeout
A timeout is ambiguous: the remote side may have failed, succeeded, or still be running.
ResponseUse deadlines, bounded backoff with jitter, idempotency keys, and a status or reconciliation path.
Overload or skew
Average capacity can look healthy while a tenant, key, partition, region, or expensive request saturates one owner.
ResponseExpose queue depth and hot-key share, apply backpressure, isolate tenants, and degrade optional work before correctness.
Evidence + level bar
Prove the design can be operated.
Health of the promise
Measure user-visible latency or freshness, correctness drift, saturation, retry volume, and time to recover. Alert on the failed promise—not only CPU.
Complete and clear
Finish the happy path, identify the state owner, choose reasonable building blocks, and explain one scale mechanism.
Trade-offs and failure
Separate read and write paths, define consistency, explain partitioning, and make duplicate or partial failure safe.
Evolution and operations
Discuss multi-region boundaries, migration, tenant isolation, capacity, observability, and how the architecture changes over time.
Interview language
Open the deep dive with a claim.
“For Event sourcing and CQRS, the decision I want to make explicit is this: Event sourcing earns its cost when replay and audit matter; CQRS alone may be enough. I’ll trace the state-changing path first, show where the result becomes durable, then test the design against the highest-risk failure and our target scale.”
08 · Retrieval check
Can you defend it without the page?
- For Event sourcing and CQRS, where is the correctness boundary and which failure would you test first?
- Which component owns committed truth, and what event or response proves the commit?
- Where is the first scaling or coordination bottleneck under the stated envelope?
- What happens after an ambiguous timeout or duplicate operation?
- Which complexity would you remove at one hundredth of the scale?