Key-Value Store: Scope
Minutes zero to ten turn one loose phrase into numbers. "Keeps answering" could describe several different stores, and until the interviewer says what a put does during a network split, you do not know which one you are designing. By minute ten you have that answer, the byte count that sets the number of machines, the rates once copies are counted, and an API of two calls.
The habit to resist is opening with N, R and W, the copies kept, the replicas that answer a read and the replicas that confirm a write. They are Chapter 4's Quorums, and they mean nothing here until the first question below has an answer.
Six Questions, One Fork
Six questions earn their time, and each one moves something on the board. The first decides what kind of store this is, so it goes first.
- During a network split, must a put still succeed, or may it refuse? The design forks here, between leaderless quorums, where any copy takes a write, and one leader per partition, which refuses on the cut-off side. Agreed: keep answering, and a write refuses only on data whose owner asked for that.
- Must a get see the latest put? Agreed: on a normal day yes, for most data. Some data may be stale for speed, such as a view count, chosen per request. So the store has dials, not one setting.
- How much data, how many keys, how big are the values? This sets the machine count, and whether values can live in memory at all. Agreed: 8 billion keys at about 2 KB.
- What rates, and what mix? Agreed: 30,000 puts and 90,000 gets a second at peak.
- Can two clients write one key at once? Agreed: yes, and the store must not silently lose one when the caller says it matters. Who merges them is settled in Scaling It.
- Anything beyond put and get, such as scans, transactions or several regions? Each of those is a different store. Agreed: no, one region with three zones. Scans come back as a curveball.
Put the first one so that the interviewer hears why you want it now: "During a network split, may a put refuse? That one answer decides between quorums and leaders, so I want it first."
The Promises and What Stays Out
Two functional requirements: put a value under a key, and get the value of a key. Two non-functional ones carry the chapter. Any one machine or zone may fail with no stop in answers and no failover. A confirmed write survives the loss of a machine.
Then rule things out while the interviewer can still object: scans, transactions, secondary indexes, several regions, and one engine's on-disk format. Left unsaid, any of them can walk in at minute twenty-five and redraw the board.
Bytes, Then Rates
The bytes come first, because they set the machine count. Eight billion keys at 2 KB is 16 TB. Three copies make that 48 TB. Assume 2 TB of usable disk on a machine, as Chapter 3 does, and that is 24 machines, eight in each zone.
Run the same sum for memory, since the first design is about to keep everything there. At about 100 GB a machine, 48 TB is 480 machines. Twenty times the hardware for the same data is what moves values to disk, and the first probe in Scaling It makes that move.
Rates next, and here copies multiply the traffic. Thirty thousand puts to three copies is 90,000 replica writes a second, or 3,750 on each machine on average. Ninety thousand gets that each ask two copies is 180,000 replica reads, or 7,500 on each machine. An NVMe read takes about 0.1 milliseconds, so a drive does 10,000 a second even one at a time. The reads fit, the writes are half as many, and the 24 machines the bytes bought already carry the load.
A second yardstick keeps that in proportion. One key-value partition in a managed store takes 3,000 reads of up to 4 KB and 1,000 writes of up to 1 KB a second. A 2 KB put counts as two, so the peak needs 60 partitions, and 120 at half load. If keys are later cut up by a ring, as in Chapter 3's Consistent Hashing, 24 machines with an assumed 100 ring points each have 2,400 ranges. Ranges will never be what runs out.
Bandwidth has one number to drop and one to keep. Normal traffic drops out: 3,750 writes at 2 KB is 7.5 MB a second on a machine, and the reads twice that, against 1.25 GB a second on a 10-gigabit link. Moving a whole machine's data does not drop out. Assume you throttle a copy to a tenth of the link, 125 MB a second, so that live traffic keeps the rest. Two terabytes at that rate is about 4.4 hours. That number comes back the moment a machine dies.
End the estimates on what they decided, because it tells the interviewer where you are heading: "The bytes decide 24 machines, and the rates fit inside that. What decides the hard part is failure, not load."
The sum below starts from the daily figure. Thirty thousand puts at a peak of twice the average is 15,000 a second, and over about 100,000 seconds in a day that is 1.5 billion puts a day.
Two Calls, One Entity
The API is two calls. A put takes a key and a value and answers once the write is confirmed. A get takes a key and answers with its value. The one entity is a key's value. Park the rest out loud, and you have told the interviewer what to expect: "I'll keep the API to two plain calls until something forces more; I expect a conflict to."
put(key, value) -> ok get(key) -> value
- Reaching for N, R and W before asking what a split may do. The dials mean nothing until that answer sets them, and the interviewer asks what your numbers are for.
- Sizing by memory because the first design is in memory, and arriving at 480 machines without noticing. The interviewer watches the bill go past without comment from you.
- Quoting CAP as a label on the store, when the interviewer's next question is which operation. The label has no answer to that.
- Letting scans and transactions in by not ruling them out aloud. At minute twenty-five they arrive as requirements, and the ring is already drawn.
- The split question asked first, with its answer treated as the fork in the design.
- Replica traffic counted rather than client traffic, once there are copies.
- The one number that does decide named out loud: 2 TB moved at a throttled rate.
Knowledge Check
You have time for one question before you draw. Which answer forks this design?
- How many keys the store holds, and how big its values are
- Whether a put must still succeed during a network split
- How many puts and gets a second it takes at its peak
- Whether two clients can ever write one key at one time
The data is 16 TB, with three copies, and you assume 2 TB of usable disk a machine. How many machines?
- 8 machines, since the data comes to 16 TB
- 16 machines, one for each terabyte stored
- 24 machines, since three copies make 48 TB
- 480 machines, at 100 GB of memory for each
Thirty thousand puts a second go to three copies, and 90,000 gets each ask two. What does one of 24 machines see on average?
- About 3,750 writes and 7,500 reads a second
- About 1,250 writes and 3,750 reads a second
- About 30,000 writes and 90,000 reads a second
- About 3,750 writes and 11,250 reads a second
Which estimate can you say once and drop, because it decides nothing here?
- The 24 machines that 48 TB needs at 2 TB of disk
- The 480 machines that values in memory would need
- The 4.4 hours it takes to move one machine's 2 TB
- The 7.5 MB a second of normal writes on one machine
You got correct