Internals

raftdb-wal, the write-ahead log

The on-disk record format, group commit, recovery, failure behaviour and measured speed of the write-ahead log.

raftdb-wal makes records durable in groups. It depends only on the Rust standard library. The design follows the PostgreSQL WAL study.

On-disk format#

file   = record* then zeros (the file is zero-filled ahead of the write position)
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

Eight bytes of overhead per record. An Lsn is the byte offset just after a record.

API#

CallBehaviour
Wal::open(path, Options, on_record)Reads the file, calls on_record(end_lsn, payload) for each valid record, cuts any torn tail (truncate, then re-extend with zeros) and continues appending after the last valid record.
append(&payload) -> LsnCopies the record into the buffer. The checksum is computed outside the lock. Not durable yet.
commit(lsn)Returns when everything up to lsn is written and flushed.
append_commit(&payload)Both.
durable_lsn(), end_lsn()Last durable position; position after the last appended record.
scan(bytes, f)The recovery walk as a function over a byte slice.
encode_record(payload, out)The exact record form append writes (used to build checkpoint files).

Options { chunk, sync }: the file growth step (default 64 MiB), and sync: false only to measure the cost of write().

Group commit#

commit tries to become the leader with a compare-and-swap on leading. The leader swaps the buffer with a spare (one short lock), writes it at the current offset, calls sync_data, publishes durable, clears the flag and notifies waiters, the last two under the wake lock. A waiter checks durable and leading while holding wake before it sleeps, so it cannot miss the wake-up; a waiter that finds no leader tries to lead. Every record appended before the leader's swap is covered by that one flush.

Failure behaviour#

If a write or a flush fails, failed is set, every later append and commit returns an error, and waiting committers are released with that error. The process should stop and recover from the log, as PostgreSQL does.

Checksum#

CRC-32C. The portable version uses slicing-by-8 with tables built at compile time: 2.2 GB/s. On x86_64 with SSE4.2 (detected at run time) the hardware instruction gives 10.3 GB/s. A test compares the portable, the hardware and a one-byte version for every length 0..300 at 9 alignments. The feature portable forces the portable version. Recovery runs at 1.5-1.6 GB/s end to end.

The storage trait#

Wal<S: Storage = File>: the log's bytes live behind a Storage trait (write_at, set_len, sync_data, sync_all), implemented for File with the standard Seek and Write only. Wal::open_storage opens any storage, so raftdb-sim can run the real log on a simulated disk that loses and tears unsynced writes. The trait is monomorphised, so it has no run-time cost; the db suite measured 3.68 M durable inserts/s after the change against 3.43 M before, within noise.

Write-through files on Windows#

On Windows (unless the portable feature is on) the file is opened with FILE_FLAG_WRITE_THROUGH, kept physically zero-filled up to its length, and the per-flush sync_data is skipped because each write is durable when it returns. The record format and recovery are unchanged, and logs written in one mode are read and extended in the other. Measured on the development NVMe: 0.06 ms per 4-20 KB durable append against 0.43 ms for write plus flush (decision D37).

Measured#

From the benchmark results log: 305,903 durable records per second with a p99 acknowledgement of 1.06 ms in the thread-per-core model; 1.49 GB/s recovery; 6.3 M records/s without flushing.

Limits#

Single file, whole-file recovery in memory, no directory sync after creating a file, no rotation. These are listed as open work.