Sub topic 16
Recovering after a crash
This chapter assumes you know how the memtable, write-ahead log (WAL), and flush work from earlier chapters. It covers what a store must do when the process restarts after a crash.
What a crash destroys
A crash wipes memory. The memtable disappears, along with any flush that was half done. Files already on disk survive. Recovery has to rebuild a consistent state from those files.
| Component | After a crash |
|---|---|
| Memtable | Lost, rebuilt from the WAL |
| WAL | Survives up to the last durable write |
| Complete SSTables | Survive, listed in the manifest |
| Partial SSTable from an interrupted flush | Present but not trusted |
The recovery sequence
- Read the manifest, a small file that lists which SSTables are live. Real engines append to it with every flush or compaction.
- Open the live SSTables. Delete any SSTable file the manifest does not list. These are leftovers from an interrupted flush or compaction.
- Find WAL files that were not yet retired. A WAL is retired only after the flush of its memtable is recorded in the manifest.
- Replay each WAL record in order into a fresh memtable. Later records overwrite earlier ones, so the memtable ends in the state it had before the crash.
- Accept new writes.
Why the order of operations matters
A flush must write the SSTable fully, make it durable with fsync, record it in the manifest, and only then delete the WAL. If a crash hits between any two steps, recovery still works.
- Crash before the manifest update: the new SSTable is unlisted and is deleted. The WAL still holds the data, so replay restores it.
- Crash after the manifest update but before the WAL delete: replay writes the same keys again. This is safe because a put is idempotent. Applying the same put twice gives the same result, and the memtable value matches what the SSTable holds.
Reversing the order would lose data. If the WAL were deleted first, a crash before the SSTable became durable would leave the data nowhere.
Torn writes
The crash can hit in the middle of a WAL record. Each record therefore carries a length and a checksum. During replay, stop at the first record that is incomplete or fails its checksum. That record was never acknowledged as durable, so discarding it is correct.
import struct
import zlib
def encode_record(key: bytes, value: bytes) -> bytes:
body = struct.pack(">II", len(key), len(value)) + key + value
return struct.pack(">I", zlib.crc32(body)) + body
def replay(data: bytes):
pos = 0
while pos + 12 <= len(data):
(crc,) = struct.unpack_from(">I", data, pos)
klen, vlen = struct.unpack_from(">II", data, pos + 4)
end = pos + 12 + klen + vlen
if end > len(data) or zlib.crc32(data[pos + 4:end]) != crc:
break # torn or corrupt tail: stop here
yield data[pos + 12:pos + 12 + klen], data[pos + 12 + klen:end]
pos = end
log = encode_record(b"a", b"1") + encode_record(b"b", b"2")
torn = log + encode_record(b"c", b"3")[:-1] # last record cut short
print(list(replay(torn))) # [(b'a', b'1'), (b'b', b'2')]Exercise
A crash happens after a flush wrote its SSTable and updated the manifest, but before the old WAL was deleted. What does recovery do, and why is the result correct?
Show answer
Recovery opens the SSTable listed in the manifest and replays the old WAL into a new memtable. The keys appear in both places with identical values. Reads check the memtable first and find the same data, so the answer is unchanged. The WAL is retired at the next flush.