1 of 4

Your RightSignal

Learn1 / 3

Event sourcing

The concept

Why your bank statement lists every transaction, not just your balance

Event sourcing means storing every change as a permanent record, called an event, instead of storing only the current state. The current state is worked out by replaying the events in order.

A bank account shows the idea. The ledger records 'deposit 100', 'withdraw 30', 'deposit 50'. The balance is not the source of truth. It is what you get by starting at zero and applying each entry in turn.

The events sit in an append-only log: new events go on the end, and old ones are never edited or deleted. A mistake is fixed by adding a new correcting event.

What it’s forComputes the current state of something from its history of events.

Sₙ = apply(Sₙ₋₁, eₙ), starting from S₀

S₀
the starting state, e.g. a balance of 0
eₙ
the n-th event in the log, e.g. 'withdraw 30'
apply
a rule that takes a state and one event and returns the new state
Sₙ
the state after the first n events

What it givesThe state at any point in history, including now.

  1. S₀ = 0, e₁ = +100 → S₁ = 100
  2. e₂ = −30 → S₂ = 70
  3. e₃ = +50 → S₃ = 120