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 5 of 9 · Slack

The shard key that lied: why a correct rule went wrong on its own

Chapters

Narration

You open Slack and send a message. A moment later it is saved in a database. But Slack is far too big for one machine, so the data is split across many, and your message must live on exactly one of them. Which one? Slack's early answer was simple. Ask which company you belong to. That answer was right for four years. Then nobody changed it, nothing broke, and it became the wrong answer anyway. So how can a correct decision go wrong all by itself? Why should you care? Every system that splits its data must pick a rule for it on day one, and that rule is very expensive to change later. Slack needed three years to change theirs, with the product running the whole time. So here is the question for the next few minutes. Why did a correct rule go wrong, and what should it have promised? Here is the plan. First, the obvious fix. Second, what the rule really is. Third, the moment it broke. And fourth, the new rule and what it cost. Stop one, the obvious fix. Picture each machine as a locker. Slack calls each locker a shard. A shard holds whole companies, and companies are not the same size. As an example, call a quiet shard's load one. One shard ran five to ten times busier than that. Five to ten times. Meanwhile the others sat idle. The obvious fix is to split that big company across two lockers. Here is the cost. The rule says, ask which company, get one locker. Two lockers would mean two answers. So a company cannot be split. And Slack wrote down the scary question. What if one company does not fit even our biggest shard? Stop two, what the rule really is. The rule is a question you ask about every row of data. Which company is this message from? The answer is a company number, called team I D. The thing you ask about is called the shard key. Now the working, with an example. Take company forty two. A small directory says, company forty two lives in locker three. So every message, channel and member of company forty two goes to locker three. One lookup, one locker. Everything a company owns sits together, so a question about one company touches only one shard. That was a good rule. But a rule that works also makes a promise. This one number did three jobs at once. It said where the data lives. It said who may see it. And it said whose name everything belongs under. Three jobs, one hat. Underneath, it promised something else. A company is an island. Everything it owns lives together, and nothing crosses the water. While that stayed true, the rule was perfect. A shard key is a promise about the product, frozen into the database. Stop three, the moment it broke. Then the product changed. Enterprise Grid let one company have many workspaces. So which number picks the locker, the company or the workspace? Slack Connect let one channel be shared by two companies. So which company's locker holds it? There is no good answer, because the island is gone. Notice what failed. Not the disks, not the servers. The product changed the promise, and the key kept making the old one. Nothing broke, so nothing warned them. Stop four, the new rule and its price. In fall twenty sixteen they had three doors. One, move to a different kind of database, and rewrite every query, giving up transactions on the way. Two, shard inside the application code, which leaves failover and backups just as sick. Three, put a routing proxy in front of plain MySQL. They chose the proxy, because of one diagnosis. The engine was not sick. The layer that manages it was. How the proxy routes is a story for another day. With the proxy they could shard by anything, so they chose the channel. The new rule asks, which channel is this message in? Here is the arithmetic, as an example. A company has three hundred channels, and there are four shards. Spread by channel, that is three hundred divided by four, about seventy five channels on each. Seventy five. Under the old rule, all three hundred sat on one shard. Then Slack copied data to the new shards while the old ones kept serving, and flipped over with a few seconds of read only. It began in December twenty seventeen, and took three years to reach ninety nine percent. There is a catch, and Slack found it in October twenty twenty two. A better key does not abolish hot shards. One customer deleted thousands of users at once. Each deletion started a job, and every job wrote to the same shard, one holding six percent of the data. That shard fell an hour behind, its main machine ran out of memory and died, and the replacement could not catch up either. So a shard key protects you from average load. It cannot protect you from a burst that lands in one place. Three things to keep. One. A shard key is a promise about your product, frozen into your data. Two. Team I D promised that a company is an island, Grid and Connect broke that promise, and the key became the channel. Three. A good key protects against average load, never against a burst that lands on one shard. The key that was right for four years was never wrong about the database. It was wrong about the future. So choosing a key is a bet on a roadmap you have not written yet, and the best key is the one that survives it.

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