Lesson 3 · Minutes 20-30

Key-Value Store: Scaling It

Problem

Ten minutes and four probes take this one machine to 24, and nothing arrives until a probe asks for it. Chapter 3's Consistent Hashing and Chapter 4's Quorums did the teaching, so from minute twenty to minute thirty you spend your time on this store's numbers. The four, in the order they come:

  • Sixteen terabytes cannot sit on one machine, so a get needs some way to find the machine holding its key.
  • A machine holding a copy of some key dies in the middle of live traffic.
  • A network split lets two replicas accept different values for the same key.
  • A replica that spent an hour offline returns and has to catch up.

Where a Key Lives

"A get arrives for one key out of 8 billion. Which machine does it go to, and what happens when you add another?" Keys hash onto a ring, the arrangement from Consistent Hashing, and a key belongs to the next machine clockwise. Assume 100 points on the ring for each machine, that lesson's virtual nodes. That gives a small map any client or machine can compute for itself. There is no directory, because the question rules out a machine everything needs.

A 25th machine is first home to one key in 25, and with one copy of every key in each zone, it holds one in nine of its own zone's copies. That is 16 TB over nine, about 1.8 TB, where modulo would move 46 TB. All of it lands on the new machine, so at 125 MB a second the copy takes just under 4 hours. It starts on purpose and never on its own, as Chapter 3's Hot Partitions says.

The number from Scope forces a second change here: values no longer fit in memory. So each machine keeps its values on disk, in the log the first design already had, with only a hash from key to place in the log in memory. One machine holds 1 billion values. Assume 50 bytes of index for each, and that is 50 GB, half its memory. The snapshot still does its job, now for the index alone, because the values now live only in the log.

What it costs: a get now costs an NVMe read, about 0.1 milliseconds, a thousand times the memory read. And overwritten values pile up in the log until a background compaction copies the live ones forward and reclaims the rest. One gap stays open, and the interviewer should hear you leave it: "Assume every machine already knows the ring. How it learns that is the deep dive."

The Dead Machine

"One of a key's machines has just gone quiet, a put is in flight and a get is waiting. Who deals with them?" The two other copies, which means there have to be other copies. Keep three, on the next distinct machines clockwise, skipping a zone that already holds one, with no leader. Any machine a client reaches becomes the coordinator, the one that sends the request out and counts the replies. A put goes to all three and counts two confirmations. A get asks two. If one of the two stays silent past a short request timeout, the coordinator asks the third. A put that times out with one confirmation is unknown, not undone: its copy can still spread, so the client retries it with the same value and the same context, so a newer write by another client survives beside it as a sibling instead of being overwritten.

Two plus two is more than three, so a read meets the last confirmed write, and one machine may die. That is Quorums with this store's numbers. It also keeps the first design's promise and improves on it: a confirmed write now sits flushed in two logs, and a restart no longer stops answers, because the other two answer while this one replays.

So when a machine dies, nothing fails over and nothing stops, because no copy was special. That is the difference Leaders, Followers and Everyone draws. Its peers notice by gossip, the part you parked: each machine keeps passing what it knows of the others to a few peers. The third copy of each put goes to the next healthy machine, with a hint naming the machine it belongs to.

The dead machine's data is not re-copied yet. Two terabytes is about 4.4 hours, for what is usually a reboot, so only a person or a slow controller declares a machine gone. Away and gone are different states, with different owners.

One more case: two of a key's homes are unreachable at once. A strict quorum refuses, and a sloppy one writes to stand-ins elsewhere. Scope said keep answering, so sloppy is the default, and a table, a named set of keys with its own settings, can turn it off. A get in that case asks the one home left and the stand-ins, so it still answers, possibly with an older value.

What it costs: every put, and every get of two, waits 1 to 2 milliseconds for another zone. Until a stand-in hands its hint back, a read of the key's homes may miss a write the stand-in helped confirm, so the overlap promise now has a condition. And neighbours carry the hints on their own disks.

Two Writes, One Key

"The split has healed, and two replicas took different values for one key. Neither may be lost. What does a get return?" Both, and that takes versions. Each value carries a version vector, a counter for each machine that coordinated a write to it. On a read, a version that has seen everything in another replaces it. Two that have each seen something the other has not are concurrent, and both are kept as siblings. Chapter 4's Concurrent Writes and Conflicts works through the comparison.

The API grows here, as you said in Scope that it might. A get returns the values and a context, the version information for what it read. A put carries that context back, so the next put collapses everything it has seen into one value. Its answer carries the new context. Each call can also name its R or W, the dial Scope promised.

The two calls, after a conflict forced more
get(key, r)                   -> values, context
put(key, value, context, w)   -> ok, context

