An embedded key-value store in C++17, built to learn how storage engines really work. It's the kind of thing that sits underneath LevelDB and Cassandra.
Disks are bad at random writes. Update something in the middle of an on-disk tree and you pay for a seek and a whole page rewrite, even for a 100-byte value. An LSM tree just never writes into the middle of anything.
A Put appends one record to a write-ahead log, which is sequential and about the fastest thing a disk can do, adds it to an in-memory sorted skip list, and returns. When that memtable fills up, it's written out as one immutable file (an SSTable) and the engine starts a fresh one. When files pile up and reads slow down, a background thread merges them into fewer, bigger ones.
That's the trade: writes get fast, reads get a bit slower, and compaction is what you pay to keep it that way.
Every key carries an 8-byte trailer
Each key ends in (sequence << 8 | type). Keys sort by user key, then by sequence number from newest to oldest, so a scan always meets the newest version of a key first. Point lookups, snapshot reads and compaction's cleanup of old versions all come from that one ordering.
A delete doesn't delete
It writes a tombstone, because the real value might still be sitting in an older file. Only compaction can throw both away.
I benchmarked it against LevelDB, the standard implementation of this design, on the same YCSB-style workloads with matching settings, back to back on an idle machine. Mine verifies checksums and LevelDB's default doesn't, so this is the harder setup for me.
This engine 432,065 · LevelDB 499,547
This engine 464,655 · LevelDB 222,640
This engine 862,548 · LevelDB 710,916
This engine 1,149,186 · LevelDB 1,210,596
This engine 1,157,849 · LevelDB 1,294,580
This engine 150,267 · LevelDB 241,630
This engine 392,766 · LevelDB 243,388
| Workload | This engine | LevelDB | Ratio |
|---|---|---|---|
| Load, 100% insert | 432,065 | 499,547 | 0.87x |
| A, 50% read and 50% update | 464,655 | 222,640 | 2.09x |
| B, 95% read | 862,548 | 710,916 | 1.21x |
| C, 100% read | 1,149,186 | 1,210,596 | 0.95x |
| D, read latest | 1,157,849 | 1,294,580 | 0.89x |
| E, scan | 150,267 | 241,630 | 0.62x |
| F, read-modify-write | 392,766 | 243,388 | 1.61x |
Operations per second, 500,000 keys, 100-byte values. One caveat on workload A: LevelDB's number swings from run to run (222K to 285K in mine) and this table caught a low one. Against its best run the ratio is closer to 1.6x than 2.1x. Scans are the one real gap.
This is the part I'm proudest of. A harness starts a writer, kills it with SIGKILL at a random moment, restarts it, and checks two things: every write the engine said OK to can be read back, and the recovered keys form an unbroken run with no holes. It does a few hundred rounds across all three fsync policies, and it also tears log records on purpose.
The last run passed 300 of 300 rounds, and 298 of them were killed mid-operation. There are also 177 unit and integration tests, clean under AddressSanitizer and ThreadSanitizer and run in CI.
A durability bug the harness found
Compaction wrote the manifest naming the active log. If a memtable rotation happened while compaction had its lock released, cleanup deleted a log that still held acknowledged writes. 34 of 300 rounds lost exactly one memtable's worth of data. The fix was to name the oldest log that recovery still needs.
Reads were slow, and I guessed the cause wrong twice
Point-read p95 sat at a flat 11 µs. I blamed allocation, then lock contention, fixed both, and nothing moved. It was checksum verification: a byte-at-a-time CRC over every 4 KiB block. Switching to the CPU's CRC32C instruction took reads from 264K to 1.2M operations a second, with verification still on.
Scans ran at 0.07x of LevelDB, and the iterator wasn't the problem
I counted the dead entries a scan stepped over for each live row and got 41. The engine was hoarding old versions: compaction kept only the newest version of each key, but flushing a memtable wrote out every version, and a 4 MiB memtable under skewed updates holds thousands of versions of its hottest keys. Applying compaction's rule to the flush took scans from 19K to 151K operations a second.
What I took from all three: measure the exact thing instead of reasoning about what's probably wrong.