System design concepts · Talking Over the Network

Long Polling vs WebSockets vs Server-Sent Events

"How does the server tell the client something happened?" sounds simple. The answer shapes chat apps, live scores, ride tracking and collaborative editors.

What you'll be able to explain
  • Compare short polling, long polling, SSE, WebSockets and WebRTC
  • Pick the right one for a given feature
  • Scale stateful connection servers
  • Route a message to the server holding a user's connection
  • Handle reconnects, presence and offline users

Five ways to push

ClientServer1GET /updates (short poll)2nothing new3GET /updates 5 s later4nothing new5GET /updates?wait=30 (long poll)6holds request open...7new message! (returns immediately)8next long poll right away
Short polling vs long polling. Long polling holds the request open until there's news.
Technique Direction How it works Good for Watch out for
Short polling Client pulls Ask every N seconds Rare updates, simple dashboards Wasted requests, up to N s delay
Long polling Client pulls (held) Server holds request until data or timeout Fallback when WebSockets are blocked Many hanging requests; reconnect overhead
SSE Server → client One long HTTP response streaming events Feeds, notifications, live scores, AI token streams One-way; text only
WebSocket Both ways HTTP upgrade to a persistent full-duplex socket Chat, multiplayer, collaboration Stateful servers, LB stickiness, reconnects
WebRTC Peer to peer UDP-based media/data between browsers Voice and video calls, screen share NAT traversal needs STUN/TURN servers
How to decide
  • Updates every few minutes? Polling is fine.
  • Server → client only, text events? SSE: simple, works through proxies, auto-reconnects.
  • Both directions, low latency? WebSockets.
  • Audio/video between users? WebRTC, with a server (SFU) for group calls.

The hard part: stateful connection servers

HTTP servers are stateless; WebSocket servers are not. Each one holds thousands of open connections, and a message for Priya must reach the specific server where Priya is connected.

CLIENTAarav's phoneCLIENTPriya's phoneSERVICEGateway 1holds AaravSERVICEGateway 2holds PriyaSERVICEMessage serviceCACHESession registryuser → gatewayDATABASEMessage storeWebSocketWebSocketsend(to=Priya)where is Priya?persistdeliver
Routing a message to whichever gateway holds the recipient's connection.

The pieces:

  1. Session registry: when a user connects, the gateway writes user_id → gateway_id into Redis with a TTL, refreshed by heartbeats.
  2. Delivery: the message service looks up the gateway and forwards the message there (direct RPC, or a per-gateway channel on a pub/sub system).
  3. Offline users: no registry entry → store the message and trigger a push notification (APNs/FCM). On reconnect, the client syncs everything after its last-seen message ID.

Scaling and operating connection servers

  • Capacity is about connections, not QPS. Size by memory per connection (often 10–50 KB) and file descriptors.
  • Load balancing: L4 load balancers or L7 with WebSocket support. Use "least connections", not round robin.
  • Deploys drain slowly: you can't restart a server with 200k connections at once without a reconnect storm. Drain gradually and make clients reconnect with jittered exponential backoff.
  • Heartbeats: ping/pong every 20–30 s detects dead connections and keeps NAT mappings alive on mobile networks.
  • Resume, don't replay: on reconnect the client sends its last acknowledged sequence number; the server sends only what's missing.
Common trap

Treating "delivered to the socket" as "delivered". A phone can lose the network a millisecond after the server writes. Real delivery needs a client acknowledgement with the message ID; un-acked messages are resent on reconnect, and the client deduplicates by ID.