Internals

raftdb-db, the durable table

How a table joins the schema, the in-memory store and the log; the write order, record types, checkpoints and the measured latency target that was not met.

raftdb-db joins raftdb-schema, raftdb-store and raftdb-wal into a Table that survives a restart. It has no unsafe code. A Table belongs to one shard thread (single writer, decision D12).

Write order#

  1. Check: the schema (if set) and, for insert, that the key is absent. A rejected write logs nothing.
  2. Log: one record is appended to the log buffer (not yet on disk).
  3. Apply: the in-memory collection is changed.
  4. Commit: Table::commit(lsn) blocks until the record is on disk. A client is acknowledged only after its write is durable.

The record payloads (length and CRC come from the log):

TypeLayout
PUT (1)[1][key_len varint][key][document]
DELETE (2)[2][key_len varint][key]
DICT (3)[3][field name UTF-8] (the id is the position among DICT records)
SCHEMA (4)[4][Schema::encode bytes]
INDEX (5)[5][kind][field name], kind 0 ordered, 1 hash

encode_put, encode_delete, encode_dict and decode_record expose these bytes. The in-memory table and the server's LSM shards use the same bytes, which is what lets servers with different engines follow each other's logs.

Pipelining#

A blocking commit makes the caller wait for a whole flush (about 0.43 ms), so one thread using it alone is capped near 140 k inserts per second. The server design is different: the shard thread keeps writing and a flusher thread runs Wal::commit(latest). While one flush runs, more writes pile up and the next flush covers all of them. A client is acknowledged when durable_lsn() passes its write. That is why Table::wal() exists.

Checkpoint#

Table::checkpoint(self) writes <log>.new holding the field names in id order, the schema and one PUT per live document, calls sync_all, closes the old log, renames .new over the log and opens the new log. Restart time then depends on live data, not on history.

  • A crash before the rename leaves the old log as the truth (open deletes a leftover .new). A crash after it leaves the new log as the truth.
  • It consumes the table, because Windows cannot replace an open file. If a flusher still holds Table::wal(), checkpoint flushes and returns an error before touching any file.
  • Writes pause for the duration. Not done: incremental checkpoints without a pause, a directory sync after the rename, log rotation.
  • checkpoint_if_wasteful(min_bytes) checkpoints only when the log is at least min_bytes and more than twice live_bytes() + 64 KiB. The server calls it at start.

Secondary indexes#

create_index(field, ordered) is logged as an INDEX record, built from the data and kept in step by every write. Definitions are in the log; the entries are rebuilt when the table opens. See Secondary indexes.

Measured#

raftdb-bench db, 1 M documents of about 111 bytes, Windows 11, NVMe:

MetricResultTarget
Durable inserts, blocking commit per 64134 k/sno target (shows the problem)
Durable inserts, shard thread plus flusher thread3.43 M/sat least 300 k/s
Restart: replay the log into memory3.23 M records/s (130 MB in 0.31 s)at least 2 M/s
Checkpoint speed3.79 M live documents/sat least 1 M/s
Log size after / before a checkpoint0.50at most 0.6

A target that was not met#

The open-loop acknowledgement test (windows of 64 inserts arriving at 300 k/s) gave p50 0.70 ms but p99 of 6.4-14.4 ms over three runs, against a target of 2 ms. In steady state the worst acknowledgement in a 100 ms slice was 1.0-1.3 ms; every 0.5-1 s a stall of 5-29 ms delayed one flush and built a backlog that drained in a few milliseconds. Log growth step, thread priority and file preparation were ruled out; the cause on that desktop PC was not identified. The status is "not met on the dev PC; threshold not changed", to be re-run on a Linux reference machine. The earlier raftdb-wal benchmark measured a p99 of 1.05 ms, so the write path itself can meet the target.

Open#

The two-log design if the Linux run still fails; multi-version rows and transactions across keys in the engine (see MVCC).