Event sourcing
A bank account stored as a list of things that happened, not as a balance. We replay the list to get the balance, look into the past, take a shortcut, and meet the format problem.
In 30 seconds
Most systems store how things are now and overwrite it on every change. Event sourcing stores each change instead, as an event added to the end of a log that is never edited.
The current state is worked out by replaying the events in order. Since nothing is overwritten, the log is also a complete history: any past state can be rebuilt.
Replaying a long log is slow, so systems save snapshots and keep ready-made views. And because events live for ever, code must keep understanding old event formats.
Event sourcing, step by step
Simplified: a real log also records who or what caused each event, and guards against two writers appending at once. Event sourcing adds real complexity and suits only some systems.
- Step 1: Storing only the present. Most systems keep one row per thing and overwrite it. Ana’s bank account shows a balance of 120, and the 70 it replaced has vanished. The table can say where things stand now, but not how they got there, who changed them, or what they were last Tuesday.
- Step 2: Store what happened. Event sourcing stores the changes themselves. Each event is a small record of something that happened, written in the past tense with its time and details: account opened, 100 deposited, 30 withdrawn, 50 deposited. New events are added at the end of the log. Existing ones are never edited or removed.
- Step 3: Replay to get the state. The current state is worked out, not stored. Start with nothing and apply the events in order: opening gives 0, the deposit makes 100, the withdrawal 70, the second deposit 120. This is called replaying the log. Because the events never change, replaying always gives the same answer.
- Step 4: A full audit trail. Because every change is kept with its time, the log doubles as an audit trail. To see the balance on Tuesday evening, replay only the events up to then: 70. A mistake is corrected by appending a new event that reverses it, so even the mistake and its correction stay on record.
- Step 5: Snapshots. A long-lived account might have a thousand events, and replaying all of them on every request is slow. So now and then the system saves a snapshot: the state as of a certain event. Loading then starts from the snapshot and replays only the events after it. The snapshot is a shortcut; the complete log is still kept.
- Step 6: One log, many views. Replaying one account is cheap, but “list every account above 100” would mean replaying them all. So small programs called projectors follow the log and keep ready-made tables up to date, called read models. Each is derived data: it can be deleted and rebuilt from the log, and new ones can be built later from old events.
- Step 7: When event formats change. Events are kept for good, so their format outlives the code that wrote them. Suppose deposits later gain a currency field. The old events have none, and rewriting history defeats the point. One common answer is an upcaster: code that converts an old event to the new shape as it is read, here by assuming euros.
Try it yourself
Learn this first
Spotted a mistake on this page?