Internals

The LSM engine (raftdb-lsm)

The disk-resident storage engine: files, memtable and flush, leveled compaction, bounded recovery, write backpressure, block cache and the measured restart and latency figures.

raftdb-lsm is the disk-resident storage engine (decision D43). The data lives in sorted files on disk; memory holds the memtable and the indexes of the files in use. It exists because the in-memory table (raftdb-db) keeps the whole table in memory and replays the whole log at restart, so neither restart time nor capacity can be bounded. It uses only the standard library plus raftdb-wal, raftdb-bloom and the hash of raftdb-store. The server uses it when a table is created with engine = lsm (raftdb-server).

Files in a shard's directory#

FileContent
wal-NNNNNNNN.logone raftdb-wal log segment per memtable (record = kind, key, value)
NNNNNNNN.ssta sorted file: data blocks (about 16 KiB, each with a CRC-32C), an index block, a bloom block and a 56-byte footer (layout in src/sst.rs)
MANIFESTthe live files per level, the oldest log segment still needed, the next file number and a CRC-32C; replaced atomically (temporary file, sync, rename, directory sync)

Writes#

put and delete append to the log and change the memtable; commit makes them durable by group commit. When the memtable reaches memtable_bytes it is handed to a flush thread and the writer continues at once in a new memtable and a new log segment. The flush thread writes a level-0 file, replaces the manifest and deletes the old segments. A separate compaction thread merges levels. Readers take a consistent view (the memtables and an immutable file list) under a short lock and read without it; a replaced file is deleted only when the last reader that can see it is done.

Leveled compaction#

The level with the highest score goes first (level 0: files divided by l0_trigger; other levels: bytes divided by their limit). Level 0 holds overlapping files and is merged into level 1 when it has l0_trigger files. A level n of 1 or more holds non-overlapping files up to level1_bytes * level_ratio^(n-1), and a file that makes it too big is merged with the overlapping files of level n+1. Deletion markers are dropped only when no deeper level has a file overlapping the compaction's whole key range. (A test found a real bug here: markers were dropped using only the upper input's key range, which could bring back an older value from a deeper level.) Picking by score fixed an earlier flaw where "level 0 first" starved the deeper levels.

Reads#

Memtable, then level-0 files newest first (range check and bloom filter), then one file per level by binary search. scan(start, end, limit) merges all sources in key order, the newest version wins and deleted keys are skipped. Point lookups use a block cache (block_cache_bytes, default 64 MiB, 0 = off; 16 locked shards, first-in first-out eviction). A cached block gets a hash table from key to entry once a point lookup has used it, built from the block's bytes and never stored on disk. Scans read blocks without building it or filling the cache.

Measured on test server A (2 vCPU, 15 GB RAM, ext4 on network block storage): random gets over flushed data went from 47.7 k/s to 543 k/s with the cache (D46); one thread on 130-byte rows with all data in sorted files measured 822 ns per present key and about 280 ns per absent key (D49).

Bounded recovery#

Open reads the manifest, deletes files that are not in it (unfinished flushes or compactions, *.tmp), replays only the log segments newer than the last flush (at most one memtable) and does not read the sorted files: their footer, index and bloom filter load on first use. Damaged blocks, indexes, footers and manifests are errors (checksums), and a torn log tail loses only the torn record.

Measured on test server A, restart after filling the memtable to about 50 MB of log (the longest replay), 100-byte values, 64 MiB memtable:

KeysDataRestartWrite amplification
1 M0.13 GB0.373 s0.6
4 M0.5 GB0.357 s2.0
16 M2.1 GB0.365 s4.6
32 M4.2 GB0.373 s4.5

Restart time is flat because the cost is replaying one memtable of log. These are 4 GB, not terabytes; the claim tested is that restart does not depend on the data size.

Backpressure and background work#

If compaction falls behind, writes are slowed by 1 ms each while level 0 has l0_slowdown (8) files and wait while it has l0_stop (12) files or while the previous memtable is still being written. Stats counts slowdowns, stalls and stall time. A background failure is returned by every later write. Measured on test server A with 32 M keys (4.2 GB), 32 windows of 1 M random keys: put latency p50 1 us, p99 5-7 us, p99.9 13-80 us; the worst single put 8.6 ms; 0 stalls; throughput 596 k keys/s in the first window and 417-511 k/s in most others. The "write rate stays flat as data grows" goal is met to 4 GB on 2 cores and still has to be shown at hundreds of GB.

RateLimiter lets the owner limit the bytes per second that compaction writes; it never delays flushes or foreground writes. The watcher sets it (Watcher).

Limits#

  • One compaction thread, so a machine with few cores can fall behind a fast writer; the throttle then slows writes.
  • No compression of values, no prefix compression in blocks, no true LRU for the cache.
  • The manifest is rewritten whole at every flush or compaction (fine for thousands of files; millions need an edit log).
  • The index and bloom filter of an open file stay in memory (about 40 bytes per 16 KiB block plus 1.25 bytes per key).
  • Single owner thread; no snapshots or transactions across keys (MVCC).
  • Keys up to 64 KiB, values up to 8 MiB.