BOOK DEEP READ · SRE · CHAPTER 20

Load Balancing in the Datacenter

Site Reliability Engineering · Ch 20 · Google · 2016

中文 →

What is this chapter about?

You tap something in an app and the request lands in a datacenter. Inside, maybe a thousand machines are running the exact same program — so which one gets your request? The previous chapter was about which building the request enters. This one is about what happens once it is inside: picking one machine out of a pile of identical machines, and on what basis.

A strange thing first

At the supermarket you naturally join the shortest queue. But suppose one cashier's terminal is broken, so every customer is bounced back in a second with "payment failed, next please" — that queue is always the shortest. Everyone piles into it, and nobody gets checked out.

This happens in datacenters every day: pick the machine with the fewest jobs in hand, and the broken one looks permanently idle.

So why not just take turns?

One each, in rotation — sounds perfectly fair. Two problems.

Jobs differ wildly in size. One customer buys a bottle of water, the next wheels up a full trolley. Counting items is not counting work.

Cashiers differ wildly in speed. Machines in the same room are of different vintages, and other programs next door are competing for resources. Same ten customers: one cashier finishes in ten minutes, another takes half an hour.

So one queue always ends up much longer. And you have to staff for the most crowded queue — which means paying for people who sit idle.

Three moves

One: don't say hello to everyone. A thousand callers times a thousand machines is millions of open lines; both ends pay memory, and each line needs a periodic "are you still alive?". So each caller knows only a few dozen of them. And the picking matters: pick at random and some machines get known by crowds while others get nobody; assigning by rule is what makes every machine known by the same number of callers.

Two: let the machine hold up its own sign. Don't guess how busy it is from outside — have it report its own busyness on the way back, riding along with the reply, costing no extra trip. Whoever reports idle gets more. Of the three approaches, this one works best.

Three: whoever is going off shift puts up a sign first. A machine that is about to restart shouldn't just switch off the lights and walk out — the customers in its hands get dropped. The right move is to post "this lane takes no new customers; those already in line will be served", then leave once the line is empty. The book calls that sign lame duck. Without it, a routine restart means a batch of users see errors on the spot.

What it buys you

The coarse decision was already made at the door (which building); inside there is only one job left: pick one of a pile of identical machines, and the basis for picking must come from the machine itself, not from guesswork outside.

The honest cost: a machine's self-reported busyness describes the little slice of time that just passed — when load spikes suddenly, the caller's information is always half a beat behind.

One line to remember

Load balancing inside a datacenter is a decision the caller makes for itself: first shrink the set of machines you keep in touch with, then let the machines report their own busyness to divide the work, and finally make a departing machine post its "no new customers" sign. The most counter-intuitive part: "fewest jobs in hand" does not mean idlest — it may mean broken.

Want the actual mechanisms, algorithms and diagrams? → switch to the deep read