Back to the map

Module 11 · Architectural patterns · Lesson 9 of 9

Event sourcing

Store every change as an immutable event and derive current state by replaying them. What you gain (complete history, audit, time travel, rebuildable projections), what you pay (event design, snapshots, schema evolution, querying).

DeepShould-knowSenior 13 minVideoPro Updated Nov 2026

The video walkthrough is part of Pro.

See what Pro includes
Video walkthrough · 2:33 · narrated, with captions

Why it matters

A bank does not store your balance; it stores every transaction and computes the balance. That is event sourcing, and it is the natural model for any domain where the history is the business: ledgers, inventory movements, order lifecycles, version control, collaborative documents. It gives a perfect audit trail and the ability to answer any question about the past, including questions nobody thought of yet. It also replaces a familiar storage model with a demanding one, and the interview question is whether you know when that trade is worth making.

The idea

In event sourcing, the system's source of truth is an append-only log of events, each an immutable fact about a change to an aggregate: OrderPlaced, ItemAdded, PaymentCaptured, OrderShipped. Current state is not stored as the primary record; it is derived by replaying the aggregate's events from the beginning (or from a snapshot). Writes append events; reads query projections built from them.

CommandAggregatereplay → validate → emitappendload eventsevent storeOrderPlaced · ItemAdded · PaymentCapturedsubscribeprojections → read modelssnapshot every N events to bound replay
Commands are validated against state rebuilt from the aggregate's events, then new events are appended. Projections consume the log to build read models; the log itself is the truth.

What it buys

  1. 1

    Complete history and audit

    Every change, who made it, when and why, is a first-class record. Compliance and debugging ask the log directly.
  2. 2

    Temporal queries

    What was this account's state at 3 pm last Tuesday? Replay to that point. Why did the inventory go negative? Read the events.
  3. 3

    Rebuildable projections

    Any read model can be created or corrected by replaying the log into it, which makes CQRS read models cheap to add and safe to fix.
  4. 4

    Natural event publishing

    The events are the integration contract; no separate outbox is needed because the write is the event.
  5. 5

    Debuggability

    Reproduce a bug by replaying the exact sequence that produced it.

What you pay

ConcernWhy it is hardMitigation
Event designEvents are permanent; a poorly named or over-granular event lives foreverModel from the business language; event storming; keep events about one aggregate
Replay costAn aggregate with 100,000 events is slow to loadSnapshots: persist state every N events and replay only from the latest snapshot
Schema evolutionOld events must remain readable as the model changesVersioned events; upcasters that transform old versions on read; never mutate stored events
QueryingYou cannot SELECT across event streamsProjections for every query; the write side answers only by aggregate id
Eventual consistencyRead models lag the logSame techniques as CQRS: versions, read-your-writes
Deleting dataImmutable log versus privacy regulationsCrypto-shredding: encrypt personal data per subject and delete the key; or tombstone events and rebuild projections
Team learning curveDifferent from CRUD at every layerApply to one bounded context first

Mechanics

Events for one aggregate form a stream keyed by aggregate id with a sequence number; appends use optimistic concurrency (expected version) so two concurrent commands on the same aggregate cannot both succeed. The event store is an append-only database (EventStoreDB, a Kafka topic with compaction turned off, a Postgres table with an index on stream id and version). Projections subscribe to the global log or to categories and maintain read models, tracking their position so they can resume. Snapshots are an optimisation, never the truth, and can be regenerated.

Event sourcing and CQRS

They are usually seen together because an event log is a poor read store, so read models are mandatory, and because the events are already there to feed them. They remain separate decisions: CQRS works over a conventional write model, and event sourcing can serve a single read model. Adopting both at once is the full pattern and the full cost.

Where it fits

Domains where the history is the product or the law: accounting and ledgers, inventory and stock movements, order and claim lifecycles, trading, versioned content, audit-heavy regulated workflows. Domains with collaborative or replayable state: document editing, games, workflow engines. It fits poorly for simple CRUD, reference data, high-volume telemetry (a time series store is better) and anywhere the team needs ad-hoc queries over current state more than history.

Event sourcing · Backplane