case study · rust · storage engines · databases
Storage Engine — where a key-value store actually keeps its data
A persistent LSM-tree key-value store built from first principles in Rust. It is the core design behind LevelDB, RocksDB, and Cassandra — rebuilt by hand to understand how a database turns slow random writes into fast sequential ones without losing data when the process dies.
The problem
A disk is happy writing in long sequential streams and slow when it has to jump between random locations. A B-tree keeps data sorted by updating pages in place, which is great for reads but means every write is a random write. The LSM-tree makes the opposite trade: it buffers writes in memory, appends them to a log, and only ever writes to disk in large sorted batches. Reads pay a little more; writes get dramatically faster. I built the whole machine by hand, in Rust, with almost no external crates, to understand exactly where that trade-off lives.
What I built
Engineering decisions
What it taught me
The pieces of an LSM-tree only make sense together. The memtable exists because writes must be fast; the write-ahead log exists because the memtable is volatile; SSTables exist because memory is finite; bloom filters exist because SSTables accumulate; and compaction exists because SSTables would otherwise pile up forever. Each component answers a problem created by the one before it.
Building it in Rust also made the correctness bar concrete. Tombstones have to propagate through every level so a deleted key stays deleted after compaction; a snapshot has to keep seeing old data even as compaction rewrites the files underneath it. The repository leans on 269 tests and a Criterion benchmark suite across seven workloads — including crash-recovery time — because in a storage engine, “probably correct” is the same as broken.