Who resolves it is chosen per table. Last writer wins where a whole value is replaced and a lost one is harmless, and Chapter 5's Clocks and Ordering says what "last" is worth. A type that merges itself for counters and sets. Siblings go to the client for the rest.

What it costs: a client that must merge, or siblings that pile up, and bytes. Assume three counters at 16 bytes, about 50 bytes a value. Three copies of 8 billion values is 24 billion, and at 50 bytes each that is 1.2 TB on top of 48 TB, or 2.5 percent. That takes the data past what 24 machines hold, and overwritten values and hints need room too, so 24 is the floor, and a real build runs a machine or more a zone above it. Leave one question parked and say so: how many counters a vector may grow to. The deep dive called Who Resolves It picks that up.

The Hour Away

"That replica was down for an hour and has just rejoined. How does it get level?" Two mechanisms, in this order, as Quorums sets out. Hints come first. At peak this machine's share is 3,750 writes a second, and an hour is 3,600 seconds. That is 13.5 million hints. At 2 KB each they come to 27 GB, held by its neighbours, and at 125 MB a second they arrive in about 4 minutes.

Then anti-entropy, the background comparison, for what the hints missed, since a neighbour holding some of them may itself have died. Replicas compare a Merkle tree for each range, the tree of hashes from Quorums. They descend only where the hashes differ, and copy only those keys. Read repair helps on keys people read, and a key nobody reads waits for the tree.

Reject the whole-range copy out loud: about 4.4 hours to fix what 4 minutes mostly fixed. Assume hints are kept for three hours and then dropped, since they cost neighbours disk, and a machine away for longer relies on the trees. Meanwhile the machine answers, and a read of two, with versions on the values, lets the newest win.

What it costs: trees are rebuilt whenever ranges move, and a tree's first read of 2 TB is about 200 seconds of the disk at full speed. So it runs at low priority.

One Engine on Every Machine

A client reaches any machine, which coordinates. At least 24 machines sit on a ring with virtual nodes across three zones, each with a log on disk and an index in memory. Three copies, R and W of 2 by default per request, sloppy writes with hints, versions on every value, Merkle trees between replicas, and gossip for membership.

Of the first design's two promises, durability survived and got stronger, and one order did not: it has been replaced by an overlap with stated exceptions and by versions that detect what the overlap cannot. Say what stayed, because it earns back the ten minutes you spent on one machine: "The one-machine design didn't go away. It's the engine on every machine now, with the log as the data. What's new is around it: where a key lives, who else holds it, and what happens when they disagree."

The full design, with what changed since the first design marked new
Zone A Zone B Zone C Neighbourholds the hintNEW Replica Adown, back soonNEW Clientget and put Coordinatorany machine, count 2NEW Replica Blog, index in memory Replica Clog, index in memoryNEW get put hint on return get 2 Merkle gossip readwriteasyncNEWsince the first design
Interview Pitfalls
  • Answering the dead machine with a failover, in a design that has no leader. The interviewer hears a design you have not understood yet.
  • Starting a 2 TB re-copy the moment a machine goes quiet, so a reboot costs hours of disk and network.
  • Saying R plus W above N makes the store consistent, when sloppy writes are one of the cases it leaves out. The next question is exactly that case.
  • Reaching for read repair alone at the hour away, and then being asked about a key nobody reads.
What Interviewers Listen For
  • What each failure does to the promise, not only to the machine.
  • A machine that is away kept apart from one that is gone, with who decides.
  • The hour away in gigabytes and minutes, and the whole-range alternative in hours.

Knowledge Check

A 25th machine joins the 24 on the ring, and copies run at 125 MB a second. What does it cost?

  • About 46 TB moved across the whole ring, in about four days
  • About 1.8 TB copied to the new machine, in just under 4 hours
  • About 2 GB of index moved, while the values stay where they are
  • Nothing up front, because each key moves when it is next read

A machine dies, and the two other copies keep answering with no promotion. What does that rely on?

  • A standby in the same zone taking over before clients notice
  • Gossip electing a new owner for those keys inside ten seconds
  • No copy being special, so any two of three can answer either call
  • The coordinator holding each write in memory until the machine returns

Why does a get now return a context, and a put carry it back?

  • It names the replica that answered, so the next call goes there
  • It lets the next put collapse the versions the client has seen
  • It holds a lease on the key, so that other clients wait for it
  • It carries the timestamp that last writer wins needs to choose

A neighbour holding hints died during the hour away. What does a key nobody reads depend on now?

  • Read repair, on the next get that reaches that replica
  • The hint, which another neighbour sends again from its copy
  • A whole-range copy, started as soon as the replica returns
  • Anti-entropy, the comparison of Merkle trees between replicas

You got correct