Skip to content
Select theme

Write a toy database

What you end up with: a key-value store that keeps your data after the process exits, handles deletes without losing them, compacts its files in the background, and answers reads in microseconds — plus benchmarks that compare it honestly against sled and redb.

The end of the finished store’s test suite: forty-six tests, every one green

This is the track for people who want to understand how storage actually works. There is nothing magical in a database: append bytes, keep an index, merge files occasionally. Writing one in Rust is also the best exercise there is for Vec<u8>, &[u8], lifetimes and error handling in a codebase that has to be correct.

  • Append-only data files with an fsync policy you chose deliberately
  • An in-memory index (starting with HashMap, ending with a real key format)
  • An SSTable format: sorted, immutable, with a sparse index
  • Compaction that merges files without losing writes or deletes
  • Bloom filters so missing keys do not touch the disk
  • A write-ahead log and memtable
  • Tombstones, range scans, and a snapshot-free iterator API
  • Benchmarks against sled and redb, including where you lose

toy-database/ in the examples repository — one tag per article, plus the benchmark harness.