Design decisions

Storage engine

Decisions about document layout, hash index, versions, indexes, checkpoints and the disk-resident lsm engine (D2 to D4, D14, D15, D18, D22, D27, D28, D35, D43, D46, D48, D49).

D2 Documents live in a slab; the hash index stores 4-byte slot numbers only#

The index maps a hash to a u32 slot, and keys and values stay in a dense slab. The study behind it found that standard maps cost 81 bytes per entry at one million entries, against 8.75 to 12.1 bytes for slab plus slots. Rejected: a map that holds entries inline, and one allocation per document.

D3 Index structure is a Dashtable-style segmented extendible hash table#

A directory of segments chosen by the top hash bits; 64 buckets of 12 slots per segment, one bucket per cache line, an SSE2 fingerprint compare, insert into the emptier of two buckets, and a full segment splits alone. Stash buckets, segment merge on delete and per-bucket versions were left out for now (versions are not needed because Raft DB uses MVCC and a WAL).

D4 Document format: typed tags, field-name dictionary, masked objects#

Documents are stored in a typed binary form with a dictionary of field names and masked objects (the byte layout is in the internals chapter). The sample document is 0.44 of its JSON size. The first version missed the size target at 0.51 and was fixed with masked objects, 3-byte integers and decimal floats instead of relaxing the target. Rejected: BSON-style repeated names, per-value back-lengths, a fixed column list.

D14 Cache-local bloom filter in front of every key lookup that can miss#

A lookup reads one 64-byte block; 10 bits per key give 0.97 % false positives at 1.25 bytes per key. Callers pass a hash they already computed. Rejected: a classic bloom filter (several cache lines per lookup) and cuckoo or xor filters (no measured need yet).

D15 In-place rows with a 12-byte version header and an undo log#

The multi-version design follows InnoDB: a header of a 6-byte transaction number and a 6-byte undo pointer (its top bit is the delete mark), snapshots from two limits plus a sorted list of running transactions, and purge of undo older than the oldest snapshot. Rejected: PostgreSQL-style full-row versions, which cost more per version and need a vacuum.

D18 Collection = one 8-byte-aligned arena + hash index that stores record offsets#

The index stores offset / 8 as a u32 and compares keys in the arena, so a read costs two memory misses. A separate offset array (three misses) failed the read target. The limit is a 32 GiB arena per collection or shard. The single dependent read is reported but has no target, and the other targets were not changed.

D22 Checkpoint = rewrite the log as the shortest equivalent log, then rename over the old one#

There is no second file format: a checkpoint is an ordinary log (field names, schema, one PUT per live document), so recovery code is unchanged and the rename is atomic. The cost is that writes pause during the rewrite. Rejected for now: a snapshot file plus a log suffix, and incremental checkpoints (those need MVCC).

D27 Secondary index = sorted (key, slot) run + small overlay#

An index entry is 12 bytes. Numbers get order-preserving keys (equality and ranges); everything else gets a type-tagged hash (equality only). A lookup returns a superset and the strict executor re-checks it. Updates go to an overlay merged when it passes 1/16 of the run. Rejected: hash tables of posting lists, a B-tree entry per key, and string prefix keys.

D28 Indexes are definitions in the log, rebuilt at open#

The log stores only index definitions; contents are rebuilt from the data at open and after each compaction, which costs O(N) but needs no second recovery path. A filter is narrowed by the smallest candidate list among its required comparisons, and the full filter then runs on the candidates, so an index can never change an answer. Rejected for now: persisted index runs and cost-based index choice.

D35 Compact logs at start (and on demand) instead of while serving#

A shard log of at least 8 MiB and more than twice its live data is rewritten when the table opens, before any request is served, so restart time follows the data. An operator can force it with an HTTP call that briefly takes the table offline. Compaction while serving waits until reads are versioned.

D43 Disk-resident storage engine: log-structured merge tree#

The in-memory table loads the whole log at restart, so neither recovery time nor capacity can be bounded; rewriting checkpoints would only make that design faster. The raftdb-lsm crate has a memtable with one WAL segment each, immutable checksummed sorted files with a bloom filter per file, leveled compaction and an atomically replaced manifest. A restart reads the manifest and replays at most one memtable of log, and files open lazily. Flush and compaction later moved to background threads with write backpressure, and compaction picks the level by score after a measurement showed level 0 starving deeper levels.

D46 Server tables on raftdb-lsm, opt-in#

The setting engine = lsm makes new tables keep their shards in raftdb-lsm, so data may exceed memory. The default stays memory, and a table keeps the engine it was created with, so changing the setting never strands data. Keys, field names and the live-document count are stored under reserved key prefixes, and the count is committed with the writes it counts.

D48 Secondary indexes on lsm tables#

For a table that may be larger than memory, the index lives in the same engine. An entry is a key made of a marker, the field id, the 8-byte index key and the primary key, with an empty value, so a range of index keys is one range scan. The index key is the same function as in the in-memory engine, so both engines agree on what an index can answer.

D49 The lsm read gap, closed from 2.6x to 1.27x#

Random gets on the lsm engine were slower than on the in-memory engine. Instead of working through a list of suspects, the lookup was timed stage by stage by cutting the sorted-file get short after each step, and the stages that cost the most were fixed. The gap closed from 2.6 times to 1.27 times.