04. Communication / Lesson 4 of 4
Real-time Updates
How a server tells a browser the moment something changes.
- 10 min
- Intermediate
- 10 steps
- Open connections—
- New requests/s50,000 (bad)
- Delay1 s avg (warning)
- Request
- Response
- Health check
- Write
- Failed request
Step 1 of 10: Asking again and again
Read every step as text
Step 1: Asking again and again
The simplest way to get fresh data: the browser asks
GET /messagesevery 2 seconds. With 100,000 users online that is100,000 / 2 = 50,000requests a second.But a user gets news only about every 30 seconds, so only about 7% of the requests return anything. The rest are empty answers. And news waits up to 2 seconds (1 s on average) before the browser asks.
Polling is simple and fine for slow data (a build status every minute). For anything live, it is wasteful and late.
Step 2: Long polling: ask, and wait for the answer
With long polling the server does not answer an empty question. It holds the request open until there is news (or a timeout such as 30 seconds), then answers, and the browser immediately asks again.
News now arrives right away: about 50 ms, the time to cross the network. Requests fall to about
100,000 / 30 = 3,333a second, but the server holds 100,000 open requests.It works everywhere, even through old proxies. The cost is a new request after every message.
Step 3: Server-Sent Events: one stream, server to browser
With Server-Sent Events (SSE) the browser opens one request and the server keeps the response open, writing a line each time there is news. The browser reconnects by itself if the stream drops and can say where it stopped.
Now there is no new request per message: new requests per second are about 0. SSE is one-way (server to browser) over plain HTTP, which is enough for feeds, scores and notifications.
SSE is the least work when the browser only listens.
Step 4: WebSockets: a two-way pipe
A WebSocket starts as an HTTP request that asks to upgrade. After the server says
101 Switching Protocols, both sides can send messages at any time over the same connection, each with only a few bytes of framing.That is what chat, multiplayer games and shared editing need: the browser also talks a lot. The cost is more to run: you manage the connection yourself, and some proxies and load balancers need settings for it.
Only listening: SSE. Talking both ways, fast: WebSockets. Rarely changing: plain polling is fine.
Step 5: 100,000 connections need 4 servers
An open connection costs memory: say about 10 KB, so 25,000 connections take about 250 MB. We plan 25,000 per server to leave room for spikes, so 100,000 users need
100,000 / 25,000 = 4gateway servers.A load balancer sends each new connection to a gateway, and the connection stays on that gateway for its whole life. That is the new difficulty: a user is now tied to one particular server.
Connection servers hold state (who is connected). Keep them thin: they only move messages, while the logic lives elsewhere.
Step 6: How a message finds the right gateway
Alice is connected to gateway 1 and Bob to gateway 3. When Alice sends a message for Bob, gateway 1 has no link to him. It publishes the message to a pub/sub channel such as
user:bob.Every gateway subscribes to the channels of the users connected to it, so only gateway 3 receives it and writes it to Bob's socket. No gateway needs to know where anyone else is.
Pub/sub lets you add gateways without changing the others. Gateways can come and go.
Step 7: Presence: who is online?
Each client sends a small heartbeat every 30 seconds over its open connection. The gateway writes
online:userto a fast store with an expiry of 60 seconds (two missed beats).If the heartbeats stop, the key simply expires and the user shows as offline, even if the connection broke without a goodbye. The cost:
100,000 / 30 = 3,333writes a second to the store.Do not tell all your friends every time you flicker offline. Add a short delay, or send presence only for the chat that is open.
Step 8: A gateway dies and everyone comes back at once
Gateway 2 restarts. Its 25,000 clients lose their connection and 75,000 remain. If every client reconnects in the same second, the other gateways get 25,000 new connections at once.
The fix: each client waits a random, growing delay before it retries (exponential backoff with jitter). Spread over about 30 seconds that is
25,000 / 30 = 833a second. After reconnecting, the client says the id of the last message it saw so the server can send what it missed.Reconnect is the normal case, not the rare one. Design for it: backoff, jitter and catch-up from the last id.
Step 9: From a hundred thousand to a million
For 1,000,000 users at 25,000 each, you need
1,000,000 / 25,000 = 40gateways. They are cheap to add, because each one is independent and the pub/sub layer links them.The limits move to other places: the pub/sub system, the presence store, and deploys (restarting a gateway drops its users, so roll them slowly). Heavy users such as a channel with a million readers use fan-out by topic, not per-user messages.
Scale by adding gateways, and by sharding pub/sub channels and the presence store by key.
Step 10: Real-time in one picture
Polling asks 50,000 times a second and gets empty answers; long polling and SSE hold a request open; WebSockets give a two-way pipe. Push removes the wait: about 50 ms instead of up to 2 s.
Many connections need many gateways, pub/sub to reach the right one, heartbeats for presence, and clients that reconnect with backoff and catch up.