Long Polling, WebSockets & SSE

How a server pushes updates to browsers and apps: long polling, Server-Sent Events and WebSockets on the wire, what HTTP/2 and HTTP/3 change, and how to run millions of long-lived connections.

Overview

HTTP was built for the client to ask and the server to answer. A server cannot send a response that nobody asked for, so on its own it has no way to tell a browser that a new chat message, a price change or the next words of an AI answer are ready. Three techniques fill that gap, and almost every real-time feature you will design uses one of them.

Long polling keeps an ordinary HTTP request waiting on the server until there is news. Server-Sent Events (SSE) keep one HTTP response open and write events into it as they happen, from the server to the client only. WebSockets turn an HTTP connection into a two-way channel where either side can send a message at any time.

All three share one hard part: the connection stays open for minutes or hours. Load balancers close quiet connections, mobile networks drop them, servers restart, and each open connection ties a client to one server. This chapter shows how each technique works on the wire, what HTTP/2 and HTTP/3 change, and how to run a very large number of these connections without losing messages, or overloading the servers when every client reconnects at once.

What you will be able to do

  • Explain how long polling, SSE and WebSockets work, down to the headers and frames.
  • Choose between them for a given feature, and say what each choice costs.
  • Keep long-lived connections alive through proxies and load balancers with the right timeouts and heartbeats.
  • Deliver one message to users who are connected to many different servers, through pub/sub.
  • Design reconnects that resume without gaps and do not turn into a storm.

Key concepts

  • Short and long polling
  • Server-Sent Events (SSE)
  • Last-Event-ID and resume
  • WebSocket upgrade handshake
  • Frames, ping and pong
  • HTTP/2 and HTTP/3 streams
  • Idle timeouts and heartbeats
  • Fan-out through pub/sub
  • Reconnect storms

Interview questions

  • Compare long polling, Server-Sent Events and WebSockets. Which would you use for a chat app, a live score page and a streamed AI answer?
  • Walk through the WebSocket handshake. What are Sec-WebSocket-Key and Sec-WebSocket-Accept for, and why are client frames masked?
  • How does SSE recover from a dropped connection without losing events, and what does the server have to do?
  • Users say live updates stop after about a minute when nothing is happening. What is going on, and how do you fix it?
  • How do you deliver a message to a user who is connected to a different server?
  • Design the connection tier for 1 million concurrent WebSocket users.
  • A connection server holding 50,000 clients crashes. What happens next, and how do you keep it from becoming an outage?
  • What do HTTP/2 and HTTP/3 change for SSE, long polling and WebSockets?
  • Why do AI chat APIs stream over SSE, and how does a browser read a stream that starts with a POST?
  • Do WebSockets need sticky sessions?

Sign up free to read the full topic

More System Design topics

Start free on Cariara · Coding problems · System design studio