Section 8.13

Append-only log

Definition

An append-only log is a way of storing data as a sequence of records in which each new record is written directly after the last one, at the end called the tail, and no record is changed once written. A change is saved by appending a record that describes it, such as the new value of a setting. The current state is rebuilt by reading the records in order, later records taking precedence. Old records are removed only by a separate step that reclaims space.

The idea

Overwriting data in place is fragile: a power failure during the overwrite can leave part of each value, and neither survives. Flash memory §6.6, the usual permanent storage of a microcontroller §6.1 (a small computer on one chip), is worse: a bit returns to 1 only through an erase of a whole erase block, often 4096 bytes, so overwriting one value first destroys every value in its block.

Appending writes only into unused space, so stored records stay exactly as they were, and a power failure can damage at most the record being written, the last one. Recovery, the code that rebuilds the state at start-up, need only decide whether the last record is complete. But a half-written last record is just bytes, so the record format must carry evidence of completeness.

How it works

A record has three parts: a fixed-size header with at least the payload length, and often a key (the name or number of the item the record concerns); the payload, the data; and a check value over header and payload, typically a CRC §4.32 (cyclic redundancy check), a short code that almost every accidental change to the bytes alters. Some formats add a commit marker §8.21, filled in a separate final write, whose presence says that the record is complete. The writer appends the parts directly after the previous record, marker last, and the record counts as saved only after its last byte is stored.

Recovery scans from the start of the log area; an offset is a position in bytes from that start.

  1. If fewer bytes remain than a header needs, or the header bytes all still hold the erased value (0xFF on typical flash memory), the log ends here.

  2. Reject the record if its declared length exceeds the format’s maximum or the record would extend past the end of the area. Make this test before reading the payload, so that a damaged length can never send the reader outside the area.

  3. Reject the record if the check value computed over header and payload differs from the stored one, or if a required commit marker is missing.

  4. Apply the record, advance the offset by its verified size, and return to step 1.

The first rejected record ends the valid log. It and everything after it form the partial tail, which recovery never applies.

On flash memory the partial bytes cannot be rewritten without erasing their erase block. Before appending again, the writer either moves that block’s valid records elsewhere and erases it, or continues in a fresh block, which recovery must then also scan.

Worked example

A 64-byte log area, offsets 0 to 63, starts erased, every byte 0xFF. A record is a 4-byte header (a 2-byte key and a 2-byte payload length, both stored low byte first, in little-endian §4.21 order), the payload, and the common CRC-32, the CRC with a 32-bit check value that zlib and Ethernet use. A payload of nn bytes therefore gives a record of 4+n+4=n+84 + n + 4 = n + 8 bytes. Record 1 sets key 1 to a 4-byte value and occupies offsets 0 to 11. Record 2 sets key 2 to an 8-byte value and occupies offsets 12 to 27. Record 3 sets key 1 to 42 and would occupy offsets 28 to 39, but power fails after offsets 28 to 34 are stored.

Recovery accepts record 1 (0+12=12≤640 + 12 = 12 \le 64, CRC matches) and record 2 (12+16=28≤6412 + 16 = 28 \le 64, CRC matches). At offset 28 the header declares 4 bytes and 28+12=40≤6428 + 12 = 40 \le 64, so the length test passes. The payload reads 2A 00 00 FF, which as a little-endian number is 4,278,190,122, a value nobody wrote. The CRC-32 of offsets 28 to 35 is 0xD0943D36, while the stored CRC at offsets 36 to 39 still reads 0xFFFFFFFF, so the record is rejected. The rebuilt state is the state before record 3: the unfinished update is lost, and nothing is corrupted.

Had power failed after offset 30, the length field would read 04 FF, which is 4+255×256=65,2844 + 255 \times 256 = 65{,}284, and the record would end at 28+4+65,284+4=65,32028 + 4 + 65{,}284 + 4 = 65{,}320, far beyond 64. Step 2 rejects it before any payload is read, where a reader trusting the length would read about 65,000 bytes beyond the area.

Stated precisely

Let the log area hold NN bytes, let hh and cc be the sizes of the header and of the check value, and let ℓmax\ell_{\mathrm{max}} be the largest payload allowed, all in bytes. A record at offset oo that declares payload length ℓ\ell is accepted if and only if:

  1. o+h≤No + h \le N, and the header bytes are not all erased;

  2. ℓ≤ℓmax\ell \le \ell_{\mathrm{max}} and o+h+ℓ+c≤No + h + \ell + c \le N;

  3. the check value computed over offsets oo to o+h+ℓ−1o + h + \ell - 1 equals the stored one, and any required commit marker is present.

Recovery accepts records from offset 0 while each is accepted.

The guarantee is a prefix property: after any power failure the accepted records are exactly those whose bytes were all stored, so the rebuilt state is the state after some prefix of the appends. It assumes sequential appends (each record is started only after the previous one is stored), contained damage (a failure changes only the bytes being written, which requires a writer that never writes into used space and hardware that does not disturb neighboring bytes) and detection (the tests reject every pattern a failure can leave). Among all contents of one length of at least kk bits, exactly one in 2k2^{k} has a given kk-bit CRC, so random-looking damage passes a CRC with probability about 2−k2^{-k}.

Limits and common mistakes

A CRC needs no secret, so an attacker can rewrite an old record and recompute its CRC. Deliberate change is detected only by a message authentication code §5.7 (a check value that requires a secret key) on each record, or by a chain of cryptographic hashes §5.1 whose latest value the attacker cannot change, as in the Merkle tree §5.43 entry; records cut off the end, only if the log’s length is kept where the attacker cannot roll it back, such as in a monotonic counter §8.28.

Stopping at the first failure also discards every record after one damaged mid-log, for example by a worn flash cell; fixed-size slots or per-block headers let recovery resume after it.

Each record is saved all or nothing, but a change that spans several records is not. A commit record, written after all the records of one change, makes the group count only when it is complete.

On a file system, a record counts as saved only after a flush such as fsync has returned.

Questions answered

How are partial tail records detected?

Partial tail records are detected during the recovery scan: each record is tested for evidence that only a completely written record carries, and the first record that fails ends the log. A header still holding the erased value (0xFF on typical flash memory) marks unused space. The declared length must not exceed the format’s maximum, and the record must fit inside the log area, tested before any payload byte is read. A check value such as a CRC (cyclic redundancy check) over header and payload must match the stored one, and a commit marker written last must be present if the format uses one. The failing record and everything after it are never applied. All tests but the CRC are exact; a kk-bit CRC lets about one random-looking damage pattern in 2k2^{k} through. The method needs each record to be started only after the previous one is stored, so that only the last can be partial.

Related entries: Circular log (§8.14), Torn write (§8.19), Commit record and selector (§8.21), Merkle trees and authenticated data structures (§5.43).