Event Sourcing
Store what happened, not what is. Know how state is rebuilt from an append-only log, what snapshots and versions are for, and the costs that make this a pattern for parts of a system, not all of it.
Overview
Event Sourcing keeps every change to an object as an event in an append-only log and treats that log as the record. The current state is not stored as the truth; it is computed by replaying the events. A bank account is not a row with balance = 500. It is AccountOpened, Deposited 600, Withdrawn 100, and 500 is what you get when you add them up.
Martin Fowler's 2005 description is one line: capture all changes to an application state as a sequence of events. Because the events are kept, the state can be thrown away and rebuilt from an empty application, the state at any past moment can be recomputed, and a wrong event can be corrected by replaying the log with the fix in place. Greg Young's CQRS documents describe the same idea as events being the storage mechanism, with the aggregate rebuilt by applying its events in order.
Interviewers want three things: the write path (load the stream, decide, append), the cost of replay and how snapshots bound it, and the honest list of problems: eventual consistency of the views, schema changes on immutable data, and deleting personal data from a log that must not be edited.
State versus events
The difference from ordinary current-state storage shows up in six places.
- What is written: current-state storage writes the new value over the old one. Event Sourcing writes a new event after the old ones.
- What is read: current-state storage reads the row. Event Sourcing reads the stream and replays it, or reads a snapshot plus the tail.
- History: with current-state storage it is gone unless a separate audit log is kept. With Event Sourcing it is the record itself.
- Undo: current-state storage writes the old value back, if you still have it. Event Sourcing appends a compensating event, and the original stays.
- Concurrent writers: current-state storage uses row locks, or the last writer wins. Event Sourcing rejects an append if the stream moved since it was read.
- Query by any field: current-state storage has SQL. Event Sourcing can only read by stream; other questions need a projection.
The last point is the one people forget. An event store answers 'what happened to account 42' well and 'which accounts are overdrawn' badly. Projections, tables built by consuming the events, answer the second kind of question, which is why Event Sourcing is almost always paired with CQRS.
Interview questions
- What is an event store, and how is it different from a table that holds the current state?
- How do snapshots improve performance, and what can go wrong with them?
- How do you handle a change to the shape of an event when old events cannot be edited?
- What consistency and delivery problems does Event Sourcing introduce, and how do you handle them?
- Design an event-sourced bank account. What are the events, the rules and the tests?
Sign up free to read the full topic
More Low-Level Design topics
Start free on Cariara · Coding problems · System design studio