Slack System Design
Routing every message to every subscriber worldwide in 500 ms
By Ankur NayakReviewed
How Slack delivers a message to everyone in a channel, anywhere in the world, in about 500 ms: stateful Channel Servers on a consistent-hash ring, Gateway Servers in multiple regions holding WebSockets, an edge cache that makes clients boot fast, and MySQL sharded with Vitess serving 2.3 million queries a second.
Open in Studio· edit a copy- Users
- Tens of millions of connected clients
- Throughput
- 2.3M DB queries/s peak (2020)
- Delivery worldwide
- ~500 ms
- Channels per server
- ~16M at peak
- DB latency
- 2 ms median · 11 ms p99
- Edge cache
- 4M connections
Requirements
Functional
- Post messages to channels and DMs and deliver them to every online member
- Show typing indicators and presence (who is online)
- Boot a client quickly, even in workspaces with tens of thousands of users
- Store message history and workspace data durably
Non-functional
- Deliver messages worldwide in well under a second
- Recover from a server failure in seconds
- Survive a regional failure by moving users elsewhere
- Absorb reconnect storms and top-of-the-hour spikes
In depth
Half a second, anywhere in the world
When someone posts in a Slack channel, everyone in that channel who is online should see it almost immediately, whether they are in the same office or on another continent. Slack's 2023 post on real-time messaging says its system delivers messages across the world in about 500 ms. Two earlier posts fill in the rest of the picture: how the database tier moved to Vitess (2020) and how an edge cache called Flannel made clients start fast (2017).
The cast of servers
Slack's real-time system is built from a handful of server types, each with a clear job:
- Webapp: the Hacklang codebase that hosts the APIs Slack clients call, from posting a message to the boot call that starts a session.
- Admin Servers: stateless servers between Webapp and the Channel Servers.
- Channel Servers: stateful, in-memory servers that each hold recent history for the channels mapped to them. At peak, about 16 million channels are served per host.
- Gateway Servers: stateful servers that hold each user's information and WebSocket channel subscriptions. They are the only service deployed across multiple geographic regions.
- Presence Servers: in-memory servers tracking who is online, behind the green dots; users are hashed to individual Presence Servers.
- CHARMs (consistent hash ring managers) manage the ring that maps channels to Channel Servers, with Consul for service discovery and Envoy at the edge for TLS termination and load balancing.
Connecting
When the Slack client starts, it calls Webapp for a user token and WebSocket connection details. It then opens a WebSocket to the nearest edge region, where Envoy forwards it to a Gateway Server. The Gateway fetches the user's information and channels, sends the first message to the client, and subscribes, asynchronously, to the Channel Servers that hold those channels. See WebSockets.
Sending a message
The write path is short:
- The client calls the Webapp API to post the message.
- Webapp sends it to an Admin Server.
- The Admin Server looks up the channel's Channel Server on the consistent hash ring and routes the message to it.
- The Channel Server sends it to every Gateway Server, anywhere in the world, that is subscribed to the channel.
- Each Gateway Server sends it to every connected client subscribed to that channel.
This is a classic fan-out with two levels: one Channel Server to many gateways, each gateway to many clients. See fan-out and consistent hashing.
Transient events, such as typing indicators, travel a slightly different path and are never persisted: the client sends the event over its WebSocket to its Gateway, which routes it by consistent hash to the channel's Channel Server, which broadcasts it to the subscribed Gateways.
Failures
Because Channel Servers are stateful, losing one matters. When a host fails, its slice of the ring is reassigned and a new Channel Server is ready to serve traffic in under 20 seconds; users of the affected channels see elevated delivery latency for that window. For bigger problems, Slack has a draining mechanism for region failures that moves users in a bad region to the nearest good one.
Load has shapes, too. The post notes spikes at the top of every hour from reminders, scheduled messages and calendar events. And when a whole office loses its network and reconnects at once, every client boots at the same moment.
Flannel: booting at the edge
That last problem is what Flannel addresses. Slack's 2017 post describes it as an application-level caching service deployed to edge points of presence. When a client starts, Flannel provides the users, channels and bots it needs from its cache, returns a much smaller startup payload, and lets the client lazily load the rest. Users from the same team in the same networking region are directed to the same Flannel instance by consistent hashing, so the team's data is cached once.
The effect was dramatic for large teams: the boot payload shrank 7 times on a 1,500-user team and 44 times on a 32,000-user team. Flannel served 4 million simultaneous connections and 600,000 client queries per second at peak, and because new or recently disconnected users are served from its cache, reconnect storms barely reach the backend.
Storage: MySQL, sharded with Vitess
From the start, Slack stored everything in MySQL. Originally, data was partitioned by workspace ID, with each shard a pair of MySQL servers in different data centres replicating to each other asynchronously in an active-active setup. That design hit scaling limits and hot spots, and a shard outage was a full outage for every workspace on it.
Slack's 2020 post explains the move to Vitess, which shards MySQL behind a routing layer. Slack ruled out NoSQL and NewSQL databases in favour of keeping MySQL's years of operational reliability. Vitess also made more flexible sharding possible, notably sharding message data by channel ID instead of workspace. The migration began in 2017; by 2020 Vitess served 99% of Slack's query load, peaking at 2.3 million queries per second (2 million reads, 300,000 writes) with a median latency of 2 ms and a p99 of 11 ms, across six regions. See database sharding.
The trade-offs
Slack's design keeps channels in one place and gateways near users. A stateful Channel Server per channel gives one point to order and broadcast events, at the cost of ring management and fast replacement. Gateways in many regions keep WebSockets short, but messages cross regions to reach them. Keeping MySQL through Vitess preserved hard-won operational experience at the cost of a multi-year migration. And an edge cache made huge workspaces fast to load, adding another stateful tier to keep consistent.
See how Discord approaches the same problem in Discord vs Slack.
What to take into an interview
- Give each channel one in-memory owner, assigned by consistent hashing, and plan for replacing it in seconds
- Put connection-holding gateways near users; subscribe them to channels
- Treat typing and presence as transient: route them, don't store them
- Cache boot data at the edge and lazy-load the rest
- Shard by the key your hottest queries use; for messages, the channel
Components
Slack Client
Client. Desktop, mobile or web app. Boots through the API, then keeps a WebSocket open to the nearest edge region.
Envoy Edge
EnvoyLoad balancer. Terminates TLS and load-balances; forwards client WebSocket connections to Gateway Servers.
Flannel Edge Cache
Cache. An application-level cache in Slack's edge points of presence. It keeps users, channels and bots for each team in memory and serves a slimmed-down startup payload, lazily loading the rest.
Why: Booting a client used to mean downloading the whole workspace; at the edge, a cache shared by the team makes it fast and protects the backend from reconnect storms.
Webapp API
HackAPI gateway. The Hacklang codebase hosting the APIs that Slack clients call: posting messages, fetching history, and the boot call that returns a token and WebSocket setup information.
MySQL via Vitess
VitessSQL database. Slack's data store: MySQL sharded by Vitess, with message data sharded by channel ID. Migration began in 2017; by 2020 Vitess served 99% of query load across six regions.
Building block: Database Sharding, Online Migrations
Admin Servers
Service. Stateless servers between the Webapp backend and Channel Servers. They look up a channel's Channel Server on the hash ring and route the message to it.
Channel Tier
CS ringRealtime gateway. Stateful, in-memory servers each holding recent history for the channels mapped to them by consistent hashing; about 16 million channels per host at peak. A CS sends each message to every Gateway Server subscribed to the channel.
Why: Owning a channel in one place gives a single point to order and broadcast its events.
Building block: Consistent Hashing, Fan-out on Write vs. Read
CHARMs
Coordination. Consistent hash ring managers: maintain the ring that maps channels to Channel Servers.
Consul
Service discoveryCoordination. Channel Servers register here; Admin and Channel Servers query it for the current ring configuration.
Channel Server Host
Realtime gateway. Serves the channels hashed to it, in memory.
Replacement Host
Realtime gateway. Takes over a failed host's slice of the ring; ready to serve in under 20 seconds.
Gateway Servers
Realtime gateway. Stateful servers holding each user's information and WebSocket channel subscriptions. The only service deployed across multiple geographic regions; they send each event to every subscribed client connection.
Building block: WebSockets & Persistent Connections
Presence Servers
Cache. In-memory servers tracking which users are online, powering the green presence dots. Users are hashed to individual Presence Servers.
Request flows
Post a message to a channel
- Alice posts a message; the client calls the Webapp API.
- The message is stored in MySQL through Vitess, sharded by channel ID.
- Webapp hands the message to an Admin Server.
- The Admin Server finds the channel's Channel Server on the consistent hash ring.
- The Channel Server sends it to every Gateway Server, in every region, subscribed to that channel.
- Each Gateway Server pushes it to its connected clients in the channel: around 500 ms end to end.
Start Slack and connect
- On boot the client fetches its token and WebSocket setup information from Webapp.
- It opens a WebSocket to the nearest edge region; Envoy forwards it to a Gateway Server.
- The Gateway Server fetches the user's channels and subscribes to the Channel Servers that hold them.
- The user is marked online with the Presence Servers.
Show a typing indicator
- Bob starts typing; the event goes over his WebSocket to his Gateway Server.
- The Gateway routes it by consistent hash to the channel's Channel Server. Nothing is written to the database.
- The Channel Server sends it to every subscribed Gateway, which broadcasts it to the channel's clients.
Failure scenarios
What if: a region's gateways fail?
Gateway Servers run in several regions. When one region fails, a draining mechanism moves its users to the nearest healthy region, where they reconnect and resubscribe.
User impact: A reconnect for users in that region.
- Multi-region gateways
- Region draining
- Edge cache absorbs the reconnect storm
What if: a Channel Server dies?
Its channels are remapped on the hash ring to a replacement, which is ready to serve in under 20 seconds.
User impact: Delayed delivery in the affected channels for under 20 seconds.
- Consistent hashing
- CHARMs manage the ring
- Fast replacement
What if: a whole office reconnects at once?
When many clients reconnect together, each boot would hit the backend. Flannel serves new and recently disconnected users from its edge cache, so the backend sees far less of the storm.
User impact: Slow starts without the cache; quick starts with it.
- Edge cache per team
- Lazy loading of users and channels
Trade-offs
Stateful Channel Servers on a hash ring
Assign each channel to one in-memory server by consistent hashing.
- Pros
- One owner orders and broadcasts each channel's events
- Only a slice of channels moves when a host fails
- Cons
- Stateful servers need ring management and fast replacement
Gateways near users, channels in one place
Deploy Gateway Servers in many regions; keep Channel Servers central.
- Pros
- Short WebSocket hops for users
- Regions can be drained independently
- Cons
- Every message crosses regions to reach far-away gateways
Vitess instead of a new database
Keep MySQL and shard it with Vitess rather than move to NoSQL or NewSQL.
- Pros
- Years of MySQL operational experience kept
- Flexible sharding (by channel for messages)
- Cons
- A multi-year migration (from 2017)
An application-level edge cache
Build Flannel to serve boot data and lazy loads from the edge.
- Pros
- Fast starts for huge workspaces
- Absorbs reconnect storms
- Cons
- Another stateful tier to keep consistent with the backend
Frequently asked questions
How does Slack deliver messages in real time?
The client posts through the Webapp API, which hands the message to an Admin Server. The Admin Server finds the channel's Channel Server on a consistent hash ring, and that server broadcasts it to every Gateway Server subscribed to the channel, which push it over WebSockets. Slack says this takes about 500 ms worldwide.
What database does Slack use?
MySQL, sharded with Vitess. Slack began migrating in 2017 from workspace-sharded MySQL pairs; by 2020 Vitess served 99% of its query load, peaking at 2.3 million queries per second with a 2 ms median latency.
What is Slack's Flannel?
Flannel is an application-level cache running in Slack's edge locations. It caches each team's users, channels and bots, returns a small startup payload to clients and loads the rest on demand. In 2017 it served 4 million simultaneous connections.
What happens when a Slack Channel Server fails?
Its share of channels is remapped on the consistent hash ring to a replacement server, which is ready to serve in under 20 seconds. Users of those channels see slightly delayed delivery during that window.
Building blocks used
Sources
- Real-time Messaging · Slack Engineering
- Scaling Datastores at Slack with Vitess · Slack Engineering
- Flannel: An Application-Level Edge Cache to Make Slack Scale · Slack Engineering