Episode 1 of 9 · Slack
Slack architecture: what one message crosses in half a second
Vertical version.
Chapters
Narration
You are at work, and you type a message in a channel. Say, as an example, two thousand people are in that channel. You press enter. Half a second later, the message is on their screens. Some are on laptops. Some are on phones, in pockets, on another continent. Nobody waited, and nobody refreshed. So what does one message cross to get there, in half a second? Why should you care? A chat that feels slow feels broken. So the whole design is a fight over half a second. Delivery around the world takes about five hundred milliseconds, and every part of the system either spends that time or protects it. Learn this one road, and every later Slack story has a place to hang. So here is the one question for the next few minutes. What does one message cross, in half a second? Here is the plan. First, the one big machine. Second, the road you wait for. Third, who should hear it. And fourth, the road that can wait. Stop one, the one big machine. Picture the simplest design. Each company gets one server. Everyone in the company connects to it, and stays connected. You press enter, and that one machine saves the message, then shouts it to everyone else. It works, for a small company. Here is the cost. One machine holds one company, so when it dies, the whole company goes dark. And it cannot be split, because a company with two hundred thousand people is still one company. Slack outgrew this design, and the road you are about to walk is what replaced it. Stop two, the road you wait for. We will build it one hop at a time. Your app opens one line to Slack, and keeps it open all day. That open line is called a socket. The machine that holds it is called a gateway server. The gateway server does not think about your message. It holds the line open and passes along whatever comes down it. When you press enter, your message goes up that line. That is the first hop. Next it reaches the front door, a huge program called the webapp. About five million lines, written in a language called Hack. The webapp does one thing before anything else. It saves your message in the database. Only when the save has landed does anyone get told. Saved first, told later. That is why you can never see a message that your own company has not safely stored. Stop three, who should hear it. The message is saved. Now someone has to find the people in that channel. Slack gives every channel exactly one owner, a machine called a channel server. How do we know which machine? Take the channel's name and run it through a rule that turns the same name into the same spot on a clock face, every time. That rule is called a hash. The machines stand around the clock like hours, and the spot points at one of them. That machine owns the channel. Anyone can work this out, so nobody has to ask around. A small machine called the admin server does the lookup. At peak, about sixteen million channels are spread over these owners. The owner knows who is subscribed. But it does not hold their lines. The gateway servers do. So the owner hands the message to every gateway that holds a subscribed line. Each gateway then pushes it down the sockets it already has open. Out it goes, to every screen. Now walk the whole road once. Your app, the gateway, the webapp, the database, the admin server, the channel server, the gateways again, and their screens. About five hundred milliseconds, all the way around the world. Now the catch. That road has a toll. Saving first means every message makes a trip to the database before anyone hears it. For a message, the toll is worth paying. For a typing indicator, the little dots that say someone is typing, it is not. Nobody needs those saved. So typing takes a shorter road. Your app, a gateway, the channel server, the gateways, and out. No webapp. No database. The cheapest write is the one you never make. Slack spends the toll only where it buys something. Stop four, the road that can wait. Saving and delivering is everything you wait for. But one message also sets off a dozen smaller jobs. A push notification to a phone. A link preview. An update to the search index. Nobody is waiting for those, so they get their own road, a queue, which is just a line of jobs to do later. Slack's queue carries about nine billion jobs a day. Nine billion divided by eighty six thousand four hundred seconds in a day is about one hundred and four thousand jobs a second. The jobs go into a log called Kafka, and one relay feeds them onward at a rate a person can turn down. So the late road can slow down, and the road you wait for never feels it. Three things to keep. One. A message crosses a road you wait for: a gateway, the webapp, the database, the channel owner, and back out to every screen. Two. Saved first, told later, and each channel has exactly one owner that anyone can find with a hash. Three. Everything that can be late, notifications, previews, search, rides a separate road, so it never slows the first one. That is the map. Every part of it nearly broke once, and the later episodes in this series open them one at a time, from the monolith to the thundering herd of sockets, the shard key, shared channels and search. For now, hold on to the one idea. Do the little that must be true before they see it, and let everything else be late.
Based on the board The Map.
Saved in this browser, and to your account when you sign in.