Design a distributed key-value store
Design a distributed key-value store. Get and put by key, data larger than one machine, survives machines failing, and configurable durability. You are building the database, not choosing one.
About this problem
“Design a distributed key-value store” is a hard-level problem in the building the storage itself family. You are not using a database. You are being asked to design one.
The key challenge: The slack here is not in the request path at all: a caller is waiting on every get and every put. All the slack lives in the background work, and the design succeeds by moving as much as possible into it.
Time budgets: Reads: milliseconds. Writes: milliseconds. Repair and rebalancing: hours.
The worked solution for Key-value store goes through why those budgets shape the design, stage by stage.
Before you start
You will work this in five timed stages. Here is what each one asks you to think about. None of it is the answer; the answers unlock in the workspace below once you have written your own.
- Stage 1 · Requirements · 5 min
- Which operations? Get and put is a very different system from range scans.
- What is the durability promise? That is the biggest decision here.
- Is this one region or several?
- Stage 2 · Actors and lifecycle · 6 min
- What has to happen on disk before you can say "committed"?
- What does the system decide without being asked?
- A node comes back after being away for an hour. What state is it in?
- Stage 3 · Numbers · 6 min
- How many machines does 300 TB need, with replication?
- What does waiting for a majority add to a write, in milliseconds?
- How long does it take to rebuild one failed node?
- Stage 4 · High-level design · 12 min
- How does a client find the node holding a key?
- What does one node look like inside — what is on disk, and in what shape?
- Where does agreement about membership live?
- Stage 5 · Deep dive and tradeoffs · 14 min
- Two clients write the same key at once. Who wins, and how do you know?
- A node fails. Walk through the next 18 hours.
- What does adding 50 machines to a live cluster involve?
Work it through
Five stages, gated. You write your answer for each stage before its model answer unlocks. That is the core of the method.
Done all five? The diagrams, numbers, trade-offs and follow-ups for this problem are on the worked solution page, to check your own design against.
How it is really built
Write your own answer first. Then read how teams who run this at scale describe it. Their constraints were real, and the write-ups often say what their choices cost.
- Amazon Dynamo paper ↗Consistent hashing, vector clocks and quorums in the paper that popularised them.
- Please stop calling databases CP or AP ↗Read after the paper, as a corrective to the classification.