Design a distributed key-value store - a Dynamo-style database with put and get that keeps 100 TB on 75 equal nodes, stays writable when a node or the network fails, and never loses a write it has answered OK. One node flushes 81,000 files in a month, and one slow copy answers in 200 ms when the target is 10 ms. The staff-level signal is choosing how files are merged, how many copies must answer, how two writes at the same time are told apart, and how copies become equal again.
Written by a Staff+ Engineer and an Engineering Manager
12+ years of experience in distributed systems and real-time messaging. Tech lead for large-scale chat applications serving billions of users. Over 12 years of industry experience with 6 years in engineering leadership, building and scaling tech infrastructure teams that deliver end-to-end large-scale distributed systems.
Finished this lesson?
Sign in to save your progress across devices.
Tip: use ← and → to move between lessons.
Was this lesson helpful?
Rehearse this exact question with the author
45-minute mock with a Staff+ engineer who interviews at your target company. Premium members save $20.
What to Design?
Could you answer these today? Tick what you already know.
A ring of nodes, three copies of each key, a log and some sorted files: most candidates can draw that. The interview is decided by four questions about what happens after the drawing: after a month, after 50 ms, after a network split, after a dead disk. Each one is a deep dive later on this page, with three designs side by side: the one that sounds right, a better one, and the one that holds.
Swipe to see all columns
| The four questions, and the answer that sounds right | |||
|---|---|---|---|
| # | The interviewer asks | The answer that sounds right | What it misses |
| DD1 | A node has flushed files for a month. How long does one read take? | Each file has a Bloom filter, so reads are fast | There are 81,000 files by then. Asking the filters alone takes 8 ms, and their wrong answers cost 81 ms more. Files must be merged, and how you merge decides how much disk you need |
| DD2 | alice writes, gets OK, and reads 50 ms later from another device. Does she see her write? | Yes, there are three copies | |
The same three habits answer all four:
The API is two calls. Spend your questions on what the store promises when things fail.
Swipe to see all columns
| Scope Decisions | ||
|---|---|---|
| Question | Agreed answer | Why it matters |
| Which operations? | put and get by one key, plus delete. The value is plain bytes. No range scans, no second index, nothing that spans several keys | Every call touches one key, so the keys can be spread by hash |
| How large? | Keys up to 256 bytes, values up to 10 KB. 100 TB today, growing towards petabytes | It does not fit on one machine, and nodes will be added all the time |
Out of scope (say this out loud): SQL queries, second indexes, range scans across nodes, transactions over several keys, and access control.
FR1 — Put and get by key. put(key, value) stores a value on disk. get(key) returns it. delete(key) removes it.
FR2 — Spread the keys. The keys are split over all nodes. When a node joins or leaves, only a small part of the keys moves.
FR3 — Three copies, and a choice. Every key is stored on N nodes (here N is 3). On each request the caller can say how many copies must answer.
FR4 — Survive failures. The store notices a dead node by itself, keeps serving, and brings the copies back in line afterwards.
The key principle behind all four
Swipe to see all columns
| Five facts and the conclusions they do not supportRead each row as: the fact on the left does not, by itself, imply the claim in the middle. | ||
|---|---|---|
| Fact | Does not imply | Why the gap exists |
| Every file has a Bloom filter | Reads are fast | A filter per file helps only when there are few files. Somebody has to merge them |
NFR1 — Latency. p99 under 10 ms for a read and for a write of one key.
NFR2 — Availability. 99.99%, which allows about 4.3 minutes of failed requests a month. The store keeps serving when nodes fail and when the network splits.
NFR3 — Durability. No write that got OK is lost when one node crashes or loses its disk.
NFR4 — Scale. From 100 TB to petabytes by adding nodes. Adding a node moves only that node's share of the data.
NFR5 — Consistency you can tune. By default a read that starts after an OK sees that write. A caller may trade this for speed, per request.
| Capacity Thought Experiment | |
|---|---|
| Parameter | Value |
| Data | 100 TB. With an average pair of 1 KB: 100 billion pairs |
| Copies | 3 of everything: 300 TB |
| Nodes | 4 TB of usable disk each: 300 / 4 is 75 nodes |
| One node, writes | 50,000 x 3 / 75: 2,000 writes a second, 2 MB a second |
| One node, reads | 200,000 x 3 / 75: 8,000 reads a second, because a read asks all three copies |
The numbers that identify the real system are two gaps:
one node writes a new file every 32 s after 30 days: 81,000 files, and a read may have to ask each one
one slow copy answers in 200 ms the target for a write is 10 ms
So the hard part is not "store a pair". It is keeping reads fast on a disk that fills with files, and deciding how many nodes to wait for. The average pair size and every per-node number on this page are illustrative assumptions.
Interviewer signal: candidates who say "consistent hashing, three replicas, done" have named the parts. Candidates who say "a node writes 2,700 files a day, and one of three copies is always the slow one" have found the system.
Three ideas shape everything:
Changing data in the middle of a file is slow. Adding to the end of a file is the cheapest write a disk has. So a node never changes a file. It appends each write to a log, keeps the newest writes sorted in memory, and writes them out as a new sorted file when memory is full. The price comes later: many files, which must be merged.
There is no leader and no central directory. Every node knows which nodes hold which keys, and any node can take any request. So no single machine can stop the store, and no election is needed when one dies. The price: nobody puts the writes in one order.
A write is OK when two of three copies have it. The third is late, or down, or on the other side of a network split. So the design does not pretend that copies are equal. It makes reads and writes overlap, gives every value a version, and repairs copies in the background.
The core recognition
This is a database that chooses "always writable" over "one order of writes". Every other choice follows from it: no leader, three equal copies, versions that can show a conflict, and repair that never stops.
| Design Decisions | |
|---|---|
| Design choice | Reason |
| Log first, then memory, then OK | A crash of the node loses nothing it has answered |
| Sorted files that are never changed, merged in levels | Writes stay cheap, and a read checks at most one file per level |
The site has a second key-value design: KV Cache with Write-Ahead Log. That one keeps data in memory and has one leader per shard, so writes have one order. This one keeps data on disk and has no leader, so it stays writable in a split. If the log itself is new to you, read Write-Ahead Logging first.
One record carries the design: the pair, as it is stored on each of its three nodes. The field to explain is
clock, the version that lets two copies be compared.
One per key and per copy. It lives first in memory, later in a file.
Swipe to see all columns
| pair | ||
|---|---|---|
| Field | Type | Comment |
key | BYTES | Up to 256 bytes, for example cart:alice |
value | BYTES | Up to 10 KB. The store does not look inside |
clock | ||
Swipe to see all columns
| What a node keeps | ||
|---|---|---|
| Thing | What it holds | Why it exists |
| WAL (disk) | Every write, in arrival order, each with a length and a checksum. The write-ahead log | After a crash the node reads it back and has its newest writes again |
| Memtable (memory) | The newest writes, sorted by key. 64 MB | Fast to write and fast to read. Lost on a crash, which is why the WAL exists |
| SSTable (disk) | A file of pairs sorted by key, with a small index and a Bloom filter. Never changed once written. The name means sorted string table | |
Swipe to see all columns
| The calls | ||
|---|---|---|
| Call | Input | Returns |
PUT /kv/cart:alice | The value as bytes. Header X-Consistency-Level: ONE, QUORUM (default) or ALL. Header X-Context: the version from the last GET, when this write changes an existing value | |
Swipe to see all columns
| The three consistency levels | |||
|---|---|---|---|
| Level | A write waits for | A read waits for | Use it when |
ONE | 1 copy | 1 copy | Speed matters more than seeing the newest value |
QUORUM | 2 of 3 copies | 2 of 3 copies | |
A retry is safe. When a client gets 503 or no answer, it sends the same PUT again with the same context. Writing the same value twice leaves the same value.
Between nodes there are a few internal calls: REPLICA_PUT and REPLICA_GET carry one pair to or from a copy, GOSSIP swaps Ring tables, DELIVER_HINT hands a kept write to its node, and COMPARE_TREE compares two copies of a key range (DD4).
Present the design as four short stories, each in its own colour on one whiteboard: Phase 1 one node (black), Phase 2 the ring (blue), Phase 3 three copies (green), Phase 4 a node goes down (purple). All four follow one key: alice's shopping cart. The number on an arrow is the same number in the animation and in the step table.
Swipe to see all columns
| The four phases | |||
|---|---|---|---|
| Phase | Colour | Story | What it leaves behind |
| Phase 1: One node stores a write (FR1) | Black | Node A logs alice's cart, keeps it in memory, and later writes it to a sorted file | A write that survives a crash of Node A |
| Phase 2: Find the nodes of a key (FR2) | Blue | Any node can work out, from the ring, which three nodes hold cart:alice | Keys spread over 75 nodes, with no directory |
How to read the picture
cart:alice, alice's shopping cart. It starts as milk. Later she adds eggs.Each phase below is an interactive walkthrough of the same picture. It plays on its own. You can pause it, step with the arrow keys, and click any component to read its role.
Goal: a single node takes a write at the speed of an append, keeps it through a crash, and can find it again.
/kv/cart:aliceClient → Node ANothing is written in this step.
Swipe to see all columns
| Phase 1 step by step (black arrows) | ||
|---|---|---|
| Step | What happens | Data written |
| 1 | alice puts milk in her cart. The PUT for cart:alice arrives at Node A. | Nothing yet |
| 2 | Node A appends the write to the end of its WAL and waits until the disk has it. Writes that arrive in the same millisecond share one wait. | WAL: one record, cart:alice = milk |
| 3 | Node A puts the pair into the Memtable, which keeps its pairs sorted by key. Then it answers OK. | |
Why it is built this way
This design of log, sorted memory and sorted files is called an LSM tree (log-structured merge tree). The word "merge" is the part that is still missing here: DD1.
What is missing. One node holds 4 TB. The data is 100 TB. Phase 2 spreads it.
In the WAL. The Memtable is memory and it is gone. When Node A starts again, it reads the WAL from the front and puts every record back into a new Memtable. Only the writes since the last flush are in the WAL, at most 64 MB, so this takes a second or two. A record that was cut off in the middle fails its checksum and is dropped: its client never got OK.
Goal: any node can say which nodes hold a key, and adding a node moves almost nothing.
ring tableNode A ↔ Node B, and so on between all nodes11,250 positionsupdateSwipe to see all columns
| Phase 2 step by step (blue arrows) | ||
|---|---|---|
| Step | What happens | Data written |
| 1 | Once a second every node picks one other node at random and the two swap their Ring tables. This is called gossip. After a few rounds all 75 nodes hold the same table. | Ring table: 11,250 positions, the same on every node |
| 2 | The client knows the addresses of a few nodes and sends the PUT to any of them. Here it reaches Node A. | Nothing |
| 3 | ||
Why it is built this way
hash mod 75. With mod, a 76th node changes the answer for almost every key, and 300 TB would move. On the ring, a new node only takes the keys in front of its own positions.If the request lands on a node that holds no copy of the key, that node passes it on to one that does. In the example Node A holds a copy itself.
Because 51 and 53 are both Node B. Two copies on one machine are one copy: when that machine dies, both are gone. The walk counts machines, not positions. A real store goes one step further and also skips machines in the same rack.
Goal: a write survives the loss of a node, and a read sees the last answered write, without waiting for the slowest node.
cart:alice = milkNode A → Node B and Node C, in parallelcart:alicenew rowSwipe to see all columns
| Phase 3 step by step (green arrows) | ||
|---|---|---|
| Step | What happens | Data written |
| 1 | Node A handles the write, so it gives it the version [A:1]. It stores the pair itself, as in Phase 1, and at the same moment sends it to Node B and Node C. | Node A: cart:alice = milk [A:1] |
| 2 | Node B has the write in its own WAL and answers. Node C is slower. | Node B: cart:alice = milk [A:1] |
| 3 | Two of three nodes have the write: Node A answers OK with the version [A:1]. The answer from Node C comes a moment later and changes nothing for the client. | |
Why it is built this way
What is missing. What happens to the third copy when its node is not slow, but down? Phase 4.
No. Node B keeps what it stored, and a later read may return it, although the writer was told "failed". The store has no undo across nodes. The rule for clients: after a failed write, send the same write again until it gets OK. A PUT of the same value twice is harmless.
Goal: a node can be away for a while without one failed request, and has all its data when it is back.
ring tableNode A → Node Csoon every Ring table says: Node C is downNode CupdateSwipe to see all columns
| Phase 4 step by step (purple arrows) | ||
|---|---|---|
| Step | What happens | Data written |
| 1 | 10:00:05. Node C restarts for a deploy. Node A hears nothing from it for 2 seconds and marks it as suspected. Gossip spreads the news, and soon every Ring table says: Node C is down. | Ring table: Node C, up to down |
| 2 | alice adds eggs. The PUT reaches Node A with the version she last read, [A:1]. Node A stores the new cart as [A:2] and sends it to Node B. The copy for Node C goes to the next node on the ring, Node D, with a note: "for Node C". That note is a hint. | Node A: cart:alice = milk, eggs [A:2] |
Why it is built this way
With a strict rule, no: only Node A of the three real nodes is left, and W is 2. A store that puts "always writable" first lets Node D count: A plus the substitute D make two, and alice gets OK. This is called a sloppy quorum. The price: a read that asks B and C a moment later, when they are back, can miss the eggs until the hints arrive.
The deep dives are the four questions from the top of the page. Each one compares three designs for the same scenario: a Bad one, which is usually the answer that sounds right; a Good one, which still leaves a gap; and a Great one, which holds. The answers start covered. Read the scenario, decide what you would build, then click each card.
Swipe to see all columns
| Deep Dive Map | |||
|---|---|---|---|
| Deep Dive | Topic | Scenario | NFR |
Scenario: Node A has run for 30 days. Every 32 seconds it wrote one more SSTable. Now alice opens a cart that she last changed four weeks ago. The read must take less than 10 ms. Keep two costs in mind: asking one Bloom filter takes about 100 nanoseconds, and one read from the SSD takes about 0.1 ms.
Three ways to build it. Decide what you would do first. The answers are covered: click a card to reveal it.
The idea. A flush writes a file. A read asks the Bloom filters. Leave it at that.
Where it breaks. After 30 days there are 81,000 files. alice's cart is in one of the oldest, so the read asks 81,000 filters first: 8.1 ms. About 1% of them say "maybe" by mistake, and each mistake is a useless read from the SSD: 810 reads, 81 ms. Together about 89 ms, when the target is 10 ms. The disk suffers too. Old values are never thrown away, so the node adds 172.8 GB of files a day: 5.2 TB in a month, on a node meant to hold 4 TB.
The idea. When four files of about the same size exist, merge them into one. Four files of 64 MB become one of 256 MB. Four of those become one of 1 GB, and so on. A merge reads sorted files and writes one sorted file, and it keeps only the newest value of each key. This is called size-tiered compaction. Compaction is the word for merging files in the background.
What it fixes. The number of files. There are 8 sizes between 64 MB and 1 TB, with at most 3 files waiting in each: about 24 files. The read asks 24 filters and does one read from the SSD. It takes far less than 1 ms.
What it still misses. The disk. The largest merge turns four files of 1 TB into one of 4 TB, and until it is done the old files and the new one exist together. It needs 4 TB of free space: the disk must be twice as large as the data. And files of one size overlap, so a key can be in every one of the 24 files.
The key idea. Give every file a place. Inside one level, no two files hold the same key range. Then a key is in at most one file per level, and a merge only touches the files next to it.
How it works
The three designs side by side, in one picture:
Play the three options. Switch between them with the tabs at the top of the figure.
flushMemtable → SSTables (disk)one new filenew rowSwipe to see all columns
| DD1 side by side | |||
|---|---|---|---|
| Bad: never merge | Good: by size | Great: in levels | |
| Files a read may check | 81,000 | About 24 | At most 9 |
| Read of an old key | About 89 ms | Under 1 ms | Under 1 ms |
| Free disk a merge needs | |||
Check it against the scenario
| A read on day 30 | |
|---|---|
| Situation | Outcome with the Great option |
| alice opens a cart from four weeks ago | The pair is in level 5. Eight filters say no, one says maybe, one read from the SSD: well under 1 ms |
A GET for a key that does not exist | Nine filters say no, and there is no disk read at all |
The trade-off to name. Levels make reads and disk use predictable, and they make writes expensive. The node takes 2 MB a second from clients. With levels it may write up to 100 MB a second in the background; with size-tiered about 16 MB a second. An SSD carries both. For data that is almost all writes, such as a log of events, size-tiered is the better choice.
4 billion keys times 10 bits is 40 billion bits: 5 GB. On a machine with 64 GB that is fine, and it is memory well spent: each filter saves disk reads. What you must not do is put the filters on disk, because then asking a filter is a disk read too.
"Each node is an LSM tree: log, a sorted table in memory, and an immutable sorted file every half minute. Without merging, a node has eighty thousand files after a month, and a read takes about ninety milliseconds even with Bloom filters. Size-tiered merging keeps about two dozen files, but its biggest merge needs free space equal to the data. So I use leveled compaction: each level is ten times the one above and its files do not overlap, so a read checks one file per level and a merge needs under two gigabytes. The price is write amplification, up to about fifty times."
Scenario: At 10:00:00 alice's phone writes cart:alice = milk and gets OK. 50 ms later her laptop reads the cart. The three nodes answer at different speeds: Node A in 1 ms, Node B in 2 ms, and Node C in 200 ms, because it is busy merging files. Does the laptop see the milk? And does the store still take writes when a node restarts?
Three ways to build it. Decide what you would do first. The answers are covered: click a card to reveal it.
The idea. A write is OK as soon as one node has it. A read asks one node. In numbers: W is 1 and R is 1. Copies go to the other nodes in the background.
Where it breaks. The phone gets OK after 1 ms, from Node A. 50 ms later the laptop's read asks one copy, and it happens to be Node C. Node C is still 150 ms away from having the write. The laptop gets "not found": an empty cart, right after alice filled it. And there is a second problem. If the disk of Node A dies before a copy has landed anywhere, a write that got OK is gone. OK meant one disk.
The idea. A write is OK only when all three nodes have it: W is 3. Then a read can ask any single node: R is 1.
What it fixes. The read. Every copy has the milk before the phone hears OK, so the laptop sees it whichever node it asks.
What it still misses. Speed and failures. The phone now waits for Node C: 200 ms, when the target is 10 ms. Worse, when Node C restarts for 60 seconds, every write of a key with a copy on C fails. That is 4% of all keys: 2,000 writes a second, 120,000 in that minute. A deploy restarts all 75 nodes one by one: 75 minutes of failed writes, when 99.99% allows 4.3 minutes a month.
The key idea. You do not need every node. You need the nodes of a read and the nodes of a write to share one. With three copies, any two nodes and any other two nodes always share at least one.
How it works
PUT reaches Node A. A stores it and sends it to B and C.GET. The node that takes it asks all three copies and uses the first two answers: R = 2.milk, version [A:1]. The laptop sees the milk.The three designs side by side, in one picture:
Play the three options. Switch between them with the tabs at the top of the figure.
/kv/cart:alicePhone → Node Acart:alicenew rowSwipe to see all columns
| DD2 side by side | |||
|---|---|---|---|
| Bad: W 1, R 1 | Good: W 3, R 1 | Great: W 2, R 2 | |
| The phone waits | 1 ms | 200 ms | 2 ms |
| The laptop sees the milk | Maybe not | Yes | Yes |
| Writes while one node is down | |||
A group of answers that is large enough to count is called a quorum. The rule for picking W and R is one line: W + R must be larger than N.
Swipe to see all columns
| Pick W and R (N is 3) | |||
|---|---|---|---|
| W | R | W + R | What you get |
| 1 | 1 | 2 | No overlap. Fast, and a read can miss the last write |
| 3 | 1 | 4 | Overlap. Fast reads, but a write needs every node |
| 1 | |||
Check it against the scenario
| OK at 10:00:00, a read 50 ms later | |
|---|---|
| Situation | Outcome with the Great option |
| The read gets its answers from A and B | Both say milk. Done in 2 ms |
| The read gets its answers from B and C | B says milk, C says not found. The newer one wins: milk. C is sent the value |
| Node C restarts for 60 seconds | No request fails. C gets what it missed afterwards (DD4) |
The trade-off to name. Two things. First, every read costs three requests inside the store, not one. A caller that can live with an older value sends X-Consistency-Level: ONE. Second, the promise has a limit. The overlap holds when the answers come from the three real nodes of the key. In a network split this store lets a substitute node count, so that writes still succeed, and a read can then miss a write for a while. That is the "available first" choice from the requirements, and DD3 is about what follows from it.
In one narrow case, yes. Take a write that reached only Node C before its coordinator gave up and answered 503. A first read hears B and C and returns the value from C. A second read hears A and B and does not see it. Read repair closes the gap: the first read sends the value to B, so from then on two nodes have it. A store with a leader never shows this. Here it is part of the price of having no leader.
"Three copies. A write is answered when two have it, a read when two have answered. Two plus two is more than three, so any read and any write share a node, and that node has the write. With W equal to one, a read 50 milliseconds later can land on the copy that is behind. With W equal to three, one restarting node fails the writes for four percent of the keys. With two of three I wait about two milliseconds for the second-fastest node, and one node can be down. The caller can change the level per request."
Scenario: Go back to the cart with only milk: version [A:1], on Node A, B and C. Now the network splits. A and B are on one side, C and D on the other. At 10:00:05 alice's phone adds eggs through Node A. At 10:00:07 her laptop adds tea through Node C, where Node D stands in as the second copy. Both get OK, because the store stays writable. At 10:01 the split is over. What is in the cart? The right answer is milk, eggs and tea.
Three ways to build it. Decide what you would do first. The answers are covered: click a card to reveal it.
The idea. Every write carries the time on the clock of the node that took it. When two copies differ, keep the one with the later time. This is called "last write wins".
Where it breaks. After the split, the nodes compare milk, eggs from 10:00:05 with milk, tea from 10:00:07. The second is later, so it replaces the first on every node. The eggs are gone. Nobody gets an error, because the store thinks it did the right thing. And "later" is weaker than it looks: the clocks of two machines differ. With a clock on Node C that runs three seconds slow, the tea would have lost.
The idea. Do not trust clocks. Count instead. Every write of a key adds one to its version number, and the higher number wins.
What it fixes. Writes that come one after the other. Version 2 beats version 1, whatever the clocks say.
What it still misses. Writes at the same time. The phone's write through Node A makes version 2 with milk, eggs. The laptop's write through Node C also starts from version 1, and it also makes version 2, with milk, tea. After the split there are two values with the same number. The numbers say "these are the same", and the values differ. The store has to pick one, by timestamp again, and an item is lost again.
The key idea. One counter cannot show that two nodes worked apart. So keep one counter per node. A vector clock is that list: for each node that handled a write of this key, how many writes it handled.
How it works
milk with the clock [A:1]: Node A handled one write. Then the split.[A:1], along. A adds one to its own counter: milk, eggs with [A:2]. Stored on A and B.The three designs side by side, in one picture:
Play the three options. Switch between them with the tabs at the top of the figure.
network splitA, B | C, DNothing is written in this step.
The rule, in one sentence: clock X covers clock Y when every counter in X is at least as large as the same counter in Y. If X covers Y, X is newer and Y can go. If neither covers the other, the two writes did not know of each other.
Swipe to see all columns
| DD3 side by side | |||
|---|---|---|---|
| Bad: timestamp | Good: one number | Great: vector clock | |
| The cart at 10:01 | milk, tea | One of the two, picked by timestamp | milk, eggs, tea |
| Does anyone know a write was dropped? | No | The store sees two values, but not why | Nothing is dropped |
The trade-off to name. The store cannot merge two carts by itself: it does not know what the bytes mean. So the app must do it, and every reader must be ready to get two values. A merge has its own trap: joining two carts brings back an item that one side had removed. For data where a lost write does not matter, such as a "last seen" time, choose "last write wins" for those keys. Then losing a write is a decision. The clocks stay small: only nodes that handled a write of this key get a counter, and the oldest are cut when there are more than about ten.
No. The second write carries the clock of the first, [A:2], and becomes [A:3]. [A:3] covers [A:2], so every node simply replaces the old cart. Most writes are like this. Only writes that started from the same version and went through different nodes end up side by side.
"The store stays writable during a split, so two clients can change the same key on two sides. Last write wins silently drops one of them, and machine clocks do not agree on which is last. One version number does not help: both sides produce version two. So each value carries a vector clock, one counter per node that coordinated a write. If one clock is at least as large in every counter, that value is newer. If not, the writes were concurrent: I keep both, return both, and the application merges them and writes back with a clock that covers both. Last write wins stays available as an explicit setting."
Scenario: Two failures. First, at 10:00:05 Node C restarts and is away for 60 seconds. It misses 120,000 writes, alice's eggs among them. Second, a week later the disk of Node C dies, and 4 TB are gone. Both times the store must get back to three equal copies, and nobody wants to copy 4 TB "just to be sure" after every restart.
Three ways to build it. Decide what you would do first. The answers are covered: click a card to reveal it.
The idea. Read repair already exists. When a read sees that one copy is older, it sends that node the newer value. Sooner or later every key is read.
Where it breaks. "Sooner or later" is not true. When alice opens her cart, Node C gets the eggs. But most carts are not opened for days, and until then they live on two disks. The dead disk is worse. A new, empty node takes the place of Node C, and reads would have to rebuild 4 billion keys one at a time. That never finishes.
The idea. This is the hinted handoff of Phase 4. While Node C is away, each write that belongs to it goes to Node D with a hint. When C is back, D hands the writes over.
What it fixes. The short outage. Node D holds 120,000 hints, about 120 MB. A minute after Node C returns, it has every one of them, whether anyone reads those keys or not.
What it still misses. Everything that is not a short outage. A substitute keeps hints for 3 hours at most, or its own disk would fill. When the disk of Node C dies, C does not return in time and the hints are thrown away. They only covered the writes during the outage anyway, not the 4 TB from before. And hints can be lost themselves, when Node D restarts. Nothing here ever checks that three copies are really equal.
The key idea. Use three tools, each for its own case. Hints for minutes. A full copy for a lost disk. And a cheap check that runs all the time and finds whatever the first two missed.
How it works
The three designs side by side, in one picture:
Play the three options. Switch between them with the tabs at the top of the figure.
every write of these 60 sNode A → Node CNothing is written in this step.
Swipe to see all columns
| DD4 side by side | |||
|---|---|---|---|
| Bad: reads only | Good: hints | Great: hints, rebuild, hash trees | |
| After 60 seconds away: keys nobody reads | Stay on two copies | Fixed within a minute | Fixed within a minute |
| After a dead disk | Never rebuilt | Never rebuilt | Rebuilt in about 5.6 hours |
Check it against the scenario
| A minute away, and a dead disk | |
|---|---|
| Situation | Outcome with the Great option |
| Node C is back after 60 seconds | Node D delivers 120,000 hints. Three copies of alice's eggs a minute later |
| Node D restarts while it holds hints | Some hints are lost. The next hash-tree compare of that range finds the missing keys |
| The disk of Node C dies | A new Node C copies 4 TB in about 5.6 hours. Until then its keys have two copies, and W = 2 still works |
The trade-off to name. Building hash trees costs disk reads and CPU on nodes that also serve traffic, so the compare runs at a low priority. Hints are a best effort; the hash trees make sure. The compare must reach every range within the 10 days a tombstone is kept. If a node misses a delete for longer, the tombstone is cleaned up elsewhere, and the node later hands the deleted key back as if it were new.
Because of what it costs when nothing is wrong, which is nearly always. Key by key means 9 million hashes for one range, every time. With the tree, two equal ranges are settled with one hash. The work grows with the differences, not with the data.
"I separate short failures from permanent ones. For a restart, the write of the missing replica goes to the next node on the ring with a hint, and is handed over when the replica is back. Hints are kept for three hours. If the disk is lost, a replacement takes the same positions and streams its ranges from the other replicas, about five and a half hours for four terabytes. And all the time, replicas compare their ranges with Merkle trees: equal roots mean an equal range, and otherwise they walk down only the branches that differ. Read repair is a bonus. It cannot be the mechanism, because cold keys are never read."
| If the interviewer keeps going | |
|---|---|
| Question | Short answer |
| How does a delete work? | It writes a tombstone, a pair like any other, to the three nodes. A merge removes the tombstone after 10 days. A node that was away for longer than that is rebuilt from scratch, or it would bring deleted keys back |
| Why not a leader per key range, with Raft? | A leader puts all writes in one order, so there are no conflicts and no vector clocks. But the smaller side of a network split cannot write. This question asked for "always writable". For the leader design see [KV Cache with Write-Ahead Log](/learn/system-design/questions/cache) |
| More than one data centre? | A quorum across regions adds 50 to 200 ms to every write. So each region keeps its own quorum of local copies and sends its writes to the other regions in the background. Vector clocks sort out what collides |
A node that fills the Memtable first and logs afterwards loses answered writes in a crash. The order is log, memory, OK.
A filter saves a disk read per file. It does not reduce the number of files. After a month a node has 81,000 of them, and a read takes about 89 ms.
A 76th node would then move almost every key. A ring with many positions per node moves 1 key in 76.
The next three positions on the ring can belong to the same machine. Count machines, not positions, or one failure takes two copies.
They are tied by one rule: W + R must be larger than N. Without it a read can miss the last write. With W equal to N, one restarting node stops writes.
It drops one of two writes with no error, and it depends on clocks that do not agree. Use it as a setting for data that can afford it, not as the design.
Another copy still has the old value and hands it back at the next repair. A delete is a tombstone that stays until every copy has seen it.
Keys that nobody reads are never fixed, and an empty node is never refilled. Use hints for short outages and hash trees to make sure.
The shape of a strong answer: start with one node: log, memory, sorted files, and a merge that keeps reads fast. Spread the keys with a ring, so that any node can find the three nodes of a key. Answer when two of three have the write, and read from two. Show how a version tells two writes at the same time apart. Then let a node fail, for a minute and for good, and bring the copies back.
Never change a file in place
Log, memory, sorted file. A flush every 32 seconds would leave 81,000 files in a month. Levels keep a read at one file per level and a merge at about 1.8 GB.
Overlap: do not wait for all, do not trust one
N = 3, W = 2, R = 2. Any read and any write share a node. A write waits 2 ms for the second-fastest copy, not 200 ms for the slowest, and one node may be down.
Copies will differ
A vector clock tells "newer" from "at the same time", so no write is dropped in silence. Hints fix a minute away, a rebuild fixes a dead disk, and hash trees check everything else.
| What the interviewer expects at each level | |
|---|---|
| Level | A strong answer shows |
| Junior | put and get on a hash map, a log so that a restart loses nothing, and the idea that the data must be split over machines and copied |
| Mid-level | The path of a write: log, Memtable, SSTable. A hash ring with many positions per node. N, W and R, and why W + R must be larger than N |
| Senior | Starts from one node and adds each part only when a number forces it. Compares two ways of merging files and two quorum settings with their costs. Explains vector clocks with an example, and separates a short outage from a lost disk. Says plainly what the store does not promise |
These aren't toy problems — they reflect real infrastructure challenges at companies that run storage at scale. If you want to walk through this design in a mock interview or get feedback on your own approach, book a session with us. We're here to help you sharpen your thinking, build your confidence, and land your next offer.
Pick an answer for each question, and you see at once whether it is right. Show explanation opens the answer, why it is right, and why each of the other options fails.
Question 1 of 10
A PUT arrives at a node. What does the node do first?
Question 2 of 10
During a read, the Bloom filter of an SSTable says the key is not in the file. What does the node do?
Question 3 of 10
A node has never merged its files. After 30 days it holds 81,000 SSTables. Why is a read of an old key slow, although every file has a Bloom filter?
Question 4 of 10
With leveled compaction and five levels below level 0, how many files of levels 1 to 5 can hold a given key?
Question 5 of 10
The store holds 100 TB, keeps three copies, and each node has 4 TB of usable disk. How many nodes does it need?
Question 6 of 10
Why does each machine get 150 positions on the ring, and not one?
Question 7 of 10
N = 3, W = 2, R = 2. Why does a read that starts after an OK see that write?
Question 8 of 10
The store runs with W = 3. One of the 75 nodes restarts for 60 seconds. What happens to writes?
Question 9 of 10
Two values of one key have the clocks [A:2] and [A:1, C:1]. What does the store do?
Question 10 of 10
A node lost its disk and was replaced by an empty machine. Which mechanism fills it?
| Which copy did the read ask? One answer is too few. Waiting for all three stops every write when one node restarts |
| DD3 | Two devices change the same cart during a network split. What is in the cart afterwards? | The last write wins | Then one item vanishes with no error. And two machines do not agree on what "last" is |
| DD4 | A node was away for a minute. Another one lost its disk. How do the copies become equal again? | Reads repair what they find | Keys nobody reads are never fixed, and an empty node is never rebuilt that way |
| How much traffic? | 50,000 writes and 200,000 reads a second | Four reads for every write, and both must stay under 10 ms |
| What happens in a network split? | Stay available. Both sides keep taking reads and writes, and some reads may be a little old | No leader that one side would lose. And two sides can change the same key (DD3) |
| What does OK promise? | A write that got OK is not lost when a node crashes | OK must mean more than one disk (DD2) |
| Two writes to one key at the same time? | "Last write wins" is fine as a default. But the store must be able to tell that two writes were at the same time | A timestamp cannot tell that. Versions must (DD3) |
| There are three copies | A read sees the last write | A read that asks one copy can hit the one that is behind |
| The write got OK | All three copies have it | OK comes after two answers. The third copy may be late, or down |
| One write has a later timestamp | It happened later | Clocks of two machines differ. And two writes can simply be at the same time |
| The node is back | Its data is complete | It missed every write while it was away. Something has to bring them |
| Files on one node |
| The newest writes sit in 64 MB of memory. It is full every 32 seconds: 2,700 new files a day |
| One node's share | A node holds a copy of 3 in 75 keys: 4% |
| A Bloom filter per file |
| Most files answer "not here" without a disk read |
| A hash ring with 150 positions per node | A new node takes 1 in 76 keys. Nothing else moves |
| Any node takes any request | No directory, no leader, no single point that can fail |
| N = 3, W = 2, R = 2 | A read and a write always share a node, and one node may be down |
| A vector clock on every value | The store can tell "newer" from "at the same time" |
| Hints for short outages, hash trees for the rest | Copies become equal again without copying everything |
LIST |
| The version: one counter for each node that handled a write of this key, for example [A:2]. Explained in DD3 |
written_at | TIMESTAMP | The wall-clock time of the write. Used only when the caller chose "last write wins" |
deleted | BOOL | True for a deleted key. Such a pair is called a tombstone: it says "this key is gone" until every copy knows |
| A full Memtable becomes one SSTable. Sorted files are easy to search and to merge |
| Bloom filter (memory) | A small bit map per SSTable, 10 bits per key. It answers "is this key maybe in the file?" It can say maybe by mistake, in about 1% of cases. It never says no by mistake | A read skips most files without touching the disk |
| Ring table (memory) | 11,250 rows: a position on the ring, the node that owns it, and whether that node is up | Tells any node which three nodes hold a key |
| Hint (disk) | A write that belongs to another node that is down, with that node's name | Handed over when the node is back (DD4) |
200 and the new version, for example [A:2]. 503 when too few copies answered |
GET /kv/cart:alice | Header X-Consistency-Level | 200, the value and its version in X-Context. Two values when the store holds two that were written at the same time (DD3). 404 when the key does not exist |
DELETE /kv/cart:alice | Same headers as PUT | 200. Inside it is a PUT of a tombstone |
| The default. A read sees the last answered write, and one node may be down |
ALL | 3 copies | 3 copies | Rarely. One node that is down fails the request |
| Phase 3: Three copies, two answers (FR3) | Green | Node A sends the write to B and C and answers after two of three have it | A write that survives the loss of a node, and a read that sees it |
| Phase 4: A node goes down and comes back (FR4) | Purple | Node C restarts. Node D keeps its writes and hands them over a minute later | Three equal copies again, with no person involved |
| Memtable: cart:alice = milk |
| 4 | After 32 seconds the Memtable holds 64 MB. Node A freezes it, starts a new one, and writes the frozen one to disk as one SSTable with its Bloom filter. The part of the WAL that this file covers is deleted. | One new SSTable of 64 MB |
| 5 | Later a GET for cart:alice arrives. Node A looks in the Memtable first. If the key is not there, it goes through the SSTables from newest to oldest. For each file it asks the Bloom filter first, and opens the file only on a "maybe". | Nothing |
| Node A hashes the key. The hash is a position on a ring: cart:alice lands on 42. |
| Nothing |
| 4 | Node A walks the ring clockwise from 42 and takes the first three different machines: 45 is Node A, 51 is Node B, 53 is Node B again and is skipped, 58 is Node C. These three hold cart:alice. Node D, at 64, would be next. | Nothing |
| 5 | A new machine, Node F, joins. It picks 150 positions and gossips them. Only the keys that sit just before one of its positions move to it: 1 key in 76, about 4 TB. | Ring table: 11,250 to 11,400 positions |
| Node C: cart:alice = milk [A:1], a moment later |
| 4 | Later a GET arrives at Node A. It reads its own copy and asks Node B and Node C for theirs. It waits until it has two answers in all. | Nothing |
| 5 | Node A compares the versions and returns the newest value: milk, [A:1]. If one of the answers had been older, Node A would send that node the newer value. This is called read repair. | Nothing here: all copies agree |
| 3 |
| Node B answers. Two of the three real nodes have the write: OK, version [A:2]. Node D keeps its copy apart from its own data. |
| Node B: milk, eggs [A:2]. Node D: one hint for Node C |
| 4 | 10:01:05. Node C is back and gossips again. The Ring tables say: Node C is up. Its copy of the cart is still milk, [A:1]. | Ring table: Node C, down to up |
| 5 | Node D sees that Node C is up, sends it the kept write, and deletes the hint. All three nodes hold the same cart again. | Node C: milk [A:1] becomes milk, eggs [A:2] |
| DD1 |
| A read after a month of flushes |
| Node A has written a file every 32 seconds for 30 days |
| NFR1 — Latency |
| DD2 | How many answers to wait for | A write gets OK, and a read follows 50 ms later while one node is slow | NFR1, NFR2, NFR5 — Latency, availability, consistency |
| DD3 | Two writes at the same time | Two devices change one cart during a network split | NFR2, NFR3 — Availability, durability |
| DD4 | A node is away for a minute, or for good | Node C restarts, and a week later its disk dies | NFR3, NFR4 — Durability, scale |
This is called leveled compaction.
| None, but the disk fills up |
| As much as the data: 4 TB |
| About 1.8 GB |
| Background writes | None | About 16 MB a second | Up to 100 MB a second |
| Go on |
| Fail for 4% of the keys |
| Go on |
| Disks that have the write at OK | 1 | 3 | 2 |
| 3 |
| 4 |
| Overlap. Fast writes, but a read needs every node, and OK means one disk |
| 2 | 2 | 4 | Overlap, and one node may be down for reads and for writes |
[A:1]. C adds a counter of its own: milk, tea with [A:1, C:1]. Stored on C and D.[A:2] has more of A. [A:1, C:1] has more of C. Neither covers the other, so neither is newer: they were written at the same time. The store keeps both.GET now returns both values, with a clock that covers both: [A:2, C:1]. The shop's code merges them: milk, eggs, tea.[A:3, C:1]. This clock covers both old ones, so the new value replaces both, on every node.| Depends on machine clocks |
| Yes |
| Yes, to break the tie |
| No |
| Extra cost | None | One number per value | A few counters per value, and a merge in the app |
A hash tree of this kind is called a Merkle tree.
| Copies that differ for any other reason |
| Found only by a read |
| Never found |
| Found by the hash trees |
| Data sent to find 100 different keys in 9 GB | — | — | About 27 MB |