Sub topic 14
Scaling connection servers and routing messages
This chapter assumes you know how WebSocket connections work, what a chat server does, and how a key-value store or pub/sub system behaves. Earlier chapters covered the architecture and group fan-out. Here the question is how to run many connection servers and get a message to the one that holds the recipient's connection.
Why routing is a problem
A WebSocket connection is stateful. It lives on one server for as long as the client stays connected. When Alice sends a message to Bob, the server holding Alice's connection often is not the server holding Bob's. Something must tell it where Bob is.
One server handles roughly 100,000 to 1,000,000 idle connections, depending on memory and tuning. At 50 million concurrent users you need hundreds of servers, so routing is a normal case.
Scaling the connection tier
Put a load balancer in front of the chat servers. It spreads new connections across them, usually by least connections. Keep the chat servers free of durable state. Messages go to the message store, so a crashed server loses only its open sockets. Clients reconnect and fetch what they missed using their last known sequence number.
When you add servers, existing connections stay where they are. Only new connections go to the new servers. To drain a server before maintenance, stop sending it new connections and close its sockets gradually. Closing all of them at once makes every client reconnect at the same moment.
Keeping a session registry
The registry maps each user to the server that holds their connection. A fast key-value store such as Redis works well.
- A client connects and the server writes
user:bob -> chat-server-17with a TTL. - The server refreshes the entry on each heartbeat.
- On disconnect, the server deletes the entry.
- The TTL removes stale entries if a server dies without cleaning up.
Routing a message
The sender's server looks up the recipient in the registry and forwards the message to that server. If no entry exists, the user is offline and the message goes to push notifications instead.
Choosing a forwarding mechanism
| Approach | How it works | Trade-off |
|---|---|---|
| Direct server-to-server calls | S1 calls S2 using the address from the registry | Low latency, but every server must reach every other server |
| Pub/sub channel per server | Each server subscribes to its own channel. S1 publishes to `server:2` | Simple connectivity, and one broker to scale |
| Pub/sub channel per user | Each server subscribes to channels for its connected users | No registry needed, but the broker holds millions of subscriptions |
Per-server channels are a common middle ground. The registry stays small, and the broker only carries as many channels as there are servers.
Failure modes
- Stale registry entry. The entry points at a dead server. The forward fails, so the sender falls back to the offline path. The recipient still gets the message from storage when they reconnect.
- Reconnect storm. A server crash sends many clients back at once. Clients should retry with exponential backoff and random jitter.
- Hot groups. A very large group concentrates fan-out on a few servers. Spread the delivery work across workers.
Because the message is stored before it is routed, a routing failure delays delivery but does not lose the message.
Exercise
A chat server crashes while holding 200,000 connections. The registry entries for those users still exist. What happens when someone messages one of those users, and why does no message get lost?
Show answer
The sender's server forwards to the dead server and the forward fails, or it finds the entry expired once the TTL passes. Either way, it treats the user as offline and sends a push notification. The message was already stored before routing, so when the client reconnects, perhaps with backoff and jitter, it fetches everything after its last sequence number.