Write-ahead log and checkpoints
How the write-ahead log makes writes durable, how group commit works, what recovery does at start, and how to rewrite a log with a checkpoint.
The log is the table#
On the memory engine each shard owns one log file, shard-N.log. Every change (a put, an insert, a delete, a new field name, a schema, an index definition) is a record appended to it, and the rows in memory are what replaying the log produces. There is no separate data file.
record = [len: u32 LE] [crc: u32 LE] [payload: len bytes]
crc = CRC-32C over the 4 len bytes then the payload
len = 1 .. 16 MiB; len 0 means "end of log"Each record has 8 bytes of overhead. The file is kept zero-filled ahead of the write position (growth step 64 MiB by default), so a zero length ends the log.
Durability and group commit#
- The shard thread applies the write (schema checked, then logged) and parks the reply with its log position.
- The shard's flusher takes all parked replies, makes the log durable up to the highest position with one
fsync, and releases them. Writes that arrive during anfsyncshare the next one: group commit. - A write is acknowledged only after step 2. If the flush fails, the parked requests get an
IOerror and later writes fail until the process is restarted. - With
sync-replicasgreater than zero, step 2 also waits until that many followers confirmed the same position (see Synchronous replication).
On Windows the log is opened write-through and kept zero-filled (D37), so a write needs no separate flush call; this was measured to double the insert rate on the test PC and is Windows-only. Elsewhere File::sync_data is used. What sync_data guarantees depends on the system; the macOS behaviour has not been verified (see Platforms).
Recovery at start#
For every shard the server:
- Reads the log, calls the apply function for each valid record and cuts a torn tail: a last record whose checksum or length does not fit is removed (truncated, then the file is extended with zeros again). Only that record is lost, and it was never acknowledged.
- Rebuilds indexes from the index definitions in the log (index runs are not persisted).
- If the log is large and mostly dead, rewrites it (below).
- Starts serving.
Recovery speed is about 3.2 million records a second in the project's measurements; the log is read fully, so restart time follows the size of the history. The lsm engine reads only the manifest and replays at most one memtable: about 0.01 s whatever the size. Mid-log damage is not reported. The recovery scan (raftdb_wal::scan) stops at the first record whose length or checksum does not verify, and everything after that point is treated as the torn tail and cut. A flipped bit in the middle of a log therefore silently discards every later record of that shard. This follows from the code; the project's tests cover torn tails, not mid-log corruption. A byte-identical copy of the log on a replica or a backup is the protection.
Checkpoints#
A checkpoint rewrites a log as the shortest log that gives the same rows, then renames it over the old one (D22). It is the only way a memory-engine log shrinks.
At start#
A shard log that is at least checkpoint-min-mb (default 8; 0 switches it off) and more than twice the size of its live rows plus 64 KiB is rewritten before the table serves anything. An already compact log is left alone.
On demand#
POST /v1/databases/{db}/tables/{table}/checkpointThe server stops the table, rewrites every shard log, reopens it and answers {"checkpointed": ..., "logBytesBefore": ..., "logBytesAfter": ...}. The table is unavailable meanwhile: connections on it fail and must reconnect. Needs the admin role. The call is in the HTTP administration API. On the lsm engine a checkpoint is a full compaction plus an exact recount.
Effects on replicas#
A checkpoint rewrites the primary's log, so a replica's copy no longer continues it: the replica detects the mismatch (a CRC-32C of its last 4 KiB is sent with its offset) and fetches the table again from the start. Replicas never checkpoint on their own: that would break byte identity.
Not built#
Compaction while serving (it needs versioned reads), a background checkpoint policy, log rotation, and a directory sync after the rename.
The lsm engine's files#
wal-NNNNNNNN.log (one segment per memtable), NNNNNNNN.sst (sorted files with 16 KiB CRC-protected blocks, an index and a bloom filter) and MANIFEST (the live files per level, replaced atomically by temporary file, sync, rename and directory sync). Open deletes files that are not in the manifest (an unfinished flush or compaction, *.tmp).