The WAL: Durability Before the Data Lands
The mechanism
Before a write touches the memtable, it is appended to a plain sequential file on disk. On restart, the engine replays that file from the start to reconstruct the memtable exactly as it was. Once that memtable is later flushed to an SSTable, the WAL is discarded.
When does the client get its acknowledgment?
Right after the WAL entry is durably on disk and the memtable is updated — not after the memtable is flushed to an SSTable.
A memtable flush to SSTable can take seconds to minutes; the WAL is what decouples durability from that. The memtable update is cheap enough to also do inline — and it has to be, otherwise a client could be told "success" and immediately read back stale data because the write hadn't landed in the memtable yet.
What actually gets stored in the WAL
Just the raw operations, in the order they happened:
[seq=1] SET user:42 {"name": "alice"}
[seq=2] SET user:43 {"name": "bob"}
[seq=3] DELETE user:41
[seq=4] SET user:42 {"name": "alice2"}Each entry carries: operation type (SET/DELETE), key, value, a sequence number, and usually a checksum. It contains no indexes, no SSTable metadata, nothing about compaction — just the plain history of writes.
The full write path — where each kind of error gets caught
A write is not a single step. Several checks happen before the WAL is ever touched:
Constraint checks happen before the WAL write. Only a write that already passed validation is ever logged — so the WAL only ever needs to guarantee durability for valid writes, which is what keeps replay simple.
Crash mid-write: torn WAL entries
If the process crashes while an entry is being written, the last entry ends up half-written:
How it's handled: every entry carries a checksum. During replay the engine verifies each entry's checksum in order. The moment one doesn't match, replay stops there and that entry — and everything after it — is discarded. Every earlier entry with a valid checksum is applied.
Why a write can be torn at all: a WAL entry is not written atomically. It is a sequence of bytes (op type, key, value, seq, checksum) appended via a write() syscall, durable only once the following fsync() returns. Lose power or get killed mid-write() and only some of those bytes reached physical storage. The file doesn't end with a shorter-but-valid entry — it ends with a partial byte sequence cut off at an arbitrary point.
Why checksums catch this and "does it parse" doesn't: a torn entry can still look plausible — the truncated "bo above reads like the start of a normal string. Structural validation cannot be relied on. The checksum is computed over the entry's bytes at write time; on replay the engine recomputes it over whatever bytes it read and compares. A torn entry almost certainly mismatches, because the recomputation is over incomplete data.
Why replay stops entirely rather than skipping the bad entry:
- Sequencing — entries are strictly ordered and replayed in order. If entry N is torn, entry N+1 can't be trusted either: the corruption may have desynced the reader's position in the file, so there is no reliable way to know where N+1 even begins.
- Nothing is lost by stopping — see below.
The Invariants
- Guarantees: Any write the client was told succeeded survives a crash, recoverable by sequential replay — at the cost of one sequential disk write per request.
- Does not guarantee: That unacknowledged writes survive (they are deliberately discarded), or that semantically wrong entries are detected — a checksum proves the bytes are intact, not that they are correct.