The till: set state and derived state
A cash register keeps a tape, not a number: why a derived total beats a stored one, and what the ledger is to your data.
A cash register does not keep the cash in the drawer as a number it overwrites. It prints every sale and every refund on a tape, the drawer's total is the sum of the tape, and at close the Z report recomputes the tape and says whether it holds. This page tells Durable's eleventh rule, one record and derived views, as that register: the stored number is set state, the tape is derived state, and the ledger is the tape of your data. The Why page plays it; this page says it in prose.
The till#
Three parts. The tape: every sale is a line, every refund a line with a minus sign, each numbered and printed after the one before, never erased. The drawer: what the tape sums to. The Z report: at close the register reads the tape from its first line, recomputes the total and prints it beside the count of lines, so anyone can hold the tape to the drawer.
The register could have been built the other way: one number in memory, added to on a sale, subtracted from on a refund, printed at close. It was not, and the reason is the whole of this page.
Set state: a stored number#
Set state is the number. A sale reads it, adds the sale and writes it back. A refund reads it, subtracts and writes it back. The number is always current and nothing else is kept. Four things go wrong with it.
- Lost history. The number says $65.25. It does not say how many sales made it, which was refunded, or what it said an hour ago. The old value is gone at the moment the new one lands.
- A crash between the write and the status. A sale writes the number and then records the receipt. The process dies between the two. The number holds the sale; nothing says a sale happened. The number is wrong by one sale, and the morning cannot tell which.
- A double-apply on a retry. The retry of that sale reads the number that already holds it and adds it again. Two writes, one sale, and the number cannot show it: it is one number.
- An edit nobody can explain. Someone sets the number right by hand. Now it is right, and nothing says it was wrong, by how much, or who changed it.
Each of these is a column on a job table: status, balance,
stock_on_hand, attempts. A status column that says shipped while the
label was never bought is this number; so is a stock mirror the provider
moved past an hour ago.
Derived state: a tape#
Derived state is the tape. Nothing is overwritten: a sale appends a line, a refund appends a negative line, and the total is a fold over the lines, computed when it is asked for. The same four cases, on the tape:
- Replay. After a crash the process starts again, reads the tape from its first line and sums it. A line the crash cut short was never printed whole, so it is not on the tape and the total is exact. The retry of that sale prints its line once.
- Audit. The tape is the history. Which sales, which refund, in what order, and what the total was after any of them: read the lines to that point.
- Undo as a reversing entry. A refund is not an edit of the sale's line; it is a new line that reverses it. Both stay. The tape shows the sale, the refund and the moment of each.
- A total anyone can recompute. The Z report is a sum anyone can do again with the tape in hand. Each line carries the hash of the line before it, so a line changed or removed after the fact breaks the chain at that line, and the report names it.
A stored number is cheaper to read. The tape is cheaper to be right about, and the total is read far less often than it is trusted.
In Durable#
The rows of the durable schema are the tape. A run row is the sale
rung; a step row is the step's intent written before the call and its
answer imaged after it; a ledger row is the image a write found and the
image it left, hashed on the row before; a fact row is what the run
said to the rest of the app. None is updated after it lands: the
ledger's interface has no update and no delete, and a database trigger
refuses either.
The total is derived. A run's status and its result are read off its rows; a redrive replays the finished steps from their rows and re-invokes the rest, so a step that landed before a crash is never called twice. The console's views, the overview, the pressure, the issues, the facts a run emitted, are queries over the rows, computed when asked for and never stored a second time.
The Z report is durable-sdk ledger verify --file <export> over an
export, or the operator API's verify read over the live rows: every
chain recomputed from genesis, the first link that does not hold named.
The reversing entry is durable-sdk undo --scope <scope> --since <iso> --api <url>: a compensation is a row too, what it found and what it
restored, naming the row it reverses.
How it works draws what is stored against what is derived; Design paradigms states the rule and the doors that hold it; Data in the ledger says what a row may carry.