Part 5 of 8 · 2 min read
Event sourcing
History first, balance second.
The book's answer to audits is event sourcing. Instead of overwriting a balance, the system stores every change as an immutable record, and balances are derived from them.
Four ideas.
- Command: what the outside world wants, such as "transfer $1 from A to C". It is an intention and may be invalid (for example, an overdraft). Commands go into a first-in, first-out queue, and order matters.
- Event: the validated fact that results, in the past tense, such as "$1 was taken from A" and "$1 was added to C". Events must be carried out. One command can create zero or more events.
- State: what events change. Here, the map of balances.
- State machine: two jobs: validate a command and make events, and apply an event to the state. It must be deterministic: no random numbers and no outside reads, so applying the same events always gives the same state.
Commands may involve randomness or outside calls, but events are facts and so are the thing to protect.
Flow in the wallet.
- Read a command from the queue.
- Read the balances it needs from the database.
- Validate it. If valid, create events for each account.
- Apply the events to the state.
Reproducibility. Because events are immutable and the state machine is deterministic, replaying events from the start rebuilds any past state. This answers an auditor's questions:
- Where did this $1 go? Replay up to that point.
- Is today's balance right? Recalculate it from all events and compare.
- Is the new code correct? Run old and new code on the same events and compare results.
CQRS (command query responsibility segregation). The write side handles commands. Clients cannot just read the write side's state. Instead, the system publishes the events, and one or more read-only state machines build views from them: a balance view for clients, a view for a time period to check double charges, and so on. These lag a little but catch up.
A worked example (made up). Events for account A: +100, −30, −25, +10. Replaying gives 100 − 30 − 25 + 10 = 55. A support agent asks "what was the balance after the third event?" The answer is 45, found by replaying three events.
Event sourcing
Event sourcing is a design in which every change is stored as an immutable event, and the current state is computed by applying the events in order.
Example: a wallet stores "A: −$30" and never edits A's balance directly. The balance is the sum of A's events.
Check yourself: 1. Why must the state machine be deterministic? 2. What is the difference between a command and an event? 3. What does CQRS add?