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

What 'delivered' really means: one Slack message, Enter to every screen

Chapters

Narration

You are in a Slack channel with a hundred people. You type a message and press Enter. Half a second later it is on every screen, in every city. Now the strange part. Say one person closed their laptop ten minutes ago. Their screen is dark, and nothing can be pushed onto it. So was your message delivered to them? And if you say yes, what exactly was delivered? Why does this matter to you? Every chat app, alert system and live dashboard has this problem. Somebody speaks, many people must hear it, and some of them are not there. If a design confuses fast with safe, messages vanish and nobody notices. So here is the question for the next few minutes. What does delivered really mean? Here is the plan. First, the obvious way, one server per company. Second, the ring, where every channel has one owner. Third, one message's journey, written down before anyone sees it. And fourth, the catch, the laptop that was shut. Stop one, the obvious way. Give every company its own message server. One program, that takes each message and shouts it at everyone from that company who is connected. Slack really built it this way. Now the cost. One machine holds one company, so when it dies, that whole company goes silent, with nothing behind it. And it cannot be split. A company with two hundred thousand people is still one company, so it is still one machine. The load follows the org chart, not the hardware. A reconnect stampede after a crash is a story for another day. The fix was not a bigger server. It was to stop making the company the unit of anything. Stop two, the ring. If the company is the wrong unit, what is the right one? The channel. Slack now asks, which channel is this message in? Here is how one channel finds its one owner. 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 called a hash. Now picture a clock face, with the machines standing at different hours. The number points at a spot on the clock, and the next machine along owns the channel. That clock is the ring. Anybody can work out the owner, without asking. Why a clock, and not a simple count? Here is an example. Channel number seven, and four machines. Count off, meaning divide by four and keep the remainder, and seven lands on machine three. Now add a fifth machine, and seven lands on machine two. It moved, though nothing about it changed. Counting off moves about four channels in every five when you add one machine. On the ring, the new machine takes only the slice next to it, and everything else stays put. A channel server holds about sixteen million channels at its peak. And when one dies, its stretch passes to a neighbour in under twenty seconds. A failure now takes a sliver of many companies, not all of one. Stop three, one message's journey. You press Enter. The message goes first to the web tier, a server that checks it and writes it into the database, the permanent record. Only when that write is done does anything else happen. Durable before visible. Next, the web tier asks a helper called the admin server, which channel server owns this channel? The admin server remembers nothing between questions, so any copy can answer. It hashes the channel and points at one machine. That channel server knows which gateway servers are holding a connection to someone in this channel. A gateway server is the machine your laptop is connected to. Your laptop opened that connection when you started Slack, and left it open all day. The channel server pushes the message once to each gateway. Each gateway pushes it down the connections it already holds. One write, then a fan out measured in connections, not in people. The whole trip takes about half a second, worldwide. Now a typing indicator, the three dots. Nobody will ever ask for it again. So it takes a shorter road. From your laptop to the gateway, to the channel server, and out to everyone else's gateway. The web tier and the database are not on this road. It is never written down. Saving every keystroke would fill the record with things nobody reads. The cheapest write is the one you never make. Stop four, the catch. Back to the closed laptop. The push goes out, and lands nowhere. Nothing on the server holds it for later. As far as Slack has published, there is no replay buffer. So how does that person catch up? When the laptop wakes, it reconnects and says, the last message I saw in this channel was this one. Give me everything after it. That number is the message's id. It looks like a clock time, but it is only a name, unique inside one channel, and it doubles as a bookmark. The database holds the truth. The connection is only the fast path. Three things to keep. One. The message is written to the database first, and pushed second. Durable before visible. Two. Every channel has exactly one owner, found by a hash on a ring, so a failure takes a sliver of many companies. Three. The connection is only a fast path, and a shut laptop catches up from the last message id it saw. So delivered never meant it reached your screen. It meant it is written down, and every screen is catching up to that. Which machine holds that written copy is a question for a later episode, and Slack's first answer went wrong.

Based on the board The Message Path.

Covered in Slack, the case study.

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