Definition
Crash consistency is the property that, after a crash or loss of power at any moment, the recovery procedure can turn the data left in persistent storage into a state that satisfies the system’s invariants. Persistent storage, such as flash memory §6.6 or a disk, survives a loss of power. The recovery procedure is the code that checks and repairs that data at start-up. An invariant is a condition that every valid state must meet. The recovered state need not be the old or the new state, only a valid one.
The idea
A device updates its firmware, the program stored in its flash memory, with three writes: it erases a spare area, writes the new version there, and then writes a record telling the bootloader §6.30, the small program that runs first after every reset, to start the new version. Power can fail before, during or after any of the three writes. For each such moment the question is whether the bootloader then starts a version that works.
Think of the sequence of writes as a film, and stop it at any frame, including frames in the middle of one write. Crash consistency means that every frame is safe to stop on: whatever storage holds at that frame, recovery turns it into a valid state. The film picture misses one thing. Storage does not always hold a clean frame. A write in progress can leave a torn write §8.19, a record of which only part reached the medium, and recovery must handle that too.
Crash consistency is weaker than failure atomicity §8.5, which demands exactly the old or exactly the new state. A data logger that appends five measurements and loses power after three has three valid measurements in storage. Recovery that keeps those three and discards the incomplete fourth is crash consistent, because every stored measurement is complete and valid, although the append of five was not all or nothing.
How it works
Establishing crash consistency is a procedure.
State the invariant. For example: the version that the bootloader starts is complete, passes its check, and its program and its configuration belong to the same release.
List the writes. Write down, in order, every operation that changes persistent storage. Include erases, the writes made by garbage collection §8.16, which reclaims space by copying records that are still needed and erasing the old blocks, and the writes made by the recovery procedure itself.
State the failure model §1.30 for each write. An interrupted write can leave its target unchanged, complete or torn. On flash memory, a torn program operation leaves some bits programmed and others still erased, and an interrupted erase leaves a block partly erased.
Enumerate the crash points. A sequence of writes has points between writes, counting the point before the first write and the point after the last, and points inside a write: points in all.
Run recovery on each crash point. Determine what storage holds, which records pass recovery’s checks, what recovery selects, and whether the invariant holds.
Repeat for recovery’s own writes. If recovery writes, for example to clean up a torn record, a failure during recovery is one more crash point, and recovery run again must still succeed.
The enumeration is valid only if the writes reach the medium in the listed order. Caches and storage controllers may reorder writes unless the program waits for each write to complete or issues an ordering command, as the Write ordering §8.20 entry explains. A variable in RAM that records the current phase of the update does not help: the failure erases it, so recovery must work out the phase from storage alone.
Designs that keep the enumeration short share one pattern: write new data to a place that recovery ignores, and let one small final write decide whether recovery uses it. Shadow paging §8.9, copy-on-write §8.11 and write-ahead logging §8.12 are such designs.
Worked example
Storage holds two firmware slots and a selector area. Slot 0 holds version A, made of program A and configuration P, with a correct check value, a short number computed from the slot’s content. The selector area holds one selector record §8.21, “slot 0, sequence number 4”, followed by its own check value. The bootloader follows one rule: among the selector records whose check value is correct, take the one with the highest sequence number and check the slot it names; if that slot fails its check, use the valid selector record with the next lower sequence number. The update to version B, program B with configuration Q, makes three writes:
: erase slot 1;
: write program B, configuration Q and their check value into slot 1;
: append the selector record “slot 1, sequence number 5” with its check value after the existing record.
With writes there are crash points.
Before : slot 1 holds old data; the newest valid selector record is number 4; the bootloader starts A.
During : slot 1 is partly erased; record 4 still names slot 0; A starts.
Between and : slot 1 is erased; A starts.
During : slot 1 holds part of B and Q and no correct check value; no selector record names slot 1; A starts.
Between and : slot 1 holds a complete, correct copy of B with Q, but no selector record names it; A starts. The new version is stored but not committed.
During : record 5 is torn and fails its check; the newest valid record is still 4; A starts.
After : record 5 is valid and names slot 1; slot 1 passes its check; B starts with Q.
At every point the bootloader starts a complete, matching pair, (A, P) or (B, Q), so the invariant holds at all seven points.
Two variations show why each part of the rule matters. If the writes were reordered so that reached flash memory before had finished, record 5 would name a slot that fails its check, and only the fallback to record 4 would keep A starting. And when the selector area fills up it must be erased; if that erase destroys the only valid selector record and power fails before a new one is written, nothing starts. The repair is a second selector area: copy the newest record there before erasing the first.
Stated precisely
Let be the writes of an update protocol in order, let be the recovery procedure, and let be the invariant, a condition that a state either meets or does not. For , let be the storage content after writes to have completed; is the content before the first write. For , let be the set of storage contents that an interruption of can leave. The protocol is crash consistent when meets for every that is one of or belongs to one of , and also for every storage content that an interruption of itself can leave.
The claim depends on four assumptions, each of which must hold for the hardware in use.
Order. The writes reach the medium in the listed order, or the crash states include every reordering that the hardware allows.
Contained damage. An interrupted write affects only its own target, not other records.
Completed writes stay. A write that completed is still intact after the failure.
Detection. Recovery’s checks reject every torn record. A check value such as a CRC §4.32 (cyclic redundancy check) does not guarantee this: whether a given torn record passes the check is decided by the old and new contents and by which parts of the write completed, so this assumption must be established for the actual record format and write order.
Limits and common mistakes
Testing some crash points samples the behavior; it does not establish crash consistency. Power-loss injection §8.38 and reset injection §8.37, which cut power or force a reset at chosen moments during a test, find bugs but cover only the moments they hit. An exhaustive enumeration §10.33 covers every crash point, but only for the failure model it assumes.
Crash consistency does not include durability §8.2. A crash-consistent design may lose the latest operations: in the logger example, the fourth and fifth measurements are gone, and that is allowed unless they were reported as safely stored.
Crash consistency is not resistance to attack. An attacker who writes back an old, valid copy of storage produces a state that meets the invariant. Preventing that needs anti-rollback §8.26 protection, which rejects versions older than the newest one accepted.
Damage outside the failure model, such as worn flash memory cells or a write that corrupts a different record, is not covered by an argument that assumes it away.
Writes outside the update itself still count. Erasing a block to reclaim space, moving records during garbage collection and writes made by recovery all belong on the list, and they are easy to leave off. So are writes hidden inside one call: a single library call may perform several writes, and power can fail in the middle of any of them.
Questions answered
List every interruption point between writes and the resulting boot state.
Every interruption point is found by listing the update’s writes in order and taking the points before, during and after each write: writes give points, and each point inside a write stands for every partial result that the write can leave. Take a typical two-slot firmware update. Version A runs from slot 0. The bootloader, the program that chooses what to start after a reset, uses the newest selector record whose check value is correct, and falls back to an older record if the slot named fails its check. The update writes = erase slot 1, = write version B with its check value into slot 1, and = append a selector record that names slot 1 with a higher sequence number. The seven points and their boot states are:
before : A starts;
during : A starts (slot 1 partly erased and not selected);
between and : A starts;
during : A starts (slot 1 incomplete and not selected);
between and : A starts (B complete but not selected);
during : A starts (the torn selector record fails its check);
after : B starts.
The list must also include the writes that are easy to forget: erasing the selector area when it is full, which must never destroy the only valid selector record; moving records during garbage collection; and every write that recovery itself makes, since a failure can cut recovery short and recovery must then succeed when it runs again. The boot states above are correct only if the writes reach the medium in the listed order, completed writes stay intact, and a torn record always fails its check.
Related entries: Atomicity (§8.3), Garbage collection (§8.16), Torn write (§8.19), Reset injection (§8.37), Power-loss injection (§8.38).