State as a log of events
Event sourcing stores state not as a single mutable value but as an append only log of events that describe what happened. The current state is computed by replaying those events through a reducer from an initial value.
- Each event is a fact, like item added or field changed.
- Replaying the log rebuilds the current state.
- Nothing is overwritten, only appended.
What the log unlocks
Keeping the history rather than just the latest value gives powerful features almost for free.
- Undo and redo by moving a pointer back and forth along the log.
- Time travel debugging by replaying to any past point.
- Audit trails because every change is recorded as a fact.
Costs and snapshots
Replaying a long log is slow, so you periodically store a snapshot of computed state and replay only events after it. Events should be immutable and versioned, since changing an event meaning later can break old replays.
- Snapshots cap the cost of rebuilding state.
- Versioned events let you evolve the schema safely.
- This pattern pairs well with collaborative and offline sync.
Key idea
Event sourcing models client state as an append only log of events replayed through a reducer, unlocking undo, time travel, and audit, with snapshots to keep replay fast.