Bookmarks

Event sourcing

A data storage pattern in which state is kept not as tables but as an immutable sequence of events; the current state is obtained by replaying them.

Also: event-sourced persistence, storing state in an event log

checked 8 Nov

All terms

A typical system overwrites the current state on every operation — and thereby erases the history: after “balance became 70” it is no longer visible which operations produced it. Event sourcing turns the approach around: the changes themselves go into the store — the events “deposited 100”, “withdrawn 30” — and the balance of 70 is derived by replaying the log.

How it works

Each change of state is recorded as an event that captures what happened: “Order created”, “Order paid”, “Item shipped” — not just the final value. Events are written to a log that is append-only: past entries are neither modified nor deleted. The current state is not stored but computed by replaying the events in sequence. Example for a bank account:

sqlPostgreSQL
-- log: past entries are never modified, only appendedINSERT INTO account_events (account_id, kind, amount)VALUES (42, 'deposited', 100), (42, 'withdrawn', 30); -- state is a fold over the logSELECT sum(CASE kind WHEN 'deposited' THEN amount                     ELSE -amount END) AS balanceFROM account_eventsWHERE account_id = 42;

Projections and snapshots

Replaying the entire log on every request is expensive, so reads are moved into projections — ready-made views of the state built for specific tasks and updated as new events arrive. This arrangement, with writes going to the log and reads coming from the projections, usually coincides with CQRS: one model for commands, another for queries. To avoid replaying the log from scratch, a snapshot of the state is saved every N events; recovery starts from the latest snapshot and replays only the events after it.

Where it pays off and what it costs

What you gainWhat it costs
a complete history: auditing, incident investigation, the state as of any past datea different data model and a different way of thinking: the team must know how to work with events
debugging: you can see which chain of events led to the stateprojections and snapshots have to be built and maintained
replaying events restores the state and builds new viewsuntested changes to the formats of old events break recovery
a natural foundation for integrations and sagas via domain eventssearching the log requires separate projections; events have no arbitrary indexes

The pattern was described by Martin Fowler in the article Event Sourcing on martinfowler.com in December 2005.

Event sourcing is chosen when the history itself matters: auditing, debugging, restoring the state as of a given date. It is not for typical CRUD applications, which need only the current state and simple operations.

Sources

the term is written from these pages

Verified 8 Nov

Not to be confused with

what it is mixed up with and how it differs

In depth in the references

where the topic is covered in detail