Lesson 4 · Minutes 30-45

Key-Value Store: Deep Dives and Curveballs

Problem

By minute thirty you have parked two things out loud, and the interviewer remembers both. The fifteen minutes left settle why the dials sit at two and two, how machines learn the ring with nobody in charge, and who answers for a conflict. Then four curveballs change the requirements under you.

The Consistency Point

"Why not one and one, it's faster?" is the question to expect. W of 1 and R of 1 takes the zone's round trip off a put and a get, and halves replica reads, from 7,500 a machine to 3,750. One plus one is not more than three, though, so a read can miss a write that succeeded, and a lost replica can take a confirmed write with it. Offer it per request, for data the product may show stale, such as a view count, and name the keys that never get it. Chapter 4's Quorums sets this out under Turning the Dials.

Going the other way, N of 5 with 3 and 3 survives two machines at once, and it costs 80 TB and 40 machines. Stay at three copies until the requirement says two machines at once, because that is two thirds more hardware for a failure Scope did not name.

The road not taken is one leader per partition, chosen by consensus, from Chapter 5's Leader Election and Consensus. It buys back the one order the first design had, and with it conditional writes, at the price of a stop per election. Choose it when a put must be "only if unchanged", which a plain quorum cannot promise. And say all of this per operation, as CAP and PACELC, Precisely asks: the default answers during a split and, on a normal day, pays one zone's round trip for the overlap; one and one takes the nearest copy.

N, R and W, by requirement
Most data: a read meets the last confirmed write, and one machine may die→N 3, R 2, W 2
Data the product may show stale, such as a view count→N 3, R 1, W 1, per request
Reads and writes must survive two machines at once→N 5, R 3, W 3: 80 TB, 40 machines
A put must be only if unchanged→One leader per partition, chosen by consensus

Membership With No Master

Scaling It asked the interviewer to assume every machine knows the ring, and here that assumption gets paid for. Gossip is how. Assume that once a second each machine sends three random peers its table of members and heartbeat counters, numbers each machine keeps raising on itself to show it is alive. At best the number who know multiplies by four each round: one, then four, then 16, then 64. So 24 machines take about three rounds, and 1,000 take about five.

Assume 100 bytes an entry. At 24 machines a message is 2.4 KB, which nobody will notice. At 1,000 it is 100 KB, and three of those a second is 300 KB a second from every machine, still far below the 7.5 MB a second Scope dropped. Bandwidth is not what ends gossip at this size. Amazon's Dynamo paper reports full-membership gossip working well at a couple of hundred machines and not trivial at tens of thousands, so that is the trigger to rethink it.

A machine whose counter stops rising is suspected by each peer on its own, after an assumed 10 seconds. That is a timeout, the same guess Chapter 5's Why Agreement Is Hard describes, since a slow machine and a dead one send the same silence. What changes is the price of being wrong, and that difference is yours to use out loud: "Being wrong about a dead machine is cheap here. Elsewhere a false alarm makes two primaries. Here it sends a few writes to a neighbour with a hint, so I can keep the timeout short, with no fencing." Fencing is what stops an old primary from writing, and a machine with no special role has nothing to be stopped from.

A false suspicion, here and with a leader
Hereno leader
What happens: some writes go to a neighbour with a hint. When it clears: the hints are delivered. Fencing: none. Timeout: can be short.
With a leaderone primary per partition
What happens: a follower is promoted while the old primary may still write. When it clears: only when the old primary is stopped. Fencing: required. Timeout: short ones cost false failovers.

Peers may disagree for seconds, and a stale ring reaches the wrong machine, which forwards the request. Adding or removing a machine stays deliberate, and gossip only carries the decision.

Gossip vs Coordination Service

Gossip spreads membership peer to peer with nothing to run beside the store, and views may differ for seconds. Use it when placement tolerates a stale view and the store must not depend on a control plane.

A coordination service gives one agreed membership and a watch, as in Leader Election and Consensus, and it costs a cluster that accepts no changes without a majority. Use it when moves are deliberate and one view matters more than having nothing else to run.

Who Resolves It

This is the other thing you parked, and it has two halves. First, who merges. The store resolves when the data allows it, with last writer wins or a type that merges. The client resolves when only the application knows how, such as a cart. Unlike file sync in Chapter 20, no person is asked.

Second, the size of the vector. A counter per coordinator could reach 24, since any machine can coordinate. So a machine that is not one of the key's three replicas passes the request to one that is, where it can. Past a limit a store may also trim old counters, at the cost of a false conflict now and then: two versions that were in order look concurrent, and a client merges what it did not have to.

A delete needs care too. It cannot simply remove the value, or a replica that missed the delete brings the value back during repair. So it writes a tombstone, a marker saying this key was deleted, kept longer than the longest outage repair allows for.

Curveballs

