BOOKS DEEP-READ · SRE · CH 23

Managing Critical State: Distributed Consensus for Reliability

Site Reliability Engineering · Ch 23 · Google (Laura Nolan et al.) · 2016

中文 →

What is this chapter about?

Name one person on call and the work gets done. Let two people each believe they're in charge and everyone starts overwriting everyone else. A pile of machines faces that question all day long: which one is the primary? Who holds the lock right now? SRE Chapter 23 is about exactly this — how a group of machines reliably agrees on one story.

A strange thing first

In October 2018, GitHub lost connectivity between its East Coast network hub and its primary East Coast data center for 43 seconds. Forty-three seconds — and the site was degraded for a full day. During those 43 seconds the East and West Coast databases each accepted writes; once the link came back, both sides held data the other did not, and neither could simply overwrite the other. Forty-three seconds of not-talking bought 24 hours of manual repair.

Why the old way is hard

The folk remedy: the primary shouts "still alive" every second, and the standby promotes itself after a few silent seconds. It sounds reasonable, but it has a dead end — silence doesn't tell you whether the other side died or the phone line was cut. If it was only the line, the other side is perfectly healthy, and the moment you promote yourself there are two people in charge. That's split-brain. Worse, the folk remedy looks completely fine on ordinary days and only shows its teeth on the day you least want it to.

The reliable way: everything needs a majority

Say you have five machines and every decision needs three votes. Here's the trick: two groups can never each collect three votes at the same time. Three plus three is six, which is more than five, so some machine would have to be counted twice — and it will not cast the same vote for two contradictory things. "Two people in charge" is ruled out at the root. When the network splits, the smaller side simply stops working — a pause you can recover from, unlike diverged data.

The second trick is keeping one shared ledger: machines don't each keep their own books. Every change is copied into the same ledger in exactly the same order. Same entries, same order — replay them and every machine necessarily lands on the same answer.

So what should you do?

The good news: this has been written and beaten on for twenty years — Google's Chubby, and the open-source ZooKeeper and etcd (the Kubernetes you use keeps its entire cluster state in etcd). The most practical line in this chapter is: don't build your own. You think you're writing "a simple primary failover"; you're writing a consensus algorithm, and you're probably writing it wrong.

The honest cost: every decision waits for a majority to nod, so it is inherently slower than one machine, and slower still the farther apart the machines sit — a round trip across a continent costs a hundred-odd milliseconds at the speed of light.

One line to remember

When a group of machines must agree on "who's in charge and what the state is," the only reliable recipe is a majority vote plus one shared ledger; heartbeats-and-timeouts will split-brain sooner or later. And this is a thing you take off the shelf, not write yourself.

Want the mechanisms, the timing diagrams and the selection tables? → Switch to the deep read