Why Agreement Is Hard
The moment two machines must agree on one fact, the design inherits a problem with no perfect solution. Which of them is the primary, whether a block of IDs was handed out, whether a transfer committed: each is a fact that every machine must see the same way, over a network that loses messages and between nodes that stop without warning.
Computer Science Deep Dive proves the problem for two parties and one link, in its lesson on the two generals. This lesson takes it to many machines, which is where the interviewer's favourite follow-up lives: "What if that node is not dead, only slow?"
From Two Generals to Many Machines
The two generals can talk only through messengers who may be captured, and no number of messengers makes both of them certain of the plan. The general result is short: no protocol over a link that loses messages ends in certain agreement.
With many machines, the question becomes consensus, and consensus has three parts. Every working node decides the same value. The value is one that some node proposed. And every working node eventually decides. A majority can decide without the rest, which is what makes consensus workable at all; how a majority votes and elects a leader is the next lesson.
Four questions in a design are consensus in disguise: who leads, who holds this lock, which configuration is current, and did this transaction commit. When one of them comes up, the design is about to need a tool from this chapter.
Crashed, Cut Off or Slow
From outside, three very different nodes look the same. A crashed node sends nothing. A node behind a network partition sends nothing that reaches you. A node paused by a long garbage collection, or by its virtual machine being moved to another host, sends nothing until it wakes. All three give the same silence, and the silence is all the other machines have.
It is doing: nothing, for anyone.
A failover: correct, and the sooner the better.
It is doing: serving the clients on its side of the cut.
A failover: a second primary, unless the old one is fenced.
It is doing: nothing, then it wakes sure it still leads.
A failover: two primaries the moment it wakes, unless fenced.
A timeout is the only failure detector the network offers, and it is a guess. Its length is a choice with a cost on each side. Assume a 10-second timeout: a dead primary costs at least 10 seconds without writes before anyone acts. Assume 1 second instead: every pause longer than a second declares a live node dead, a replica is promoted, and the old primary wakes still believing it leads. That is how two primaries happen.
So the timeout is stated as an assumption, with both of its failures. Keep it short while seconds without writes cost more than a rare false failover; lengthen it when false failovers start to show up in the logs, and make sure the old primary can be fenced either way. Failure detection by gossip, with no central monitor at all, is the deep dive of the key-value store in Chapter 10.
The Impossibility Results in One Paragraph
Three results set the limits. Fischer, Lynch and Paterson showed in 1985 that in a fully asynchronous system, where a message may take any time to arrive, every deterministic consensus protocol can fail to finish, even when only one process is faulty. The two generals show that certainty over a lossy link is impossible. And CAP says that during a partition a store chooses between answering and staying consistent, which Chapter 4 states precisely. Agreement among nodes that may lie, the Byzantine generals problem that Computer Science Deep Dive hands to this course, costs more nodes and more rounds of messages. Interview designs assume nodes that crash, not nodes that lie, and one sentence saying so is enough.
What Systems Do Anyway
FLP forbids a guarantee that agreement always finishes. It does not forbid agreement. Real protocols never give a wrong answer, and they stall only while the network misbehaves; the Raft paper sets it as a requirement that safety "must not depend on timing". Three moves keep them moving. Timeouts that are eventually right, so a live leader stops being suspected once the network settles. Randomised timers, so two nodes rarely start an election at the same instant. And a majority, so a minority's silence costs nothing.
The strongest move is a fourth: remove the need to agree. One owner per partition makes "who may write this key" a local fact. An idempotent operation makes "did it already happen" safe to get wrong, because doing it twice is the same as doing it once. Merges that commute, the CRDTs of Chapter 4, let replicas accept writes apart and converge without asking each other. Each one turns an agreement into a local decision.
Spotting Agreement in a Design
The habit to build: for every component on the board, name the fact that exactly one machine may believe at a time, and the tool that guarantees it. The URL shortener's counter service must never hand out a block twice. A message queue's partition needs exactly one leader, the problem of Chapter 14. A wallet's transfer must commit on both shards or on neither, the problem of Chapter 25.
The tools are the rest of this chapter: a consensus service, a lease with a fencing token, a transaction across partitions, or a design that needs none of them. Naming the fact before the tool is what lets you defend the tool. You might say: "The one thing only one machine may believe here is which server leads each partition, so that lives in a consensus service, and nothing else does."
How Interviewers Probe This
The first probe is "Your monitor says the primary is down. Is it?" A good answer says the monitor only knows that a timeout passed, lists what the silence can mean, crashed, cut off or paused, and says what stops the old primary from writing once a new one is chosen: a fencing token, from the lesson on leases.
The second is "Why not let the two database servers decide between themselves which one is primary?" A good answer shows the tie. Cut apart, each server sees a dead peer, and neither can tell which side was cut off. A third voter, a small witness or a third replica, makes a majority, and a majority, not a pair, is the unit of agreement.
The third is "Isn't distributed consensus provably impossible?" A good answer states what FLP rules out, a guarantee of finishing in a fully asynchronous system, and what real systems keep: never a wrong answer, and progress whenever the network behaves.
- "A failed health check means the node is down." It means no answer arrived in time. The node may be serving clients on the other side of a partition, and a failover that assumes it is dead makes two primaries.
- "Two replicas can decide between themselves." Cut apart, each sees the other as dead and neither can break the tie. A third voter, a witness or a third replica, makes a majority.
- "FLP means consensus does not work in practice." It rules out a guarantee that agreement always finishes. etcd and ZooKeeper agree all day, and stall only while the network misbehaves.
- "A longer timeout is safer." It trades false failovers for a longer outage when the node really is dead, and no length removes a pause that runs just past it.
- "Tolerating lying nodes is the stronger choice." It costs more machines and more rounds to answer a threat that a fleet run by one company rarely faces.
- Name the facts that only one machine may hold at a time before you draw the tool that guards them.
- State every timeout as an assumption with both of its failures: the outage when it is too long, the second leader when it is too short.
- Remove the need to agree where the design allows it: one owner per partition, idempotent writes, commutative merges.
- Give every agreement an odd number of voters in separate failure domains, never two.
Knowledge Check
A health check on the primary times out. What does the monitor actually know?
- That no answer arrived in time, which a crash, a cut or a pause all cause
- That the primary process has crashed and can safely be replaced at once
- That the primary is overloaded and will recover once the traffic drops
- That the network is cut between the monitor and every one of the clients
Two database servers watch each other and promote themselves when the peer goes quiet. The link between them is cut. What happens?
- Neither promotes itself, because each waits for the other to confirm it
- The one with the longer log of writes wins, and the other steps down
- Each sees a dead peer and promotes itself, so the pair has two primaries
- Both stop taking writes until an operator restores the link between them
What does the FLP impossibility result rule out?
- Any consensus protocol that stays correct after one process has crashed
- Consensus in practice, which is why stores such as etcd avoid needing it
- Agreement among more than two parties over a network that drops messages
- A guarantee that consensus always finishes in a fully asynchronous system
Which fact in a URL shortener must exactly one machine believe at a time?
- Which number blocks the counter service has already handed out
- Which keys each cache node currently holds in its own memory
- Which web server answered the previous request from one user
- How many redirects the service served over the last minute
Which change removes the need for agreement instead of buying it?
- Shorten the failure timeout so that a dead leader is replaced sooner
- Add a fourth voter so that the cluster can survive one more failure
- Retry every write until a majority of the replicas have confirmed it
- Give every partition one owner, so only that machine writes its keys
You got correct