Index

Free, with Google

Keep going, free.

A free account opens lessons 2 and 3 of every track, 33 more lessons, and keeps your progress and review cards on every device.

Start free with Google

A chalk plan

Open everything on chalk.

Every lesson, board, video, lab and paper, and the full review decks. Yearly is $39/year.

See plansStart free instead

Episode 3 of 9 · Slack

The thundering herd: when everyone reconnects at once

Chapters

Narration

It is Monday, nine in the morning. You open Slack, and your messages are there in a moment. Now say your wifi blinks for two seconds. Slack quietly reconnects, and you never notice. Here is the strange part. If four thousand colleagues blink at the same moment, that same quiet reconnect can take the whole service down. Nothing broke. Everybody simply came back at once. So how do you stop everyone returning together? Why should you care? Every app that keeps a connection open has this problem. Slack's worst outages were the same bug wearing a different hat. Everybody arrives at once, with an empty cache. So here is the one question for the next few minutes. How do you stop four thousand clients returning in the same second? Here is the plan. First, what one reconnect costs. Second, the obvious fix, waiting longer. Third, the real fix. And fourth, the catch. Stop one, what a reconnect costs. When you open Slack, before you read one message, the app asks for a full description of your company. Every person. Every channel. Who belongs to which. Engineers call it the boot payload. In Slack's old design it was one call, named r t m dot start. At four thousand people it came to about seven megabytes, and the app needed tens of seconds just to read it. Seven megabytes. Keep that number. One reconnect is not a ping. It is the whole workspace again. Now do the sum, with the same numbers. Four thousand people, seven megabytes each. Four thousand times seven is twenty eight thousand megabytes. That is about twenty eight gigabytes. On a normal morning people open Slack one at a time, so the service sees a trickle. But when the network hiccups, every connection drops together. And every app asks for its whole workspace again in the same second. Twenty eight gigabytes, asked for at once. That synchronised arrival has a name, the thundering herd. The steady load was never the problem. The arrival is. Stop two, the obvious fix. If everyone failing at once is the problem, make them wait. When a reconnect fails, do not try again straight away. Wait one second. If that fails, wait two seconds. Then four. Then eight. The wait doubles every time, which is why it is called exponential backoff. As code, it is one line. Retry in two to the power of the attempt number. Waiting does cut the load. Each round lands further from the last, so the service gets room to breathe between them. But look at the rule. Every client follows the same rule, and every client started failing at the same moment. So every client waits exactly one second. Four thousand retries land together at one second. They fail again, and every client waits exactly two more. Four thousand retries again, at three seconds. The spike has not shrunk. It has moved. Backoff did not spread the herd. It scheduled the herd. Four thousand, then four thousand, then four thousand. Stop three, the real fix. Give every client its own dice. Jitter means a small random shake added to a schedule. Instead of waiting exactly two seconds, each client rolls a number between zero and two, and waits that long. In code, that is one changed line. Now the same four thousand clients leave the same failure, but each rolled a different number. So they do not arrive together. They land spread across the whole window. Nobody had to coordinate. Each client only had to roll its own dice. Now the working, as an example. Say all four thousand clients are still failing. The first window is one second wide, so they still arrive together, four thousand a second. On the second try the window is two seconds wide. Four thousand spread over two seconds is two thousand a second. Next window, four seconds, one thousand a second. Next, eight seconds. Four thousand divided by eight is five hundred a second. From four thousand a second down to five hundred. Same clients, same code, one dice roll each. Stop four, the catch. Jitter fixes the clients' retries, but it is only the clients' half. On the fourth of January, twenty twenty one, everyone came back from the holiday at the same time, with cold caches. At six fifty seven in the morning, errors began to climb. Engineers tried to add twelve hundred servers, but the tool that adds servers hit limits of its own. Recovery took until ten forty. So Slack also works on the server's half. Limit how many cold boots get in, shed the cold, keep the warm. And make each boot cheaper. Their cache near the user, called Flannel, made the payload forty four times smaller for a thirty two thousand person company. Three things to keep. One. A reconnect costs a whole workspace, so four thousand clients at seven megabytes each is twenty eight gigabytes at once. Two. Waiting longer only schedules the herd, because every client waits the same time. Three. Randomness spreads it, because each client rolls its own wait, and the server should limit cold boots too. Recovery is its own workload. The question for anything you build is not only how it behaves under load. It is what happens in the first second after it comes back.

Saved in this browser, and to your account when you sign in.