Lesson 2 · Minutes 10-20

Key-Value Store: First Design

Problem

At minute ten you draw one machine, in answer to a question that says distributed. It sounds backwards, and it is the quickest way to say exactly what a confirmed put means, which every later change has to keep or give up out loud. By minute twenty you have walked one put and one get through it, argued that it is correct at its size, and counted the values at which it stops.

The ring and the copies stay off the board for now, because nothing has asked for them yet. The interviewer will: the quiz at the end holds their four probes, and Scaling It takes them one at a time.

A Hash Table and a Log

One machine holds every key and value in a hash table in memory. Beside it, on disk, sits a write-ahead log: a file that only grows at the end, where every change goes before it goes anywhere else. From time to time the machine writes a snapshot of the table, which marks how far back a restart has to read.

The first design
Clientget and put One machinehash table in memory Write-ahead logon disk get put flush, answer readwrite

A Put and a Get, Step by Step

A put arrives. The machine appends the key and value to the log and flushes it, which forces the bytes onto the disk instead of leaving them in the operating system's memory. Then it updates the table, and only then does it answer. Say that order slowly, because it is the durability promise: "A put is confirmed once the log is flushed, and not one step earlier." A crash after the flush and before the answer leaves the client hearing nothing. It retries, the same value goes in twice, and if no other client wrote the key in between, nothing is harmed. A crash before the flush loses a put nobody was told about.

A get reads the table and answers, about 100 nanoseconds of memory. After a restart, the machine loads its latest snapshot and replays the log written since, which is why the snapshot exists: it keeps the replay short.

The Case for One Machine

This design meets both functional requirements exactly. A confirmed put survives a crash, because the log was flushed before the answer. Every read sees the last write, because there is one copy and one order. Hold on to that second promise: the next lesson gives it up, and the deep dives say what buying it back costs.

Now put a size on it. Assume half of one 100 GB machine, which is 50 GB, or 25 million values of 2 KB. A restart reads those bytes back at about 10 GB a second, about 5 seconds of reading, with the table filling as it goes. For that size, where a few seconds of restart is acceptable, nothing more is needed. One engine's log in depth is PostgreSQL Deep Dive's lesson on the write-ahead log.

Nor is any of this thrown away later. This log and this flush become the engine on every machine, and telling the interviewer so now is what makes ten minutes on one machine look like a plan.

Make that case before naming any limit, and then name the one gap against the question yourself: while this machine is down, nothing answers.

Where One Machine Stops

Three limits go on the record, with no fix attached yet. The data: 16 TB is 320 times the 50 GB. The rate: 120,000 requests a second at peak, past one machine's 100,000. The failure: this machine is the one thing that can stop everything, and its disk is the one copy of everything.

Volunteer them as one count and one consequence, in about ten seconds: "One machine works while the data fits half its memory, about 25 million values, and a restart may stop answers for a few seconds. We have 8 billion, and the question says keep answering, so both assumptions go. Where a key lives comes first."

Interview Pitfalls
  • Skipping the one-machine design as too small to say. It sets the two promises, durability and one order, that every later section keeps or loses, and the interviewer never hears them stated.
  • Not flushing before answering, so that confirmed means nothing. The first crash question then has no answer.
  • Calling it a straw man. It becomes the engine on every machine, and saying so earns the time spent on it.
  • Drawing the ring and three copies before anything has forced them. The interviewer cannot tell which failure each one answers.
What Interviewers Listen For
  • Exactly when a put is confirmed, said in so many words.
  • The gap against the question named without being asked.
  • The limit volunteered as a count of values, 25 million against 8 billion.

Knowledge Check

"You have 16 TB and this node holds a sliver of it. How does a get find the right machine, and what moves when you add a machine?"

  • "Hash keys onto a ring with virtual nodes, where a new machine takes one key in 25."
  • "Hash each key modulo the machine count, and let clients recompute on any change."
  • "Keep a table from key to machine on a directory node that every client asks first."
  • "Give each machine a slice of the sorted keys, and split a slice when it fills up."

"One of a key's machines just died, with a put in flight and a get waiting. Who answers each?"

  • "Hold both until a follower has been promoted to take over as primary for that key."
  • "The coordinator retries the dead machine with backoff until it comes back or fails."
  • "The other two copies answer both, and the third copy of the put waits with a hint."
  • "Copy the dead machine's 2 TB to a spare at once, then serve its keys from the spare."

"During a split, two replicas each took a different write to one key, and neither write may be lost. The split heals. What does get return?"

  • "The version with the higher timestamp, since clocks in one region are close enough."
  • "Whichever replica the read reaches first, and read repair copies it to the others."
  • "An error, until an operator looks at both of the versions and picks the winner."
  • "Both versions, told apart by their version vectors, and the next put merges them."

"A replica was down for an hour and it's back. Walk me through how it catches up."

  • "Read repair brings it level as clients read its keys over the following few hours."
  • "The hints held for it arrive first, then Merkle trees find whatever hints missed."
  • "It copies its whole range from a neighbour, which is the simplest and always right."
  • "It stays out of every quorum until an operator has replayed the hour from a backup."

You got correct