Architecture
Event Sourcing
Instead of storing what things are, store every event that happened — current state is just a replay of that history.
✗ The problem
Storing only the current state loses history
A bank account row says balance = 100. That's it. How did it get there?
Which deposits, which withdrawals, in what order? A bad update silently overwrites the truth forever.
class Account {
balance = 100;
deposit(amt) { this.balance += amt; }
withdraw(amt) { this.balance -= amt; }
// how did we reach 100? no idea — overwritten.
}
No audit trail, no "why", no way to debug a wrong balance or replay
what happened. The past is gone the moment the present overwrites it.
✓ How it works
→
→
The log is the source of truth
Every change is appended as an event — never mutated, never deleted. Current state is derived by folding (replaying) the log.
const events = [
{ type: 'Deposited', amount: 100 },
{ type: 'Withdrew', amount: 30 },
{ type: 'Deposited', amount: 50 },
];
// state = fold of history, never mutated in place
const apply = (bal, e) =>
e.type === 'Deposited' ? bal + e.amount : bal - e.amount;
const balance = events.reduce(apply, 0); // 120
Event log
append-only
replay
fold(apply)
Current state
balance
✓ See it live
Append events — replay or time-travel
Add events to the log below, then Replay to fold them into a balance step by step, or drag the slider to see the balance as of any past event.
Balance
$0
no events yet
✓ Takeaway
The log never lies
- Full audit trail — every change is a fact you can inspect, forever.
- Time-travel & debugging — rebuild state as of any point in history to see exactly how a bug happened.
- Rebuild any projection — replay the same log into new read models without touching the source of truth.
- Natural fit with events — domain events, not table rows, become the primary data.
- Caution: event schema versioning, replay cost at scale (use snapshots), and eventual consistency of derived views.
- Used in: ledgers, git commits, Kafka topics, banking systems.
🎯 Combines / relates: the event log is like stacked
Mementos/Commands;
distributed via pub-sub; commonly paired with
CQRS.
Back to all topics →