KatInterview

Building a Crash-Safe Storage Engine for a Single-Node Key-Value Service

System DesignModel answer
MediumModel answerSystem Designstorage-enginelsm-treewrite-ahead-log

Last updated

Design the storage engine of a single-node persistent key-value store covering indexing, large values, thread-safe access and cheap durability for acknowledged writes.

You are asked to design the storage layer of a single-machine key-value service that keeps its data on disk and serves many client threads at once.

Interface

  • get(key) returns the value stored under key, or a not-found result.
  • put(key, value) stores value under key and replaces any earlier value for that key.

Requirements

  • Durability: once put has returned success, the write must still be there after a crash or power loss.
  • Throughput: the store must serve a large volume of reads and writes.
  • Larger than RAM: lookups must stay efficient even when the full dataset is far bigger than memory.

Assumptions

  • Keys are strings. Values are arbitrary binary blobs and some can be very large.
  • The hot subset of keys fits in memory, while the complete dataset lives on disk.
  • Many threads call the store at the same time.

What to cover

Describe the storage engine and explain your reasoning on these four points:

  1. How you index keys so that a lookup needs as few disk seeks as possible.
  2. How you deal with very large values without putting heavy pressure on memory.
  3. How you let threads share the store safely while keeping throughput high.
  4. How you guarantee durability without paying for a full disk sync on every single write.

State your assumptions about scale (dataset size, RAM, request rates, latency targets) up front and discuss the trade-offs of your choices.

Hints

Hint 1

Start by putting numbers on the problem (dataset size, RAM, key count, request rates). Check whether a full in-memory index of every key would even fit, because the answer decides between a dense and a sparse index.

Hint 2

Think about which on-disk data can be made immutable. Immutable sorted files plus a small mutable in-memory structure make both concurrency and crash recovery much simpler.

Hint 3

Separate large values from the key index so that background reorganisation of the index never has to copy huge blobs.

Hint 4

An fsync is expensive but its cost does not depend much on how many bytes it covers. Ask how many client writes can share a single one, and what ordering of file syncs makes recovery correct.