BOOKS DEEP READ · SRE · CH 19

Load Balancing at the Frontend

Site Reliability Engineering · Ch 19 · Google · 2016

中文 →

What is this chapter about?

You open a global site on your phone — mail, video, a cross-border shop — and the request lands on one particular machine. Why that one? Chapter 19 of Google's SRE book is about exactly that: how traffic gets handed out before a request ever reaches a datacenter. It is not "put a box in front of the building." It is a relay, with three legs.

An analogy

Think of a global restaurant chain. Getting fed takes three steps: ① you ask for directions — "where's the nearest branch?"; ② you drive there, and the forks in the road decide which branch you actually walk into; ③ inside, a host seats you at a table. Each step is a different mechanism, and each one knows different things and reacts at a different speed — which is why none of them can replace the others.

The odd thing about asking directions

You think you're asking head office. You're actually asking the fellow at the end of your street — your phone doesn't query the site directly, it delegates to a middleman. So head office knows where the middleman is standing, not where you are. He may be a hundred miles away, and you get sent to a far-off branch.

Worse, he writes the answer in a notebook and reads it back next time. So if head office wants to close a branch temporarily, it has to wait for every notebook to go stale — this leg is slow, too slow to use in an emergency.

Three moves that fix it

One: have the middleman mention roughly where you live. When he asks, he adds "I'm asking on behalf of someone over on the east side," and head office can point better. The cost: his notebook now needs a separate page per neighbourhood, so it gets fat.

Two: make one street address valid in many cities at once. Everybody worldwide uses the same address and the road network delivers you to the closest branch. Nice: no directions needed, and you can pull a branch out simply by taking its sign down. Not nice: "closest" is measured by the road map, not by how long the drive takes — and if the roads get re-signed halfway, you may be dumped at the same-named branch in another city, and the order you just placed is gone.

Three: seat people by calculation, not by bookkeeping. There's more than one host at the door. If they keep a ledger of "who sits where," handing over to the next host breaks it. Instead, compute a fixed table from your name — whoever greets you computes the same table. No reconciliation between hosts, and losing one host changes nothing. There's a companion move too: don't route the food back past the host's desk. The kitchen carries it straight to your table, because the food going out is far heavier than the order coming in.

So how do you choose?

Coarse decisions go to the directions step (which region); fine decisions go to the door (which machine). The honest cost: computing seats instead of recording them saves the ledger, but when a branch really does open or close, a small share of people get recomputed — and the meal they were in the middle of is interrupted.

One line to remember

Handing out traffic is a three-leg relay: the directions leg (DNS) can only point coarsely and slowly at a region, the road network drops you into some datacenter, and the machine is actually chosen at the door — where the trick is to compute rather than remember, and to keep the return trip away from yourself.

Want the mechanisms, notation and diagrams? → Switch to the deep read