Slack's real-time path
Read first: Networking for request paths, Partitioning and sharding
Slack's send path writes MySQL before it broadcasts on the websocket, so the database sits on the user's clock. They shard messages by channel id, moved query load to Vitess starting in 2017, and published a peak of 2.3 million queries per second with a median of 2 ms and a p99 of 11 ms. Channel servers are in memory, on the order of 16 million channel ids per host, and a dead one can be replaced in under 20 seconds.
A Slack message is persisted before it is broadcast on the websocket. That one ordering puts MySQL on the send path, which is why the database has to be fast, and it is why the live fanout is allowed to be an in-memory process you can replace.
The problem
A chat product has two copies of a message in play. One is the durable history. One is the set of sockets that should see the message now. If you tell the sockets first and the durable write then fails, people saw a message that history does not contain. If you write history first and then tell the sockets, the failure that remains is “not on the socket yet,” and a client can catch up from the store.
Slack took the second order. The persist is on the send path, so the sender waits for the database. Everything else in this page is how they made that wait short, how they spread a large customer’s channels across shards, and how they keep the live fanout from becoming the system of record.
There are two writeups in the notes, and their numbers do not belong on one dashboard. The query rates and the latencies are from the Vitess writeup. The channel-server counts and the replacement time are from the messaging writeup. Label them when you quote them.
The design
The database on the send path
MySQL has been the storage engine from the start. They ran active-active MySQL, then migrated to Vitess beginning in 2017. By the time of that writeup, Vitess served 99% of query load, and they expected to finish by the end of 2020. The span from the beginning of that migration to the end of 2020 is the window they described. These notes do not date the writeup itself, and they do not confirm the day the last percent moved. Say 99% at the time of the writeup, and the expectation. Do not announce a finish date you do not have.
Active-active serves writes in more than one region and then has to reconcile conflicts. That is what they ran on MySQL before the Vitess migration. It is a different sentence from the cellular design further down, which keeps a service call inside one zone. Do not merge them into a single timeline. The migration date for the cells is not in these notes.
Peak in the Vitess post: 2.3 million queries per second, about 2 million reads and 300,000 writes, median 2 ms, p99 11 ms. The split is published with “about.” Add the two figures anyway, because the rounded split lands on the peak:
about 2,000,000 reads
about 300,000 writes
sum 2,300,000 queries per second, the published peak
2,000,000 / 2,300,000 = about 0.87 of the peak is reads
300,000 / 2,300,000 = about 0.13 of the peak is writes
p99 / median = 11 ms / 2 ms = 5.5
p99 means about 1 in 100 queries is slower than 11 ms. The median is 2 ms. These are query latencies from that post, not a measurement of the user’s end-to-end send. The sender still waits on the persist, so this query time is part of the send and the rest of the path is extra. Do not tell an interviewer the message appears in 2 ms. Tell them the database query is that fast, which is why putting the persist on the send path is survivable.
A sketch of concurrency, using Little’s law, shows what “fast” still costs in in-flight work. Average concurrency equals arrival rate times average latency. The published latency here is a median, and Little’s law wants a mean. The published rate is a peak. Using them together is a sketch, and the writeup does not say the peak and the median were measured at the same moment.
sketch, not a pool size
arrival rate stand-in = 2.3 million queries per second
latency stand-in = 0.002 seconds, the median
in-flight = 2.3e6 * 0.002 = 4600
if every query took the p99
in-flight = 2.3e6 * 0.011 = 25300
Thousands of queries are in flight at that peak even when each one is a few milliseconds. The p99 line is what the same arrival rate would hold if the whole fleet slowed to the tail. It is a bound on the sketch, not their thread-pool configuration.
Shard by channel
The sharding lesson they published: do not put every channel of a large workspace on one shard. Shard messages by channel id, so one big customer spreads out. A workspace is the tenant. A channel is the conversation. A large workspace has many channels. A shard key of workspace id pins all of that customer’s messages to one shard. A shard key of channel id puts different channels on different shards.
A single busy channel is still one key. More shards do not split it. A hot key is not fixed by more partitions, which is the limit of the key they chose. History for one channel stays together, which is what you want when someone opens that channel and reads a range. You paid for that locality with a hot shard when one channel is the hot key. These notes do not describe a specific Slack hot-channel incident. State the limit. Do not invent the page.
Do not equate the 300,000 writes per second with 300,000 messages per second. A message persist is on that write path. A message may be more than one query. The published number is query writes.
Sockets, channel servers, presence
The real-time path splits the live work away from MySQL. Channel servers are in memory. Channels are assigned to them by consistent hash. At peak, a host holds on the order of 16 million channels. “Channel” in that sentence is an id for a channel, a user, a file, or a similar entity. It is not 16 million chat rooms, and it is not 16 million copies of message history. History is in MySQL, because the message was persisted. The in-memory server is the live fanout for those ids.
Gateway servers hold the client’s websocket and the subscriptions. They sit in several regions so the client connects nearby. The channel server is wherever the hash placed that channel, which may not be the gateway’s region. The socket is placed for the client. The channel is placed for ownership and balance. The path between those two placements is the cost of splitting them.
Admin servers sit between the web app and the channel servers. A realtime client and a web app are different doors into the same live tier. Envoy is the edge proxy in front of this path. Consul holds discovery. Consul uses Raft, so the discovery record is a consensus system’s record, which is the consensus lesson and not a Slack-specific protocol. These notes do not describe their Consul deployment. The name and the job are the facts.
Presence servers track who is online. The client only subscribes to the people currently on screen. A presence update fanned out to an entire large workspace would be a fanout the size of the company. The subscription is the size of the viewport. Someone off screen is not in that subscription. That is a product rule that keeps the live path bounded.
CHARMs rebuild the hash ring and can replace a dead channel server in under 20 seconds. Users of the channels that moved see a short latency blip. The notes say that about the channels that moved. They do not say the rest of the host’s users blip. The ring changes, those channels get a new owner, and the replacement is under 20 seconds. The message that already finished its persist is in MySQL. The channel server was not the system of record.
flowchart LR
client[Realtime client]
envoy[Envoy]
gateway[Gateway]
channel[Channel server]
db[MySQL]
webapp[Web app]
admin[Admin server]
client --> envoy --> gateway --> channel
webapp --> admin --> channel
channel --> db
Read the arrows as placement, not as a private RPC trace. The client reaches a nearby gateway, which holds the socket. The web app reaches an admin server on the way to the channel servers. The message is persisted to MySQL before it is broadcast on the websocket. These notes do not name which process issues the insert. They do say the channel server is in memory, the gateway holds the socket, and MySQL is the durable copy. If the sender’s process dies after the persist and before the broadcast, history has the message and the live sockets do not yet. Clients catch up from the store. The failure they refused is the other order: a socket that already showed a message history does not have.
Calls stay in one zone
A later cellular design keeps service calls inside one availability zone, so a failure does not fan across zones. That is the same idea as a cell: a slice of the stack that serves its own traffic, with the failure staying inside the slice. Slack’s published version of the idea is the zone boundary on the call. These notes do not give the migration date. Do not supply one.
A cell gives up an easy pool. Capacity in another zone is not a free spare for this call, because using it would let the failure cross. Active-active MySQL, earlier, serves writes in more than one region and then reconciles. You can retell both. You cannot draw them as one design.
| Piece | What it holds | What it does not hold |
|---|---|---|
| MySQL, later mostly Vitess | The persisted message | The user’s socket |
| Gateway | The websocket and the subscriptions, near the client | The shard of record for the message |
| Channel server | Live state for ids assigned by consistent hash, on the order of 16 million per host | Durable history |
| Presence server | Who is online, for the people currently on screen | A subscription that covers the whole workspace |
| Admin server | The path from the web app to channel servers | The client’s socket |
| One zone, in the cellular design | The service calls for that slice | A free cross-zone pool for those calls |
What they traded
| Choice | What they gained | What they spent |
|---|---|---|
| Persist, then broadcast | A client does not see a message that history lacks | Send latency includes a database write |
| Shard messages by channel id | A large workspace spreads across shards, and one channel’s history stays together | One hot channel is still one shard |
| In-memory channel servers | Live fanout without a database read for every recipient | Process death drops live state; replacement is under 20 seconds and a blip for the channels that moved |
| Gateways in several regions, channels on a hash ring | The socket is near the client, and ownership is balanced | Gateway and channel server may be far apart |
| Presence only for people on screen | Fanout stays the size of the viewport | Someone off screen is absent from that subscription |
| Service calls stay in one zone | A zone failure does not fan across zones | Other zones are not a free spare for the call |
The first row is the one a chat interview is for. You accept a slower send so the durable copy exists before anyone is told. The Vitess numbers are how they kept that slower send small: median 2 ms and p99 11 ms at a peak of 2.3 million queries per second, with the caveats above about what those numbers measure. A design that broadcasts first has a nicer send latency and a class of ghost messages. They refused the ghost messages.
The Vitess migration is the other trade on the same table, stretched over years. MySQL stayed the engine. Vitess took 99% of query load by the writeup, starting from a migration that began in 2017, with an expectation of finishing by the end of 2020. Sharding was the point of the move. The shard key they tell you to use is the channel id.
What to steal
Steal the order. In a chat design, the send path writes the message, and only then does it fan out on the sockets. Say what the remaining failure is: persisted, not yet broadcast, caught up from the store. Say what you refused: broadcast, then a failed write.
Steal the shard key. Shard by the id that spreads a large tenant. For this product that id is the channel. Name the hot key that remains, which is one very busy channel, and say more partitions will not split it.
Steal the split between the socket and the record. A gateway holds the connection, because a websocket is stateful and the client should attach nearby. A channel owner holds the live subscriptions for an id. The database holds the message. When the in-memory owner dies, you rebuild the ring and accept a short blip, which they bounded by “under 20 seconds” for the replacement. You can describe that recovery without knowing the internal name CHARM. Use the name when you are retelling their system.
Steal the presence bound. Subscribe to the people on screen. A presence stream for every member of a large workspace is a fanout you chose, not a law.
Steal the cell as a blast-radius tool when the question is a failure that fans out. Calls stay in one zone. You say that you gave up cross-zone pooling.
Leave the scale. 2.3 million queries per second, 16 million ids on a host, and a Vitess fleet at 99% of query load are their published peaks and ratios, not a target for a new system. You start on one MySQL with a channel id you could shard by later. You do not start on Vitess, and you do not start active-active because they once ran it. A five-person chat system copies persist-then-broadcast, the channel shard key, and a single process that holds sockets until you have measured a reason to split the gateway from the channel owner. The under-20-second replacement is a story about a ring you can rebuild. It is not a capacity plan.
In five minutes: persist before broadcast; shard by channel, not by workspace; peak from that writeup is 2.3 million queries per second, about 2 million reads and 300,000 writes, median 2 ms, p99 11 ms; channel servers in memory on a consistent hash, on the order of 16 million ids per host, replaced in under 20 seconds with a blip for the channels that moved; presence is who is on screen; a later cell keeps calls in one zone, and you do not have the migration date.
Check yourself
Add the published read and write peaks from the Vitess writeup. What do you get, and what does the word about stop you from over-claiming?
About 2 million reads plus about 300,000 writes is 2.3 million, which is the published peak. The word about means the split is rounded, even though the rounded figures add to the peak exactly. About 87 percent of that peak is reads and about 13 percent is writes. Those are query rates. They are not a count of messages per second, because a message persist can be more than one query. The median is 2 ms and the p99 is 11 ms, so the tail is 5.5 times the median, and about 1 in 100 queries is slower than 11 ms.
Sketch in-flight queries at the published peak using Little's law and the median latency. Which two substitutions did you make?
Concurrency is arrival rate times latency. Using the peak of 2.3 million queries per second and the median of 2 ms, which is 0.002 seconds, gives about 4600 in flight. Using the p99 of 11 ms instead gives about 25300. You substituted the median for the mean, and you treated the peak as if it were the arrival rate at the moment of that median. The writeup does not say those were simultaneous. The result is a sketch of thousands of in-flight queries, not a published pool size.
A large workspace has many channels. Why is the message shard key the channel id, and what does that key still refuse to split?
Every channel of a large workspace on one shard pins that customer to one place. Channel id spreads the workspace across shards and keeps one channel’s messages together for a history read. One busy channel is still a single key. Adding shards does not split a hot key. These notes do not describe a particular Slack incident of that hot channel. The limit is the key they chose.
A channel server dies after a message has been persisted. What does the user feel, how soon can a replacement exist, and why is the message still in history?
CHARMs rebuild the hash ring and can replace the dead channel server in under 20 seconds. Users of the channels that moved see a short latency blip. The message is in MySQL because the send path persists before it broadcasts. The channel server is in memory and is not the system of record. A crash in the gap after the persist and before the broadcast leaves history ahead of the sockets, and clients catch up from the store.
What does the cellular design refuse to let a service call do, and how is that different from the active-active MySQL they ran earlier?
The cellular design keeps service calls inside one availability zone so a failure does not fan across zones. Capacity in another zone is not a free spare for that call. Active-active MySQL serves writes in more than one region and then has to reconcile conflicts. They ran that on MySQL before the Vitess migration, which began in 2017. The cell is a later published idea, and these notes do not give its migration date. Retell them as two choices. Do not draw them as one architecture.
Based on: Slack Engineering, Vitess migration writeup; Slack Engineering, real-time messaging writeup.