Lessons 1/4 · 25 min
The log and the state
What is true: the statement or the balance?
Kofi opens his mobile-money account history: a list of dated operations, and at the top a balance of 90 francs. Both pieces of information are correct, but they are not of the same nature. The balance can be deduced from the list; the list cannot be deduced from the balance. If only "90" were kept, you could neither explain why, nor detect an error, nor find out what happened yesterday. That asymmetry is the idea of the whole workshop.
The words
- An event is a fact that occurred: "credit of 100 to Awa", "transfer of 30 from Awa to Kofi". It has a sequence number.
- The log is the ordered series of events. It is append-only: you add at the end, you never modify or delete past entries.
- The state is the summary at a given moment: the balances, the content of accounts. It is derived from the log.
- The projection (or replay) is the computation that goes from log to state: start from an empty state and apply each event in order. As a formula: state after event k = rule(state after k - 1, event k).
A worked example
Two accounts start at 0 (Awa, Kofi). Here are six events, with the rule "a debit or transfer that would make a balance negative is rejected: no effect, but it stays in the log".
| No. | Event | Effect | Awa | Kofi |
|---|---|---|---|---|
| 1 | CREDIT Awa 100 | accepted | 100 | 0 |
| 2 | CREDIT Kofi 50 | accepted | 100 | 50 |
| 3 | TRANSFER Awa → Kofi 30 | accepted | 70 | 80 |
| 4 | DEBIT Kofi 100 | rejected (80 < 100) | 70 | 80 |
| 5 | DEBIT Awa 20 | accepted | 50 | 80 |
| 6 | CREDIT Kofi 10 | accepted | 50 | 90 |
The final state is Awa = 50, Kofi = 90. Consistency check: accepted credits 100 + 50 + 10 = 160, accepted debits 20, 160 - 20 = 140 = 50 + 90. The transfer neither creates nor destroys money: it moves it.
Why keep both?
| Log | State | |
|---|---|---|
| Answers | "what happened, in what order?" | "how much do we have now?" |
| Cost of reading a balance | O(n): replay the whole log | O(1): one key access in a dictionary (workshop 102) |
| Modification | never: you append | at every event |
| If you lose it | the history is lost | you rebuild it from the log |
So you can throw away the state and recompute it, but not the reverse. That is why systems that must account for themselves (banks, ledgers, blockchains) treat the log as the source of truth and the state as a reading convenience.
The opposite approach, which overwrites the value in place ("set the balance to 90"), keeps no trace: an error or a fraud leaves nothing to examine.
What this does not guarantee
- An append-only log is only append-only if the software and the access rights enforce it. Someone who can write to the file can change the past; we will see this in the security exercise.
- The log can contain false events (a credit that should not have happened). Replaying faithfully rebuilds what is written there, not what is true in the world.
- If a bug makes the state drift from the log, the log is authoritative: you replay.
Recap
Log = ordered facts, added at the end. State = derived, rebuildable summary. A rejected event stays in the log. The log's reliability rests on what prevents it from being modified, not on the word "append-only".
