Episode 9 of 9 · Slack
Slack: what you wait for, and what is allowed to wait
Vertical version.
Chapters
Narration
You are at work, and you type a message in a channel. Good morning. You press enter. On the other side of the world, a colleague's screen shows it. That takes about half a second. Half a second. Think about what has to happen in that time. Your message has to be kept safe. Someone has to work out who should see it. And it has to reach every one of their screens. So here is the question for the next few minutes. What happens in that half second, and what is allowed to wait until later? Why should you care? Even a company with two hundred thousand people must feel this chat as instant. A message that feels slow feels broken. So the whole design is a fight over one thing. What must happen before you see your message appear, and what can happen after? Get that split right, and the product feels instant. Get it wrong, and nothing else you built matters. Here is the plan. First, the slow way, where one server does everything. Second, the path you wait for. Third, the work that is allowed to be late. And fourth, the one idea that holds all of it together. Stop one, the slow way. Picture the simplest design. One server for each company. You press enter, and that server does every job before it answers. It saves your message. It tells everyone in the channel. It sends the push notifications. It builds the preview for any link. It updates the search index and the unread counts. That is a dozen pieces of work for one message. Say, as an example, each takes one hundred milliseconds. A dozen of them is twelve hundred milliseconds. Over a second, before you see anything. And the design has a second flaw. One server holds one company. When it dies, that whole company goes dark. Stop two, the path you wait for. Follow your message. First it reaches the nearest edge. That is a small data centre close to your city, so the first hop is short. From there it goes to the webapp. That is Slack's big front program, the monolith, one large codebase that handles every request. Now the important rule. The webapp writes your message to the database before it tells anyone. The database is called Vitess. It spreads messages across many MySQL machines, keyed by channel. Only when Vitess says stored does the message move on. Why first? Because then you can never see a message that your company has not safely stored. Write first, tell second. Now the message is safe, but nobody has seen it. Someone must work out who should hear. Slack asks one question. Which channel is this? Take the channel's name and run it through a rule that turns the same name into the same number, every time. That rule is a hash. Lay the machines out like hours on a clock, and the number points at one of them. That machine owns the channel. It is called a channel server, and it knows who is listening. Slack has about sixteen million channels at peak, spread around that clock. The channel server hands the message to the gateways. A gateway holds each person's connection open, so it can push the message down the line. Edge, write, find the owner, push. Half a second. Stop three, the work that is allowed to be late. Remember the dozen jobs. The notification, the link preview, the search index. None of them has to finish while you wait. So the webapp does one small thing. In the same write, it drops a job into a queue, a line of work to do later. Slack puts a log called Kafka in front, and one relay lets the jobs out at a rate a person can turn down. Workers then do the slow half. How many jobs? Nine billion a day. A day has eighty six thousand four hundred seconds. Nine billion divided by that is about one hundred and four thousand jobs a second. A hundred and four thousand a second, and not one of them is on your path. Here is the catch. A queue is only safe if it can always drain. Slack's first queue lived in memory, in Redis. Taking a job off a full queue needs memory too, so once it filled, it could not be emptied. In twenty sixteen, that happened. The database slowed, jobs arrived faster than they finished, and the queue stayed dead until people dug it out by hand. The fix was the Kafka log in front, where a backlog is just a longer file on disk. The worst backlog went from ninety minutes to four minutes. Pages fell from seventy one a month to nine. And the price of the late plane? A message can be saved before it is searchable. Durable first, searchable later. Stop four, the one idea underneath. Slack's story had earlier episodes, on the shard key, the thundering herd, shared channels and search. Each of those is its own episode, so we only name them here. What they share is simple. Slack began with one assumption. One company, one space. That company id was the unit of everything. It was the shard key, the permission boundary, and the namespace. Every hard problem since was prying those three jobs apart, while the product was live. So when you design a system, ask which unit your product is built on. Because that unit becomes the unit of your system. Pick the one that survives your roadmap, not the one that fits today. Three things to keep. One. A message has two planes. The path you wait for takes about five hundred milliseconds, and it writes before it tells. Two. Everything allowed to be late goes into a queue, nine billion jobs a day, about a hundred and four thousand a second. Three. The unit of your product becomes the unit of your system, so pick the one that survives your roadmap. Fast chat is not a faster server. It is a sharp line between what you wait for, and what can wait. Draw that line first, and the rest of the system gets room to be slow.
Based on the board Everything.
Covered in Slack, the case study.
Saved in this browser, and to your account when you sign in.