"One user must read their own writes." Do not raise the whole store, because only the writer needs it, which is the point Chapter 4's Consistency Models That Come Up makes. At two and two, strict quorums already give it, and a sloppy write is the exception. So the client keeps the context of its last put, and a read older than that does not count. It asks the third replica, then the stand-ins holding hints, and if none has the write yet it waits or says so. For data written at one and one, that user reads from all three until a reply is as new as that context, not for a fixed window.

"One key takes 50,000 reads a second." At R of 2 that is 100,000 replica reads on three machines, about 33,000 each, more than four times the 7,500 a machine carries on average. More machines change nothing, because the key still has three homes, as Hot Partitions says. If it may be stale, R of 1 halves that, and a one-second copy in the callers takes nearly all of it, at the cost of that one second. Chapter 2's When the Cache Hurts calls that copy the local tier. If the key is hot for writes, salt it into sub-keys or buffer its writes.

"A whole zone is lost." The placement bought this. Every key still has two copies, two and two keep working, and nobody fails over. What the interviewer wants next is the capacity sum. All 180,000 replica reads land on 16 machines, 11,250 each, half as much again, for as long as the zone is away. A zone that may have to take on half as much again runs at no more than two thirds of its limit, the sum Chapter 6's Regions and the Arithmetic of Nines does for regions. Against a drive's 10,000 one read at a time, 7,500 is already three quarters. So 24 machines hold only on a condition you state: the sum relies on a drive running reads side by side. If a load test shows it cannot, or the margin must be two thirds of that 10,000, build nine machines a zone, 27 in all, the margin the bytes already asked for in Scaling It. Three more machines bring it to about 6,700 reads each, and 10,000 after a loss. Third copies pile up as hints: 30,000 a second at peak, at 2 KB, is 216 GB an hour. Hold that peak for the three-hour limit and it is about 650 GB of the survivors' disk, so the limit is a decision. On return, eight machines catch up in the same order, hints and then trees, and you stagger them, because the catch-up is a burst.

"Clients want range scans." Ask what range. Inside one entity, use a compound key, hashed then sorted, from Chapter 3's Range, Hash and Directory, and each machine's hash index must become a sorted one. Across all keys, the ring threw the order away and a scan asks every machine. Say that is a different partitioning, or an ordered copy built off the writes, with its lag. Do not promise a small change.

How Each Level Answers

A candidate at mid level reaches a ring and three copies with some steering, quotes the R plus W rule and does the arithmetic for three copies. They come to hints and versions when the probes raise them, and they have to be asked what the rule leaves out.

A senior candidate asks the split question first, then sets N, R and W with the milliseconds and failures each one buys. They separate away from gone, say why a re-copy waits, offer one and one per request with the data it suits, and give the hour away in gigabytes. They name the cases the overlap misses before being asked.

A staff candidate makes conflict policy something the store's users sign up for per table, and asks who answers for a lost write. They plan operations out loud: throttles, how long hints are held, capacity after a lost zone, how a delete survives repair. They also ask whether keeps answering applies to every operation, and name the operations better off with a leader.

Interview Pitfalls
  • Calling the store AP and stopping, when the next question is what the cut-off side does with which operation.
  • Reciting ring, quorum, vector clock and gossip as a list, with no failure or number forcing each. The interviewer cannot tell what you would drop.
  • Choosing last writer wins for everything because it is simple, with no word on what vanishes.
  • Forgetting the tombstone, so that a delete returns during repair.
What Interviewers Listen For
  • A false suspicion priced here against one in a leader-based design, and the difference used.
  • The zone curveball answered with the capacity sum, not just the quorum.
  • The kind of range asked for before the scan curveball is answered.

Knowledge Check

"One user must read their own writes." What is the smallest move?

  • Raise every request in the store to read from all three copies
  • Keep the last put's context, and ask further if a read comes back older
  • Move that user's keys to one leader per partition, chosen by consensus
  • Send that user's reads to the zone their last put was coordinated in

"One key takes 50,000 reads a second." At R of 2, what does that do to its replicas?

  • About 2,100 reads each, once the load spreads over all 24 machines
  • About 17,000 reads each, because the coordinator asks only one of them
  • About 50,000 reads each, because every replica answers every request
  • About 33,000 reads each, more than four times what a machine carries

"A whole zone is lost." What does it cost the zones that remain?

  • Half as much load again on 16 machines, and 216 GB of hints an hour
  • A failover for each partition that zone led, of about ten seconds
  • Refused puts until it returns, as two of three cannot be reached
  • A 4.4-hour re-copy of all eight of its machines, begun straight away

"Clients want range scans." What do you do first?

  • Say yes, because virtual nodes keep neighbouring keys together
  • Swap each machine's hash index for a sorted one and call it done
  • Ask what range: inside one entity, or across every key in the store
  • Promise a scan that asks each machine in turn, a small read-path change

You got correct