Long Polling vs WebSockets vs SSE
You'll learn to
- -Explain why plain HTTP cannot push data to a client, and what polling costs
- -Compare short polling, long polling, server-sent events and WebSockets by direction, overhead and infrastructure
- -Work out the load of short polling in numbers, and the number of servers needed to hold open connections
- -Choose a technique for a given scenario and name the operational issues of long-lived connections
Back in the first module, HTTP was described as request and response: the client asks, the server answers, and that is the end of it. The model breaks down the moment the server needs to tell the client something the instant it happens: a new chat message, a live score, a price change. The server has no way to start a conversation, so the question is how to fake it, or how to replace it with something better. There are four answers, and each suits a different job.
An Analogy: Waiting for a Parcel
You are expecting a parcel, and there are four ways to find out when it arrives. You can walk to the front door every ten seconds and look. You can ask the doorman to tell you when it comes, and he stands beside you saying nothing until it arrives (or an hour passes), and then you ask again. You can have a doorbell that rings every time something is delivered, so you only listen. Or you can have an intercom, where either of you can speak at any moment.
- -Walking to the door repeatedly is short polling: the client asks again and again, on a timer.
- -The doorman who holds you until there is news is long polling.
- -The doorbell is server-sent events (SSE): the server pushes, and the client only listens.
- -The intercom is a WebSocket: a two-way line that stays open.
The Cost of Asking Over and Over
Short polling is the easiest to build and the worst at scale. Say 100,000 users each ask for updates every 5 seconds. That is 100,000 / 5 = 20,000 requests a second, arriving whether or not anything has changed. If a given user gets one real update an hour, they make 3,600 / 5 = 720 requests in that hour and 719 of them, about 99.9%, find nothing. You pay for every one of those on the server and on the network, and the user still waits up to 5 seconds to hear anything. Shortening the interval cuts the delay but multiplies the load.
Three Better Techniques
Long polling keeps the request open. The client sends a request and the server holds it, not answering until there is new data or a timeout is reached. The moment the client gets a response it sends another request. It works over plain HTTP with no special infrastructure, and it gives near-instant updates, but it keeps a connection tied up for each waiting client and pays for a new request after every message.
Server-sent events (SSE) use one long-lived HTTP connection over which the server pushes a continuous stream of events. Browsers reconnect automatically and can say which event they saw last, so the stream resumes where it stopped. It is simple and works over ordinary HTTP, but it is strictly one-way: the client cannot send data back on the same connection.
WebSockets give a true two-way, persistent connection. The client starts with an ordinary HTTP request that asks to upgrade, and if the server agrees, the same connection switches to the WebSocket protocol, after which either side can send a message at any time with almost no overhead:
GET /chat HTTP/1.1
Host: chat.example.com
Upgrade: websocket
Connection: UpgradeHTTP/1.1 101 Switching Protocols
Upgrade: websocket
Connection: UpgradeThe card below describes the WebSocket server component as it appears in the lab:
Normal HTTP requests are like sending letters: you send a question, wait for a reply, and the connection closes. WebSockets are like a phone call. You dial once, and then both sides can talk to each other anytime without hanging up. A WebSocket server keeps a persistent, always-open connection with each client. This means the SERVER can push data to the client instantly, without the client asking. Imagine a chat app: without WebSockets, your browser would have to ask the server 'Any new messages?' every second (called polling), which wastes bandwidth and battery. With WebSockets, the server instantly pushes new messages the moment they arrive. Zero delay, zero wasted requests. This is how WhatsApp Web, Slack, Google Docs collaborative editing, live sports scores, and stock tickers work in real-time.
Examples: Socket.io, ws (Node.js library), STOMP (Spring), Pusher, Ably, AWS API Gateway WebSocket
Choosing One
- -Need one-off updates, simplicity over efficiency? Long polling.
- -Need server-to-client only, over plain HTTP (live scores, price tickers)? SSE.
- -Need true two-way, low-latency communication (chat, multiplayer, collaborative editing)? WebSockets.
The diagram shows what makes a WebSocket different: a single long-lived pipe in both directions, instead of a fresh request and response for every message.
A WebSocket connection is a single, long-lived, bidirectional pipe, unlike a fresh HTTP request/response for every message.
What Long-Lived Connections Cost You
Holding connections open changes how you run servers, and it is worth knowing before an interviewer asks.
- -Connection count becomes the limit. If one server can hold about 50,000 idle connections, a million concurrent users need about 20 servers just to keep them open.
- -Servers must be able to message each other. A user connected to server 1 cannot receive a message that server 2 receives, unless the servers share messages through a publish/subscribe layer. The next chapter shows how.
- -Connections die silently. Phones change networks and proxies close idle connections, so clients and servers send small periodic heartbeats and the client reconnects, with a growing delay between attempts so a restart does not trigger a stampede.
- -Deploys drop connections. Restarting a server disconnects everyone on it, so roll out gradually and expect reconnect storms.
- -The load balancer must support the upgrade and keep long connections alive.
In one line: HTTP cannot push, so choose by direction. Short polling is simple but wasteful, long polling gives near-instant updates over plain HTTP, SSE is the easy one-way stream, and WebSockets are for true two-way traffic, at the cost of running many open connections.
Interview Signal is part of Pro
See a real weak answer next to a real strong one for this exact topic.
Quiz is part of Pro
Test what you just read with a short quiz, and bank the XP.
Discussion0
Join the Discussion
Sign in to leave comments, reply to others, or like insights.
No comments yet. Be the first to start the thread!