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:
-- 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 gain | What it costs |
|---|---|
| a complete history: auditing, incident investigation, the state as of any past date | a 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 state | projections and snapshots have to be built and maintained |
| replaying events restores the state and builds new views | untested changes to the formats of old events break recovery |
| a natural foundation for integrations and sagas via domain events | searching 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 pagesVerified 8 Nov