Design a Distributed Key-Value Store
Design a key-value store in the style of Amazon Dynamo and Cassandra. It spreads its data over many servers and keeps working when some of them fail.
Think of a key-value store like a giant locker room: every locker has a unique number (the key), and inside is a bag (the value). You can go straight to any locker without searching through all the other lockers.
- Clients
- Client app
- Services
- Key-value service
- Failure detector
- Data, outside
- Storage node A
- Storage node B
- Storage node C
1. Requirements and scope
- Put(key, value)
- Get(key) returns value
- Configurable tunable consistency (N, R, W)
- High availability (AP model)
- Sub-10ms read/write latency
- Linear horizontal scalability
2. Back-of-the-envelope numbers
- Writes: 10,000 writes a second (17%)
- Reads: 50,000 reads a second (83%)
Read-heavy: about 5 reads for each write. A cache and extra read copies help most here.
3. API
4. Data model and storage
Key: byte array, Value: byte array, Metadata: Vector Clock + Timestamp
5. The design, in three versions
Version 1: Single Node Embedded B-Tree
Single node server with SQLite / LMDB engine.
Distributed Key-Value Store: Version 1: Single Node Embedded B-Tree Version 1 of 3
The picture follows one request through the parts of this version, in order. Under it, switch a part off to see what the request does without it.
One request, step by step
Each numbered arrow is one step. Press "Simulate Request Flow" to follow them one at a time.
- The order of the steps
- A request or its reply
The parts: switch one off
1. Clients
2. Services
3. Data, outside
The steps in words0 of 3 steps shown
Client app to Key-value service
The client asks for the key user:42.
Key-value service to Local disk file
One server reads the key from its disk file.
Key-value service to Client app
The value goes back to the client.
Write path, read path and failures
Each path follows one request through the final design (version 3), one hop at a time.
Write pathPut the key user:42
- Client
- Coordinator node
- Hash ring
- Storage nodes
Client to Coordinator node
The client sends PUT user:42 to any node. That node becomes the coordinator for this request.
Coordinator node to Hash ring
It hashes the key to find the three nodes that hold it (N = 3).
Coordinator node to Storage nodes
It sends the write to all three. Each node appends it to its write-ahead log, then to its table in memory.
Storage nodes to Coordinator node
The coordinator waits for 2 of the 3 to confirm (W = 2).
Coordinator node to Client
It answers success. The third node gets the write a moment later.
Read pathGet the key user:42
- Client
- Coordinator node
- Storage nodes
Client to Coordinator node
The client sends GET user:42 to any node.
Coordinator node to Storage nodes
The coordinator asks the three nodes that hold the key.
Storage nodes to Coordinator node
It waits for 2 answers (R = 2). Because R + W > N, at least one of them has the newest write.
Coordinator node to Client
It returns the newest version, chosen by version number.
Coordinator node to Storage nodes
If one node returned an old version, the coordinator sends it the new one. This is called read repair.
What breaks, and what happens then
One of the three nodes is down
- What users see
- Reads and writes still succeed, because 2 of 3 is enough.
- What the design does
- The coordinator keeps the missed writes as hints and replays them when the node returns.
Two of the three nodes are down
- What users see
- Requests for those keys fail while W = 2 and R = 2.
- What the design does
- A caller that can accept stale data may retry with a weaker setting, such as W = 1.
A network break splits the cluster
- What users see
- Both sides may accept writes to the same key.
- What the design does
- Version numbers (vector clocks) show the conflict later. The store keeps both versions and the app merges them.
6. Deep dives
7. Follow-up questions
- How do nodes detect failure without a central master? (Answer: Decentralized Gossip Protocol heartbeats).
What it costs
Storage is the main bill: three copies turn 10 TB into 30 TB. More copies also mean more network traffic on each write. Compaction uses disk and CPU in the background, so leave room for it.
Another way to design it
A store with one leader per shard is simpler to reason about. Every write for a key goes through one node, so there are no clashing versions to merge. In return, writes for that shard stop while a new leader is chosen.
8. How answers are judged
A passing answer at Mid level. A complete design with numbers: about 10,000 writes and 50,000 reads a second. It explains how keys spread across several servers and goes deep on one part, such as replicas.
| What is judged | Junior | Mid | Senior | Staff |
|---|---|---|---|---|
| BreadthHow much of the system does the answer cover? | Names the core parts: clients, servers, a database and a cache. | Covers the whole main flow, from the request to the stored data and back. | Builds a complete design without being led through it. | Covers the system, the systems around it and who owns each part. |
| DepthHow far does it go into the hard parts? | Explains what each part does and why it is there. | Goes deep on one part, such as the database or the cache. | Goes deep on the two or three riskiest parts. | Goes deepest where the risk is largest, and explains why the risk is there. |
| DrivingWho leads the conversation? | Follows the interviewer, step by step. | Moves forward alone, with an occasional nudge. | Leads the conversation from start to finish. | Changes the problem when the question asked is not the right one. |
| Trade-offsAre choices compared and justified? | Names more than one option. | Says why one option was picked over another. | Compares options in detail, with what each one costs. | Ties each choice to cost, speed and risk for the business. |
| Unclear questionsWhat happens when the question is vague? | Answers the questions that are asked. | Asks questions to make the problem clear. | Sets the scope and says what is left out. | Questions the assumptions behind the problem itself. |
| ScopeWhat does it consider beyond the normal path? | Covers the normal path, when everything works. | Adds what happens when a part fails. | Adds growth: what breaks at ten times the load, and the fix. | Adds abuse, cost and the daily work of running the system. |
Requirements and scope
Before I design, I want to check the scope. This is a Put and Get store with tunable consistency: N replicas, and R and W for how many must answer. I will assume most callers want fast, available writes, but some need R plus W greater than N for an up-to-date read.
Every key gets stored on three servers, and every read and write waits for all three to answer, so the data is always fully consistent.
Shows: Unclear questionsThe difference: The problem asks for configurable N, R and W, not one fixed strict setting for every caller. The strong answer treats it as a per-caller choice.
Estimates
At 10,000 writes and 50,000 reads a second, with a 64-byte key and up to a 1 MB value, storage adds up fast. 10 TB of data needs 30 TB of raw storage once each key is copied to 3 replicas. A 128 GB memtable per node holds recent writes in RAM.
The store holds 10 TB of data, so I would plan for 10 TB of disk in total across the cluster.
Shows: DepthThe difference: 10 TB is the data before copies. With 3 replicas of each key, the cluster needs about 30 TB of raw storage, not 10 TB.
Core design
Put and Get requests hash the key onto a ring of virtual nodes. Each key lands on one server and its next two neighbours going clockwise, giving 3 replicas. Adding or removing a server only moves the keys near it on the ring, not the whole dataset.
I would hash the key modulo the server count to pick which one stores it, then copy it to the next two servers.
Shows: DepthThe difference: Hashing modulo the server count reassigns nearly every key when a server joins or leaves. Consistent hashing on a ring only moves the keys near the change.
Deep dive
The hard part is writes at 10,000 a second without slow disk seeks. A write goes to a log for durability, then an in-memory table sorted by key. Once that table fills, it flushes to disk as an immutable sorted file. A background job then merges old files, so reads do not check too many of them.
Each write goes straight to a row in a normal on-disk table, updated in place, with an index on the key for fast reads.
Shows: DepthThe difference: Updating rows in place means a random disk seek per write, which cannot keep up with 10,000 writes a second. Writing to a log and memory first avoids that.
Failures and trade-offs
If a replica is briefly unreachable, another node holds its writes as a hinted handoff and delivers them once it returns, so writes still succeed. A background repair job also compares replicas and fixes ones that drifted apart. Nodes gossip with each other, not a central master, to first detect a node is down.
If a replica goes down, the coordinator returns an error for any write meant for it, until that replica comes back.
Shows: Trade-offsThe difference: Failing every write for a down replica trades away the availability this design is meant to have. A hinted handoff lets a different node hold the write instead.
We wrote this guide in our own words, informed by public interview guides such as AlgoMaster and Hello Interview. We are not connected to either site. Interviewers differ. The guide and the example answers show the direction each level takes, not a checklist.
10. Check your design with your own AI
Write your design first. Then copy this prompt into the AI chat you already use, and add your design at the end. It asks for a score in each area, one fix for each, and two follow-up questions.
The site sends nothing anywhere. An AI review can be wrong, so check what it says against sections 8 and 9.
See the prompt
You are a system design interviewer. Review my design for the interview problem below. Problem: Design a Distributed Key-Value Store Design a key-value store in the style of Amazon Dynamo and Cassandra. It spreads its data over many servers and keeps working when some of them fail. What users must be able to do: - Put(key, value) - Get(key) returns value - Configurable tunable consistency (N, R, W) How well it must work: - High availability (AP model) - Sub-10ms read/write latency - Linear horizontal scalability Numbers to assume: - Key size: 64 bytes, Value size: up to 1 MB, about 60,000 operations a second (10,000 writes + 50,000 reads), roughly 5 billion a day Judge my design at the Mid level. A passing answer at this level: A complete design with numbers: about 10,000 writes and 50,000 reads a second. It explains how keys spread across several servers and goes deep on one part, such as replicas. For each area below, say whether my design is strong, fine or weak. Give one reason and one fix for each: 1. Requirements and scope 2. Estimates 3. Core design 4. Deep dive 5. Failures and trade-offs Then ask me the two follow-up questions an interviewer would ask next. Do not write a full model answer for me. My design: [Describe your parts, how one request moves through them, and why you chose each one.]