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.

The ACK path. The client sends a write request. Step one appends it to the WAL with write and fsync; from here it is durable. Step two inserts it into the in-RAM memtable; from here it is readable. Step three returns the acknowledgement, and the client hears success. The flush to an SSTable happens later in the background, seconds to minutes afterwards, and is not on the ACK path.
The client hears “success” after the fsync — not after the flush

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:

The full write path. A client write request passes three checks in order: parse catches syntax errors, plan and validate catches wrong table or column names, constraint checks catch duplicate keys, missing foreign keys and NULLs in NOT NULL columns. A failure returns an error to the client and the WAL is never touched. A pass goes to the durable step: WAL append and fsync, then the memtable insert, then the ACK back to the client.
Every check runs before the WAL — only valid writes are ever made durable

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:

Two WAL entries, each with fields seq, op, key, value and checksum. Entry 1, seq 1, SET user:42 with value name alice, is complete and its checksum matches, so it is applied. Entry 2, seq 2, SET user:43, is cut off mid-value after "bo" because the process crashed mid-write; the rest of the entry never reached disk. Its recomputed checksum does not match, so replay stops and this entry and everything after it is discarded. Its fsync never returned, so no ACK was ever sent, so discarding it breaks no promise.
A torn entry fails its checksum, and replay stops right there

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.