CMD Guide
HomeSystem DesignSystem Design Trade-offs

LongPolling vs WebSockets vs ServerSent Events

All four of these run over the same TCP/HTTP plumbing; they differ on exactly one axis — how long the connection stays open and which direction bytes may flow once it is — and every trade-off below falls out of that single choice. A plain HTTP request is the degenerate case: the client opens a connection, the server computes a reply, sends it, and the connection is done. To get server-initiated updates you have to bend that cycle in one of four ways.

The four mechanisms, on one axis

A traced example: a stock ticker

The server receives an AAPL price update at t = 2.0s (189.20) and again at t = 7.0s (190.05). The client wants each price as soon as possible. Poll interval = 3s; long-poll timeout = 30s. Watch when the client actually sees each update and how much is wasted.

ApproachNetwork events in 0–9sSees 189.20 (@2.0s)Sees 190.05 (@7.0s)Waste
Polling @3sRequests at 0, 3, 6, 9st=3.0s (1.0s late)t=9.0s (2.0s late)2 empty responses (t=0, t=6)
Long-poll1 held request → reply @2.0s, reconnect, held → reply @7.0st=2.0s (~0 late)t=7.0s (~0 late)0 empty, but 2 full header exchanges
WebSocket1 handshake @0, then 2 pushed framest=2.0s (~0 late)t=7.0s (~0 late)~6 bytes framing/msg; can also send a buy order upstream instantly
SSE1 GET @0, then 2 data: eventst=2.0s (~0 late)t=7.0s (~0 late)Text only; auto-resumes if dropped

Polling is the outlier: it trades latency and bandwidth for zero server-held state. The other three all deliver in one round-trip; they differ in what they cost you operationally, not in freshness.

diagram
diagram

What the wire actually looks like

SSE is just a never-ending HTTP response. The id: field is the mechanism behind its killer feature — resumption:

GET /prices HTTP/1.1
Accept: text/event-stream

HTTP/1.1 200 OK
Content-Type: text/event-stream
Cache-Control: no-cache

id: 1
data: {"sym":"AAPL","px":189.20}

id: 2
data: {"sym":"AAPL","px":190.05}

If the stream drops, the browser's EventSource reconnects on its own and sends Last-Event-ID: 2, so the server can replay from event 3 — no lost messages, no client code. WebSockets, by contrast, start as a one-time HTTP upgrade and then leave HTTP behind entirely:

GET /ws HTTP/1.1
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 there is no built-in reconnect and no resumption — you own both.

Side-by-side trade-offs

PropertyPollingLong-pollWebSocketSSE
Directionclient-pullclient-pull, delayedfull-duplexserver → client only
TransportHTTP req/respHTTP req/respupgraded TCP (ws/wss)one long HTTP response
Per-message overheadfull headers each pollfull headers per message~2–14 B framea few bytes per event
Latencyup to poll interval~1 RTT~1 RTT~1 RTT
Binary payloadsyesyesyesno — UTF-8 text only
Auto-reconnect + resumemanualmanualmanual (you build it)built-in (Last-Event-ID)
Proxy / firewalluniversaluniversalcan be blocked — use wss://universal (disable buffering)
Counts vs 6-connection capyesyesno (separate, higher)yes (on HTTP/1.1)
Server statefulnessstatelessmostly statelessstateful (sticky + backplane)held stream per client; reconnect is stateless if events live in a shared, id-addressable log

One honesty note on that last row: SSE does not exempt you from the fan-out backplane — a stream is pinned to one node just like a socket, so an event published on node B still needs a pub/sub bus to reach an SSE client held on node A. What SSE removes is sticky routing (any node can serve a resume from the shared event log keyed by Last-Event-ID) and the client-side reconnect code — not the backplane.

Pitfalls

When to use which — and when not

In one line: use SSE when data flows one way and you want reconnection for free; reach for WebSockets only when you truly need to talk back on the same channel; keep long-polling as the compatibility fallback; and plain polling when holding connections open isn't worth it.

Takeaways


Re-authored and deepened for this guide. Sources: MDN Web Docs (Server-sent events, EventSource, WebSockets API); RFC 6455 (The WebSocket Protocol) for the handshake key/accept example; the WHATWG HTML Living Standard (event stream format and reconnection); Ilya Grigorik, High Performance Browser Networking (transport trade-offs and the per-origin connection limit); and the original "Grokking the System Design Interview" lesson this page expands.

🤖 Don't fully get this? Learn it with Claude

Stuck on LongPolling vs WebSockets vs ServerSent Events? Open Claude, copy a block below, and it'll teach you this exact concept — visually and interactively.

🎨 Explain it visually

Build the mental picture, not memorization.

I just read a lesson on **LongPolling vs WebSockets vs ServerSent Events** (System Design) and want to truly understand it. Explain LongPolling vs WebSockets vs ServerSent Events from first principles using ONE vivid real-world analogy and a visual mental model — draw it as ASCII art or a clear step-by-step diagram — with a concrete example using real numbers. Then ask me one question to check I got the mental picture, and wait for my reply. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
🤔 Walk me through it (interactive)

Socratic — adapts to where you're stuck.

Teach me **LongPolling vs WebSockets vs ServerSent Events** interactively. Ask me ONE guiding question at a time, wait for my answer, and adapt to my confusion — build the idea with me step by step instead of explaining it all at once. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
🧪 Quiz me & fix my gaps

Active recall exposes what you missed.

Quiz me on **LongPolling vs WebSockets vs ServerSent Events** with 5 questions, easy to tricky, ONE at a time. Tell me if each answer is right; at the end, explain clearly what I got wrong and why. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
🧠 Make it stick

Intuition + hook + flashcards for long-term memory.

Help me remember **LongPolling vs WebSockets vs ServerSent Events** for the long term: give the one-sentence intuition, a memorable hook/mnemonic, a tiny worked example, and 3 active-recall flashcards (Q -> A). If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.

📝 My notes