Home / System Design / Concepts / Real-Time Delivery: Polling, Long Polling, SSE, WebSockets Concepts
CONCEPT SHEET · Concepts
Real-Time Delivery: Polling, Long Polling, SSE, WebSockets
One-liner: Four ways to get server events to clients: ask repeatedly, ask and wait, one-way stream over HTTP, or a full-duplex socket.
Article Concepts © Arthur Bernier Jr 589 words 1 code block
Say this#
SAY THIS
Short polling — client requests every N seconds; simple, wasteful, latency up to N.
Long polling — server holds the request until data or timeout (~30 s), client immediately re-requests. Works everywhere.
SSE (Server-Sent Events) — one-way server→client text stream over a single HTTP response; text/event-stream; auto-reconnect with Last-Event-ID.
WebSocket — full-duplex, persistent TCP connection after an HTTP Upgrade handshake; ws:// / wss://.
Upgrade handshake / 101 Switching Protocols — how WebSocket starts.
Heartbeat / ping-pong — keep connections alive through proxies and detect dead peers.
Fan-out via pub/sub — Redis Pub/Sub, Kafka, NATS carry events to whichever node holds the user's socket.
Connection registry / presence — map user → gateway node (Redis hash) so you can route a message.
Sticky routing — needed for WebSockets at L7 (connection stays on one node); L4 LB is fine.
Backpressure — slow clients; drop, buffer with cap, or disconnect.
WebTransport / HTTP/3 — the newer alternative (QUIC-based).
Examples — Socket.IO, Pusher, Ably, AWS API Gateway WebSocket APIs, Phoenix Channels, SignalR.
How it works#
TEXT Copy
clients <--wss--> [WS gateway nodes x N] <--subscribe--> [Redis Pub/Sub / Kafka] | registry: user_id -> node_id (Redis) services --publish(user_id, event)--> broker --> node holding that socket --> client
Client connects (Upgrade: websocket); gateway authenticates (token in query/cookie/first frame), registers user → node in Redis with TTL, heartbeats refresh it.
Gateway subscribes to channels for that user (or the node subscribes once and filters locally).
Backend publishes message(to=user_id) to the broker; the right node receives and writes to the socket.
Disconnect → registry cleanup → client reconnects with backoff → missed messages fetched via API (store-and-forward) or SSE Last-Event-ID.
Scale by adding gateway nodes; each holds hundreds of thousands of idle connections.
Trade-offs#
Choice Pro Con Short polling Trivial, cache/LB friendly Latency ≈ interval; wasted requests Long polling Near-real-time, works through any proxy Connection churn; server holds many open requests SSE Simple, HTTP, auto-reconnect, works with HTTP/2 multiplexing One-way; text only; HTTP/1.1 6-connection limit per domain WebSocket Bidirectional, low overhead per message, binary Stateful connections complicate LB/scaling; proxies/timeouts; needs heartbeats Managed (Pusher/Ably/API GW) No connection fleet to run Cost per connection/message; vendor limits
Numbers#
A tuned gateway node holds on the order of 100k-1M idle WebSocket connections (memory-bound, ~tens of KB each).
WebSocket frame overhead: 2-14 bytes vs hundreds of bytes of HTTP headers per poll.
Heartbeat every 20-30 s; many proxies/LBs idle-timeout at 60 s (AWS ALB default 60 s, API Gateway WS 10 min idle, 2 h max).
Long-poll timeout ~20-30 s; SSE reconnect default 3 s.
In the interview#
SAY THIS
Pick per need: "Chat and collaborative editing need WebSockets; notifications and live feeds can be SSE; a dashboard refreshing every 30 s can poll."
Say: "WebSocket gateway tier behind an L4 LB, user→node registry in Redis, fan-out through Redis Pub/Sub or Kafka, heartbeats, and store-and-forward for offline users."
Follow-up: "How does a message reach a user connected to a different node?" → "Publish to the broker; every node subscribes to its users' channels (or the registry tells you which node); that node writes the frame."
Follow-up: "Why not WebSockets for everything?" → "Stateful connections cost memory and complicate deploys and LB; SSE/polling ride plain HTTP and caches."
Creator
Written by Arthur Bernier Jr (Big Poppa Code). Arthur Bernier Jr — Big Poppa Code — is a software engineer, educator and entrepreneur: a former CTO at VC-backed startups and engineering lead at a billion-dollar company, mentor to 800+ engineers, now Senior Director of AI, Computer Programming and Cloud Computing at Per Scholas, and the host of the Deeper Than Code podcast. Everything on this page is his original teaching material.
bigpoppacode.io
License © Arthur Bernier Jr all rights reserved