Crash it in your browser → The engine is compiled to WebAssembly and writes a real log and real SSTables into an in-memory filesystem. Write until the memtable flushes, watch compaction merge files down the levels, trace a read and see which files its Bloom filters ruled out, decode any file, then kill the database without a clean shutdown and watch the log replay.
An embedded log-structured merge-tree key-value store written in C++17, in the shape of
LevelDB and RocksDB. Around it: a Python/FastAPI REST layer over the C++ engine via
ctypes FFI, and a React/TypeScript dashboard for live telemetry.
Written to understand why storage engines are built this way: why writes go to a log before they go anywhere useful, why on-disk files are immutable, and why deleting a key means writing more data rather than less.
| Layer | Technology |
|---|---|
| Storage Engine | C++17: skip list memtable, write-ahead log, levelled SSTables with Bloom filters |
| API | Python/FastAPI: ctypes FFI over libnexus.so |
| Frontend | React/Vite/TypeScript: Tailwind CSS, Lucide Icons |
Requires: g++, Python 3.8+, Node.js/npm.
Terminal 1: build the engine. Produces both the nexus_db benchmark binary and
libnexus.so for the API to load:
make clean && make
./nexus_db # optional: 1M-write benchmark, see Benchmarks belowTerminal 2: start the API:
cd backend
pip install -r requirements.txt
uvicorn api:app --reload
# Listening on http://localhost:8000Terminal 3: launch the dashboard:
cd frontend
npm install
npm run devWhy an LSM tree at all. A B-tree updates records in place, which turns a write into a random seek. An LSM tree only ever appends: writes go to memory and are later flushed to disk in one sequential pass, trading read complexity for write throughput. That trade is the whole design, and everything below follows from it.
MemTable: Writes land in a probabilistic skip list in RAM: expected O(log n) insertion and point lookup, and it stays sorted without any of the rebalancing a tree needs. Sorted order is what makes the eventual flush a single sequential write.
Write-Ahead Log: Every write is appended to wal_N.log before it touches the
MemTable, so the log records the intent before memory records the effect. Each record is
framed as [crc][length][record] and goes down in one write call. By default that
leaves it in the kernel's page cache, which survives the process crashing but not a power
cut. With Options::sync_writes the append is followed by fdatasync and put returns
only once the device has it. Logs are numbered, one per memtable, and a log is deleted
only once the file holding its records is synced and named by a synced manifest.
Recovery: Opening a database replays every log it finds, oldest first, into a fresh MemTable, so a crash between flushes loses nothing that reached one. A crash in the middle of an append leaves a partial record at the end of the file; replay stops at the first record that is short or fails its checksum and truncates the rest, because a torn tail left in place would sit in front of every record appended after it, where the next replay could never reach them. Lengths are bounded by the bytes actually left in the file, so a corrupt length is never allocated.
SSTables: A full MemTable is handed to a worker thread, which writes it to an
immutable data_N.sst in one sequential pass while the writer carries on against a fresh
one. Each file carries what a reader needs to avoid it:
magic "NXSSTBL", version, count, tombstones, offsets, crc 60 byte header
records [flags][klen][key][vlen][value], sorted by key
index every 16th record, plus the first and last: [key][offset][crc of its block]
bloom 10 bits per key, 7 probes
crc over the index and the filter
Checksums: Everything on disk carries a CRC-32C: each log record, each block of an
SSTable (the records between one index entry and the next), its header, its index and
filter, and the manifest. A read checks the one block it needs before believing a byte of
it. A block that fails is an error, Corruption, and never a miss: a miss would send the
search on into older files, which would answer with a value that was overwritten or
deleted long ago. A damaged file or manifest stops the open, and a compaction that meets
a damaged input stops and leaves every file where it was, because a merge deletes its
inputs once it has written what it read.
Levels: Level 0 holds whatever a flush produced, so its files overlap in key range and a read has to consult all of them, newest first. Every level below is disjoint and sorted, so at most one file per level can hold a given key, found by binary search over key ranges. A level is compacted when it passes its byte budget, ten times the level above; level 0 when it reaches four files.
Compaction: A merge takes its inputs plus every overlapping file one level down, keeps the newest version of each key and writes the result one level down. This is what reclaims the space an overwrite or a delete costs. A tombstone is only dropped once nothing below can still hold the value it covers, and a level thick with tombstones earns a compaction on its own, since otherwise a delete can sit above its value indefinitely while every level is within budget.
Manifest: MANIFEST names every live file and its level. Each update writes
MANIFEST.tmp, syncs it, renames it and syncs the directory, so it lands whole or not at
all, power cut included. A crash during a compaction
leaves outputs the manifest does not name and inputs that it does; the next open removes
the first and reads the second. Files are retired by reference count rather than unlinked,
so a compaction cannot delete a file a read is about to open.
Reads: MemTable first, then the immutable MemTable waiting to be flushed, then the levels, so a newer value always shadows an older one. A file whose key range or Bloom filter rules the key out is never opened. The lock is held only long enough to look at the memtables and take a reference to the files to search; opening and scanning them happens with no lock held, so reads scale with threads rather than queueing behind each other.
Scans: scan(start, end, limit) returns the live keys in a range, in order. Every
source is already sorted, so a scan is a merge: the memtables, each level 0 file, and each
deeper level read as one run, since its files are disjoint. The sources are ordered newest
first and the newest holder of a key wins, which is the rule a point read follows by
stopping at the first file that answers. A tombstone that wins is skipped along with the
older versions under it. What the scan will read is pinned under one hold of the lock, the
memtables' part of the range copied and the files referenced, so the result is a single
moment of the database even while a flush moves records from one to the other. Compaction
is the same merge with its output written to files, so it streams too: one block per input
in memory, however large the inputs are.
Tombstones: A delete writes a record with the tombstone flag set rather than removing anything. The old value is still sitting in a file further down; the flag is what makes the search stop and report "not found". This is why a delete in an LSM tree adds data, and why the space comes back only when compaction reaches it.
Where this departs from a real storage engine:
- A scan copies its part of the memtable. That is what lets it read files without
holding the lock, and it is bounded by the scan's limit, but a real engine would pin a
sequence number and walk a lock-free skip list in place. There are no snapshots a caller
can hold open across calls either: each
scanis its own moment. - No group commit.
sync_writespays onefdatasyncperput, about 0.6 ms here, under the exclusive lock. A real engine batches the writers that arrive during one sync into the next, so a hundred of them share one trip to the device. - Damage is detected, not repaired. A file that fails its checksum stops the open or the read that touched it. There is no tool to salvage the blocks that are still good.
- Writes serialise. Readers no longer wait for each other or for a read that has gone
to disk, but every
putstill takes the exclusive lock, and one worker does all the flushing and compacting. - No block cache. Every read opens the file and reads its block through the page cache, then checksums it again.
make testtests/test_db.cpp builds with AddressSanitizer and UndefinedBehaviorSanitizer and covers
overwrites, tombstones shadowing older files, re-adopting files on reopen, the Bloom filter
itself, and the recovery paths. The crash tests fork a child that writes and then
_exit()s, skipping every destructor, so the parent can only read back what the log
preserved: unflushed puts and deletes, a crash after several flushes, a torn final record,
writes appended after a torn tail, and a log that starts with a garbage length.
The scan tests check a range against keys spread over the memtable and several levels with
overwrites and deletes on top, then against a std::map given the same 30,000 random
writes and deletes while compactions run, before and after a reopen. A third runs three
scanning threads against a writer: every key exists throughout, so a page of 50 from any
key must be exactly the next 50 keys, and one missing or repeated would mean a scan saw a
flush half done.
The corruption tests flip one byte in a file and check what happens next: a read that lands in the damaged block is refused while the block beside it still reads, a damaged index or manifest stops the open with every file left on disk, a log record with a flipped value is not replayed, and a compaction that meets a damaged input stops without deleting anything.
The rest are about the tree: a value that happens to equal the old @@TOMBSTONE@@ marker
is still a value, compaction merges flushes into fewer files and moves them off level 0,
rewriting every key five times does not keep six copies on disk, deleting every key
releases the space, a file the manifest does not name is removed at open while the ones it
names are untouched, and four readers stay correct against a tree that a writer and the
compactor are changing underneath them. That last one also passes under ThreadSanitizer
with no reports:
g++ -std=c++17 -O1 -g -fsanitize=thread -I./include tests/test_db.cpp src/db.cpp -o test_tsan -pthread
./test_tsanweb/db_web.cpp wraps NexusDB for JavaScript and web/build.sh compiles it with the
unmodified engine using Emscripten, backed by its in-memory filesystem. GitHub Actions
runs make test, builds the demo, and publishes it to Pages on every push to main.
web/build.sh # needs em++ on PATH
python3 -m http.server -d web/dist # then open http://localhost:8000The FastAPI and React dashboard in backend/ and frontend/ is the local way to drive the
engine; the web demo replaces the HTTP hop with direct WebAssembly calls.
./nexus_db [num_writes] [reads_per_kind] starts from an empty directory, writes
sequential keys with ~45 byte JSON values, waits for the worker to settle, then times point
reads and scans. Numbers below are 1,000,000 writes and 200 reads per kind, Ryzen 9 5900XT, g++
-O3, data directory on tmpfs:
| 1M writes | 1.10 s, ~906k ops/s, then 31 ms to settle |
| tree | 30 SSTables over 3 levels, 63.2 MB |
| bytes written | 63.2 MB flushed + 146.1 MB compacted for 51.8 MB of keys and values, 4.0x |
| read, key still in the memtable | p50 0.5 µs, p99 2.5 µs |
| read, random existing key | p50 6.5 µs, p99 10.9 µs |
| read, oldest key | p50 4.8 µs |
| read, missing key, sorts outside every file | p50 0.1 µs |
| read, missing key, sorts inside the data | p50 0.3 µs, p99 0.7 µs |
| scan, 100 keys from a random key | p50 38.8 µs, p99 57.7 µs |
| scan, every key | 226 ms, 4.4M keys/s |
Reads scale with threads, because a read holds the lock only while it looks at the memtables and takes references to the files it will search:
| threads | 1 | 2 | 4 | 8 |
|---|---|---|---|---|
| random existing keys | 177k/s | 376k/s | 726k/s | 1356k/s |
Every number here is from this machine, same workload, measured against the commit before the change.
Bloom filters and the sparse index. Before them a lookup opened every file and scanned it from the first record. 200,000 writes into 9 files, mean latency:
| case | before | after |
|---|---|---|
| random existing key | 19035.7 µs | 11.0 µs |
| oldest key | 39.7 µs | 5.0 µs |
| missing, outside range | 39.6 µs | 0.1 µs |
| missing, inside range | 33217.8 µs | 0.7 µs |
Compaction. 200,000 keys written five times over:
| before | after | |
|---|---|---|
| files | 55 | 15 |
| on disk | 69.2 MB | 25.5 MB, for 12.0 MB of live data |
| random read | 9.9 µs | 11.4 µs |
Writes pay for that: 727k ops/s before, 275k ops/s after on the same 200,000 sequential writes, because compaction rewrites data the flush already wrote. That is the trade an LSM tree makes, and the benchmark prints the amplification so it stays visible.
The shared lock. Reads used to hold one mutex for the whole read, files included:
| threads | 1 | 2 | 4 | 8 |
|---|---|---|---|---|
| before | 97k/s | 92k/s | 89k/s | 86k/s |
| after | 93k/s | 180k/s | 364k/s | 682k/s |
Checksums and fsync. Adding a checksum to every block made reads faster, which is not what a checksum is for. A block is now read with one call and parsed from memory, where the old reader pulled each length and key through a file stream one field at a time, and that saving is larger than the cost of the CRC. Compaction reads the same way, so the worker keeps up with the writer for longer. 1,000,000 writes on tmpfs:
| before | after | |
|---|---|---|
| writes | 305k ops/s | 906k ops/s |
| random existing key, mean | 13.5 µs | 6.8 µs |
| reads, 1 thread | 87k/s | 177k/s |
| reads, 8 threads | 673k/s | 1356k/s |
tmpfs has no device to wait for, so that is the CPU side only. On an NVMe drive (btrfs),
where every SSTable and manifest is now synced before the log that covered it is deleted,
200,000 writes run at 323k ops/s before and 320k ops/s after: the syncs spend what the
parsing saved. With sync_writes on, each put waits for its own fdatasync: p50
619 µs, p99 about 950 µs, roughly 1,600 writes a second. That gap is what group commit
exists to close.
Streaming compaction. A merge used to read each of its inputs whole before writing anything. It now pulls them through the same merging iterator a scan uses. 1,000,000 random-order writes of 200 byte values, 205 compactions, peak memory of the process:
| before | after | |
|---|---|---|
| peak RSS | 45.6 MB | 14.1 MB |