System design concepts · Communication

Long Polling vs WebSockets vs SSE

Short answer

All three let a server send updates to a browser without the user refreshing the page. Long polling is plain HTTP: the client sends a request, the server holds it open until there is news or a timeout, and the client immediately asks again. Server-Sent Events (SSE) keep one HTTP response open and stream text events from server to client, and the browser's EventSource API reconnects automatically. WebSockets upgrade an HTTP connection to a persistent, full-duplex channel where both sides can send messages at any time. Use SSE for one-way streams such as live scores, dashboards or streaming LLM output, WebSockets for two-way interactive traffic such as chat, collaborative editing or games, and long polling when you need the widest compatibility or updates are infrequent.

Read the video transcript

Three ways to get live updates into a browser. Watch the same messages travel down each lane. Long polling is plain HTTP. The client asks, and the server holds the request open until there's news. It answers, and the client immediately asks again. Every message costs a full request and response. Server-Sent Events open one HTTP response and keep it streaming. Each event is a few bytes of framing, it only flows from server to client, and the browser reconnects on its own, resuming from the last event ID. WebSockets upgrade the connection into a full-duplex channel. Both sides send small frames whenever they like, text or binary. The catch, for both streaming options: each user is pinned to one gateway server, so a message has to find the gateway that holds the recipient's connection. So pick by direction. One-way feeds, like live scores or streamed AI tokens, suit SSE. Chat, games and shared cursors need WebSockets. And long polling is the fallback that works through almost anything.

Long Polling vs WebSockets vs SSE: comparison table

AspectLong pollingWebSocketsServer-Sent Events
DirectionServer to client, one response per requestBoth directions at any timeServer to client only
TransportRepeated HTTP requestsHTTP upgrade to the WebSocket protocol over one TCP connectionOne long-lived HTTP response (text/event-stream)
Per-message overheadA full HTTP request and responseA small frame headerA few bytes of event framing
Reconnect and resumeNatural: every request is newYou build it yourselfBuilt in: automatic reconnect with Last-Event-ID
DataAnything HTTP can carryText or binaryUTF-8 text only
InfrastructureWorks through almost any proxyProxies and load balancers must support the upgrade and long-lived connectionsPlain HTTP, but buffering proxies must be set to flush
Good fitInfrequent updates, fallback pathsChat, multiplayer, collaborative editing, trading screensLive scores, notifications, dashboards, token streaming

When to use each

Long polling

  • Updates are infrequent and you want plain request and response semantics.
  • Clients or networks cannot hold WebSocket or streaming connections.
  • As a fallback path behind a WebSocket or SSE client.

WebSockets

  • The client sends frequent messages too, not just the server.
  • Latency between users matters, as in chat, games or shared cursors.
  • You need binary frames.

Server-Sent Events

  • Traffic is one way, from server to client.
  • You want automatic reconnect and resume without writing it yourself.
  • You want to stay on plain HTTP, including HTTP/2 multiplexing.
The verdict

Long polling

Updates are infrequent and you want plain request and response semantics.

Otherwise

WebSockets

The client sends frequent messages too, not just the server.

Otherwise

Server-Sent Events

Traffic is one way, from server to client.

Scaling persistent connections

WebSockets and SSE make the gateway tier stateful: each user is attached to one server. When a message for that user is produced elsewhere, the system has to find the right gateway. The usual pattern keeps a registry from user ID to gateway, or has every gateway subscribe to a pub/sub channel for the users it holds, so producers publish once and the owning gateway delivers.

A registry maps each user to the gateway holding their socket.

Long-lived connections also change capacity planning. A gateway is limited by open connections and memory per connection more than by requests per second. Plan for heartbeats to detect dead connections, for load balancer idle timeouts, and for the reconnect storm when a gateway restarts and all of its clients come back at once.

Browser details that matter

Over HTTP/1.1, browsers cap the number of connections per host at a small number, so several SSE streams in different tabs can exhaust it. Over HTTP/2 the streams share one connection and the cap stops being a problem. SSE carries only text, so binary data has to be encoded. WebSocket messages have no built-in acknowledgment or resume, so chat systems add message IDs and a sync call on reconnect to fetch anything missed.

HTTP/1.1 caps SSE at six connections per domain, HTTP/2 multiplexes them.
Interview angle

How it comes up in a system design interview

Any real-time feature leads here: chat, presence, live comments, scores, collaborative editing and streamed AI responses. Pick one, say why its direction and delivery guarantees fit the feature, then spend most of the time on how messages find the right connection and what happens on reconnect.

Where you will meet it

  • How does a message from user A reach user B, who is connected to a different gateway server?
  • How does the system know a user is offline, and how quickly?
  • A live event has a very large audience posting comments. How do you fan them out without overloading gateways?
  • How do you stream tokens from a model to the browser as they are generated?
How a strong answer sounds
“For chat I would use WebSockets because both sides send frequently. Each gateway registers its connected users in Redis, and when a message arrives the chat service looks up the recipient's gateway and forwards it there. On reconnect the client sends the last message ID it saw and fetches anything newer, so a dropped connection never loses messages.”

Common mistakes

  • Choosing WebSockets for a one-way feed where SSE would be simpler and reconnect for free.
  • Ignoring how a message finds the server that holds the recipient's connection.
  • Treating the socket as reliable delivery, with no message IDs or catch-up after reconnect.

Frequently asked questions

Is SSE better than WebSockets?

For one-way server-to-client streams, SSE is usually simpler: it is plain HTTP, reconnects on its own and resumes from the last event ID. When the client also sends frequent messages, WebSockets fit better.

Can SSE send binary data?

No. SSE carries UTF-8 text, so binary data must be encoded, for example as base64, or sent over a different channel.

Is long polling still used?

Yes, as a fallback where streaming connections are blocked and for features with infrequent updates. It also keeps the server side simple because every exchange is an ordinary request.

Do WebSockets work through load balancers?

Yes, if the load balancer supports the HTTP upgrade and its idle timeout is longer than your heartbeat interval. Because connections are long lived, rebalancing happens only as clients reconnect.

Learn and practise

Lessons

Problems where this choice matters

Knowing the trade-off is half of it

Practise saying it under follow-up questions: a timed system design interview with an AI interviewer that probes your choices and grades against a rubric.

Start a mock interview