Real-Time Updates
A huge share of system design questions have the same hidden requirement: when something changes on the server, the user should see it right away. A chat message arrives. A driver's car moves on the map. The score changes. Someone else edits the paragraph you're reading. The like count ticks up. HTTP was built for the opposite pattern, where the client asks and the server answers, so getting data to a client without being asked takes some deliberate design.
There are two halves to every real-time design, and interviews test both:
- The last mile: how bytes get from a server to a client. Polling, long polling, Server-Sent Events (SSE), WebSockets, and occasionally WebRTC or mobile push.
- The server side: how an update that happens somewhere in your backend finds the one server, out of hundreds, that holds the user's connection. This is the part most candidates skip, and it's where most of the interesting design is.
We'll go through both, then cover the reliability and scaling problems that come with long-lived connections: missed messages, reconnect storms, load balancer timeouts and how many connections a server can hold.
The Last Mile: Getting Updates to the Client
Short Polling
The simplest approach is for the client to ask on a timer:
setInterval(async () => {
const res = await fetch(`/api/orders/${id}/status`);
render(await res.json());
}, 5000);
It works through any proxy, needs nothing special on the server, and every request is a normal stateless HTTP call that any server can answer. For a lot of features that's genuinely the right answer. An order status page, a dashboard that refreshes every 30 seconds, or a build status indicator doesn't need anything fancier.
The costs show up at scale and in latency:
- Latency is half the interval on average. Polling every 5 seconds means updates appear 2.5 seconds late on average, up to 5.
- Most requests are wasted. With 1 million clients polling every 5 seconds, that's 200,000 requests per second, and if the data changes once a minute, more than 90% of them return "nothing new." Each one still pays for TLS, auth, routing and a database or cache read.
- Shorter intervals multiply the load. Getting latency down to 1 second means 1 million requests per second.
Short polling is fine when updates are infrequent, a few seconds of delay is acceptable, and the client count is modest. Cache the response (or use ETag and 304 Not Modified) so empty polls are cheap.
Long Polling
Long polling fixes the waste without leaving plain HTTP. The client makes a request, and the server holds it open until there's something to send or a timeout (say 30 seconds) passes. As soon as the client gets a response, it immediately sends the next request.
async def poll(request):
cursor = int(request.query["after"]) # last event the client saw
deadline = time.monotonic() + 30
while time.monotonic() < deadline:
events = await store.events_after(request.user, cursor)
if events:
return json_response(events) # client re-polls right away
await wait_for_notification(request.user, timeout=deadline - time.monotonic())
return json_response([]) # timeout: client re-polls
Updates now arrive almost immediately, and an idle client costs one request every 30 seconds instead of one every 5. It works anywhere HTTP works, which is why it was the standard technique before WebSockets, and why libraries like Socket.IO still fall back to it.
The weaknesses are subtle:
- There's a gap between polls. An event that happens after one response is sent but before the next request arrives must not be lost. The fix is the cursor in the code above: the client says "send me everything after event 1041," so nothing falls through the gap.
- Each message costs a full HTTP round trip. For a chat with bursts of messages, each burst is a new request with headers and auth. Fine for occasional updates, poor for high-frequency ones.
- Servers hold many open requests. That's the same resource problem as WebSockets (lots of idle connections), with none of the efficiency.
Server-Sent Events (SSE)
SSE is a standard way for a server to stream events to a browser over a single long-lived HTTP response. The client opens a connection with the built-in EventSource API, and the server keeps writing events in a simple text format:
HTTP/1.1 200 OK
Content-Type: text/event-stream
id: 1042
event: score
data: {"home": 2, "away": 1}
id: 1043
event: score
data: {"home": 3, "away": 1}
const es = new EventSource("/games/88/stream");
es.addEventListener("score", (e) => render(JSON.parse(e.data)));
SSE has some very practical advantages:
- It's just HTTP. It goes through most proxies, load balancers and CDNs, uses normal cookies and auth headers, and every web server can do it.
- Reconnection is built in. If the connection drops,
EventSourcereconnects automatically and sends aLast-Event-IDheader with the lastidit received, so the server can resend what was missed. You get catch-up semantics almost for free. - It's simple to implement. A server writes lines to a response and flushes. There's no framing protocol to implement.
The limitations:
- One direction only. Server to client. The client sends anything back with ordinary HTTP requests, which is perfectly fine for most apps (live scores, notifications, dashboards, streaming LLM tokens), but awkward for high-frequency two-way traffic like a game.
- Text only. Binary data has to be encoded (base64), which adds about a third to its size.
- Connection limits on HTTP/1.1. Browsers allow about six connections per domain over HTTP/1.1, and each SSE stream uses one, so a user with many tabs open can run out. Over HTTP/2 and HTTP/3, streams are multiplexed on one connection and this goes away.
- Buffering proxies. Some proxies buffer responses until they're complete, which breaks streaming. You may need to disable buffering (for example
X-Accel-Buffering: nofor Nginx).
WebSockets
A WebSocket is a persistent, full-duplex connection: after an initial HTTP handshake, client and server can each send messages to the other at any time, with very little overhead per message.
GET /chat HTTP/1.1
Host: example.com
Upgrade: websocket
Connection: Upgrade
Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==
Sec-WebSocket-Version: 13
HTTP/1.1 101 Switching Protocols
Upgrade: websocket
Connection: Upgrade
Sec-WebSocket-Accept: s3pPLMBiTxaQ9kYGzzhZRbK+xOo=
After the 101 Switching Protocols response, the TCP connection carries WebSocket frames. A small message has only a few bytes of framing (2 to 14 bytes of header), compared with hundreds of bytes of headers for an HTTP request. Frames can be text or binary, and there are control frames for ping, pong and close.
WebSockets are the right tool when:
- Both sides send often: chat with typing indicators, multiplayer games, collaborative editing where every keystroke goes up and everyone else's comes down.
- Latency matters at the millisecond level: trading UIs, live auctions, gaming.
- Messages are small and frequent, so per-message overhead dominates.
They come with real operational costs:
- Stateful servers. Each connection is pinned to one server process for its lifetime. You can't treat those servers like stateless web servers; deploys, scaling and failover have to account for connections.
- No built-in reconnection or catch-up. Unlike SSE, if a WebSocket drops, your code has to notice, reconnect, re-authenticate, resubscribe and fetch anything missed.
- Infrastructure support. Load balancers and proxies have to support the
Upgradeand long-lived connections (all modern ones do, but idle timeouts need tuning, more below). - You design the protocol. WebSocket gives you a pipe for messages, not a schema. You define message types, acknowledgments and versioning yourself, or use a library.
WebRTC and Mobile Push
Two more options round out the toolbox:
- WebRTC sets up peer-to-peer connections between clients, usually over UDP, for audio, video and low-latency data. It needs a signaling channel (often a WebSocket) to exchange connection details, STUN servers to discover public addresses, and TURN servers to relay traffic when peers can't connect directly. Use it for video calls (/system-design/video-conferencing) or peer-to-peer game data, not for server-to-client updates.
- Mobile push notifications (APNs for iOS, FCM for Android) reach devices when your app isn't running. A mobile OS won't keep your app's WebSocket open in the background, so "new message while the app is closed" goes through push, and the app opens its real-time connection when it comes to the foreground. See /system-design/notification-service.
Choosing a Protocol
| Short polling | Long polling | SSE | WebSockets | |
|---|---|---|---|---|
| Direction | Client pulls | Client pulls, server holds | Server to client | Both ways |
| Latency | Up to the interval | Near real-time | Near real-time | Near real-time, lowest overhead |
| Server state | None | Open requests | Open streams | Open connections |
| Reconnect / catch-up | N/A | Cursor per request | Built in (Last-Event-ID) |
You build it |
| Infrastructure | Anything | Anything | Most HTTP infra | Needs upgrade support |
| Best for | Infrequent changes | Fallback, simple setups | Feeds, scores, notifications, LLM streaming | Chat, games, collaboration |
A good default in an interview: SSE when updates flow one way, WebSockets when the client also sends frequently, and polling when updates are rare. Saying "WebSockets" for everything is common; noticing that a live scoreboard only needs server-to-client and choosing SSE for its simplicity is a small but real senior signal.