Skip to content
Optimal Lab

System design · interactive

CQRS and event sourcing

Instead of storing the current balance and overwriting it, store every change as an event that never changes. Current state is what you get by replaying them. Different read models can be built from the same events. Drag back in time.

Event store (append-only)

  1. Mar 1AccountOpenedowner: Ada
  2. Mar 2MoneyDeposited+100
  3. Mar 5MoneyWithdrawn−30
  4. Mar 9MoneyDeposited+250
  5. Mar 12MoneyWithdrawn−80
  6. Mar 15AddressChangedLahore
  7. Mar 20MoneyWithdrawn−40

Read models, rebuilt from the events

Balance

$200

Deposits

2

Total withdrawn

$150

Address

Lahore

Replaying 7 events, the account stood at $200 on Mar 20. Nothing was overwritten, so any past state can be rebuilt and every change is audited.

  1. 1

    Each event records something that happened. Events are never updated or deleted.

    A full audit log for free.

  2. 2

  3. 3

  4. 4

In production

Tools and technologies

Event stores and stream processors make this practical.

Not tied to one language — use these names when you search docs or design a system.