Skip to content

Key Value Store

You'll build a database that survives kill -9, with a write-ahead log, crash recovery, compaction and a benchmark you can defend.

About

Every database is a key value store with opinions, so build the store. Set, get and delete over a socket, with data that survives the process dying halfway through a write. The ideas are old and public: an append-only log, an index in memory, checksums and compaction. Writing them yourself in C or Rust takes a season, because you have to reason about what's on disk at every moment, not just what's in memory.

At a glance

Per requirement
50 XP
Completion bonus
600 XP
Requirements
6
Takes
A season
Skill level
Advanced (years of practice)
Cost
Free
Supplies
You need some
Where
At home
Other people
On your own
Locked for now
Earn two more Rare badges in Systems Programming to open this one. Log in to see how far along you are.

Requirements

  1. 1
    Build an in-memory store with set, get and delete behind a plain text protocol on a localhost socket. Reuse your poll loop from the Echo Server badge and your hash table from Leak Free. SET name value, GET name and DEL name over TCP, bound to 127.0.0.1 only, because an unauthenticated store must never sit on the open network. Test it with netcat from another terminal. Film a short clip of a session from a second window. Video
  2. 2
    Append every write to a log with write and fsync before you acknowledge it, and rebuild the index from the log on startup. Write the entry, call fsync, then reply OK, in that order. Replay the file on start. Kill the process with kill -9, restart it, and show the data is still there. Film a short clip of the kill and the restart. Video
  3. 3
    Make it survive a crash mid-write with a length and a CRC32 on every entry. Prefix each entry with its length and checksum. Truncate the file mid-entry with the truncate command or a hex editor. On restart it should drop the torn tail and keep everything before it. Take a screenshot of the recovery message and the count of entries kept. Photo
  4. 4
    Add compaction so the log doesn't grow forever, without losing a write if you crash during it. Write a snapshot to a new file, fsync it, then rename it over the old one, because rename is atomic on a POSIX filesystem and a half-written snapshot is never seen. Then fsync the directory as well, or the rename itself can vanish in a power cut. Do a million sets on one key and show the file stays small. Take a screenshot of the file size before and after. Photo
  5. 5
    Benchmark 100,000 writes and 100,000 reads, with and without fsync, and explain the gap. Measure operations per second for each. Without fsync it's fast and a power cut loses data; with it, writes drop by roughly a hundred times on a laptop disk. Write a paragraph on what you'd trade for speed and when you wouldn't. Take a screenshot of the table and the paragraph. Photo
  6. 6
    Kill the server 50 times at random under write load and show nothing acknowledged was lost. Write a harness that writes, kills with -9, restarts and checks every acknowledged key. You want zero missing. If any are missing, that's the real bug, and finding it is most of this badge. Take a screenshot of the harness passing 50 rounds. Photo