← Lessons

quiz vs the machine

Platinum1880

Frontend

The Event Sourcing on the Client

Model state as a log of events you replay, unlocking undo, audit, and time travel in the browser.

6 min read · advanced · beat Platinum to climb

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.

Check yourself

Answer to earn rating on the learn ladder.

1. How is current state obtained in event sourcing?

2. Why store periodic snapshots in an event sourced client?

3. What feature does keeping the full event log naturally enable?