← Lessons

quiz vs the machine

Silver1110

System Design

The Event Sourcing Pattern

Storing the full history of changes as events instead of only the latest state.

4 min read · intro · beat Silver to climb

State as a sum of events

Most systems store the current state of a record and overwrite it on every change. Event sourcing flips this. It stores an append only sequence of events that describe each change, and the current state is computed by replaying them.

How it works

  • Every change becomes an immutable event such as money deposited or money withdrawn.
  • Events are appended to a stream, never updated or deleted.
  • To get the current balance you fold all events for that account from the start.

What you gain

  • Full audit trail because no history is lost.
  • Time travel since you can rebuild the state as of any past moment.
  • New read shapes by replaying the same events into a different view.

What it costs

  • More complexity than a simple update in place.
  • Replay cost that grows with history, which snapshots later help with.

Key idea

Event sourcing keeps the full list of changes as immutable events and derives current state by replaying them.

Check yourself

Answer to earn rating on the learn ladder.

1. In event sourcing, the current state is

2. Which property do events in an event sourced store have?