Build a Crash-Safe Key-Value Storage Engine in Rust: a Localpane Lab Kit
Build a small storage engine in Rust, one tested step at a time. You write a checksummed write-ahead log, recovery that can tell a crash's torn last write from real corruption, a sorted memtable, SSTables with checksummed blocks and a sparse index, an atomically published manifest, flushes, full compaction and a read-only verifier. Every step has a starter with marked TODOs, tests that fail until your code works, plain explanations and hints. One command checks your work. At the end, crash tests fail every durable step of your engine and check that it reopens with every acknowledged write. The complete solution is included.
What's inside
- WORKBOOK.md: 8 steps, each with the goal, key concepts in plain words, where the starter code is, the tests that must pass, hints and a check command
- 8 starter crates (steps/step-01 to step-08): earlier steps solved, the current one stubbed
- The finished solution: about 2,700 lines of Rust (with doc comments) plus about 3,700 lines of tests
- 108 graded tests, including property tests (random operation sequences against a reference map, hundreds of randomly mutated files), and 9 tests for the code the kit provides
- check.sh to check one step and everything before it
- Two examples: a small kv command line and a crash demo that tears the log, recovers and is refused on corruption
The 8 steps:
- Log records: encode, validate, decode
- Reading a log after a crash
- The durable writer: write, fsync, truncate
- The memtable and the store
- Sorted tables (SSTables)
- The manifest and the flush
- Merging and compaction
- A read-only verifier, proven by crash tests
Who it's for
Rust developers who want to understand what "durable" really means by building it: backend engineers curious about how databases store data, people preparing for systems interviews, and anyone who has read about LSM trees and wants to see one survive a crash.
Not for: anyone looking for a production database or embedded store (use an established one), or for a tour of a large engine's advanced features (no Bloom filters, block cache, leveled compaction, transactions or concurrency beyond one coarse lock).
Prerequisites
- Rust 1.88 or later; tested with Rust 1.100.0 on macOS (Apple Silicon). Linux not yet tested; Windows is not supported (use WSL).
- Comfortable with ownership, Result, iterators, traits and std::fs
- No prior storage-engine knowledge needed
- About 10 to 14 hours
Licence
Single-developer licence: use and modify the code, including in your own products; no redistribution and no public repositories. Free updates within 1.x.
Licence summary: payhip.com/u1791141470733132530294/licences