Section 8.1

Persistent state

Definition

Persistent state is the information a system keeps in storage that survives a restart and a loss of power, such as flash memory §6.6 or a disk. Everything else is volatile state: values held in RAM or in processor registers, which a loss of power erases. Whether a value is persistent depends on where it is stored and on whether its write has finished, not on what the program calls it. After a restart, the system rebuilds its working state from the persistent state alone.

The idea

A sensor keeps its sampling interval, the number of seconds between two measurements, in flash memory. Power fails while a user changes the interval from 7 to 8 seconds. When power returns, the processor starts again from its first instruction and everything in RAM is gone, including the variable noting that a change was in progress. All the sensor can know is what flash memory holds: the complete old record for 7 and part of a new record for 8.

Persistent state is the system’s notebook. After a restart the system remembers only what the notebook says, so every fact that recovery needs must be written down before the failure. The picture fails in two ways. A line in a notebook is written or not, but a stored record can be half written. And stored bytes may be old, damaged or put there by an attacker, so the system checks each record before it believes it.

How it works

Designing persistent state means deciding, for every piece of information, what happens to it at a restart. Each piece falls into one of three classes.

A durable record usually holds more than its value: an identifier that says which item it stores, a sequence number that grows with each new record so that the newest can be found, the length of the data, the data itself, and a check value. A check value is a short number computed from the other bytes of the record, such as a CRC §4.32 (cyclic redundancy check). Recovery computes the check value again and rejects the record if the two differ, which catches almost all incomplete or damaged records.

At start-up, a recovery procedure, the code that turns stored bytes back into working state, runs in this order:

  1. Read the stored records, never reading past the end of the storage area.

  2. Reject every record whose check value or format is wrong.

  3. Where several valid records hold the same item, select one as authoritative, normally the one with the highest sequence number.

  4. Rebuild the reconstructed values from the selected records.

  5. For any item that has no valid record, use a default value, or refuse to start if no safe default exists.

Each record also needs a stated list of the failures it must survive, because different failures preserve different things. On many microcontrollers §6.1, small computers on one chip, a reset that keeps power applied leaves RAM physically intact, but the start-up code then sets the program’s variables to their initial values; only a region that the program deliberately excludes from that step keeps its content, and only until power fails. A loss of power during a write leaves a torn write §8.19, a record of which only part was stored. Worn flash memory cells can lose bits. An attacker with access to the storage can write back an old copy that is still valid. A cache, a copy kept only to save time, can be rebuilt after any of these failures; a record of the oldest software version the device may still run must survive all of them and resist the attacker.

Worked example

Take a 4-byte record: a sequence number, an item identifier, the value, and a check byte equal to the sum of the first three bytes modulo 256 (the remainder after division by 256). Real systems use a CRC, which catches far more errors; the sum keeps the arithmetic short. Erased flash memory reads as bytes of value 255, written 0xFF in hexadecimal.

Before the failure, flash memory holds record 1: sequence number 1, item 16 (the sampling interval), value 7, and check byte 1+16+7=241 + 16 + 7 = 24. The device then starts record 2: sequence number 2, item 16, value 8, whose check byte should be 2+16+8=262 + 16 + 8 = 26. Power fails after the first three bytes of record 2 are written, so its fourth byte still holds the erased value 255. At that moment RAM holds an index entry saying that item 16 is in record 1, the decoded value 7, a variable saying that a change is in progress, and a connection on which the user waits for a reply.

After the restart, RAM holds none of this. Recovery reads record 1, computes 1+16+7=241 + 16 + 7 = 24, finds 24 in the check byte and accepts the record. Recovery reads record 2, computes 2+16+8=262 + 16 + 8 = 26, finds 255 and rejects the record. Item 16 has one valid record, so the interval is 7 seconds, and recovery rebuilds the index entry and the decoded value from record 1. The variable saying that a change was in progress is lost, and recovery does not need it: the rule “use only records that pass their check” already gives the right answer. The connection is lost for good. The user’s tool receives no reply and must send the request again.

Had the correct check byte for record 2 happened to be 255, the incomplete record would have passed its check. This is why real designs use a check value of 16 or 32 bits, which a torn record passes only by rare chance, or write a separate completion marker as the very last byte of a record.

Stated precisely

Let DD be the bytes in persistent storage when power returns, let VV be the content of RAM and processor registers just before the failure, and let RR be the recovery procedure. The system restarts in the state R(D)R(D). VV does not appear in this expression: the content of VV affects the restarted system only through what was written into DD before the failure.

A design is correct across restarts when R(D)R(D) is an allowed state for every DD that a failure can leave behind. The failure model §1.30, the written list of failures the design must survive, fixes which DD can occur. The claim rests on five assumptions.

  1. Completed writes stay. A write that finished before the failure is still readable afterwards, within the medium’s rated retention time and number of erase cycles.

  2. Interrupted writes are contained. An interrupted write damages only the record being written, and that record’s check value reveals the damage.

  3. Recovery reads only DD. RR depends on nothing that the failure erased.

  4. Recovery can itself be interrupted. If RR writes to storage, a second failure during RR leaves another DD, and RR must handle that DD too.

  5. Nobody else writes DD. If an attacker can change the storage, records also need authentication §1.6, a cryptographic check that only a holder of a secret key could have produced them, and protection against an old valid copy being written back.

Limits and common mistakes

Stored is not the same as valid, current or genuine. A record in flash memory can be incomplete, damaged by worn cells, older than a record that replaced it, or a copy that an attacker put back. Recovery decides which records to believe; the storage does not.

A variable whose name says persistent or retained is not durable unless its value is written to storage that survives the failure in question. RAM that keeps its content across a reset still loses it when power fails.

A requested write has not necessarily finished. Software layers and storage devices hold data in RAM buffers before they write it, so a value counts as persistent only after its write is confirmed complete. The Durability §8.2 entry treats this boundary.

Every fact that recovery needs must be stored. If the device must know after a restart that an update was in progress, that fact needs its own durable record, or must follow from records that exist, such as a complete new version that no record yet selects.

Storing more is not safer. Flash memory blocks survive a limited number of erases, so storing a value that could be reconstructed costs wear and gains nothing.

The state of other devices is not part of the local persistent state. Whether a peer received a reply before the failure cannot be learned from local storage.

Questions answered

Which records survive a reset and which are reconstructed?

The records that were completely written to persistent storage, such as flash memory or a disk, before the reset survive it; everything held only in RAM or in processor registers is either reconstructed from those records or lost. Reconstructed values are those that can be computed again from surviving records, such as an index of where each record is stored or a decoded copy of a setting. Lost values are those that no record describes, such as an open connection or a variable noting that an operation was in progress.

Surviving is not enough for a record to be used. At start-up the recovery procedure reads each record, rejects every record whose check value or format is wrong, picks the newest valid record for each item, and only then rebuilds the working state; a record that was being written at the moment of failure fails its check, so the previous valid record counts. A reset that keeps power applied may leave RAM physically intact, but only a region that the start-up code deliberately leaves alone keeps its content, and only until power fails. A surviving record can also be old, or written by an attacker, which a check value does not reveal.

Related entries: Durability (§8.2), Crash consistency (§8.4), Finite-state machine (§8.33).