Skip to content

7 chapters · 88 minutes

The workshop

From a JSON file that loses everything to a log that survives a crash at any byte. Work through them in order; each ends with a way to check it worked.

  1. 01The one-line databaseEveryone's first database is a dictionary saved as JSON. Pull its plug and watch what is left.10 min
  2. 02Append, don't overwriteThe big idea behind most databases: never change what is already on disk. Only add to the end.12 min
  3. 03An index in memoryA log is great for writing and terrible for reading. Keep a map from each key to where its value lives, and reads become one seek.12 min
  4. 04Pull the plugA crash leaves half a record at the end of the log. Find it, cut it off, and carry on. Then meet a cosmic ray, which is a different problem.15 min
  5. 05What "saved" meanswrite() returned. Is your data safe? It depends where the bytes are: in your process, in the operating system, or on the disk. Kill a real process thousands of times to find out.15 min
  6. 06CompactionAn append-only log only grows. Squirrels forget where old nuts are buried; databases clean up. Rewrite the log with only what is still alive, and swap it in safely.12 min
  7. 07The trade-offs, and real databasesStash is fast, simple and crash-safe. It also keeps every key in memory and cannot answer "all keys between A and B". Meet the designs that make different trades.12 min