Which to choose for live data: polling, SSE or WebSockets
Plain HTTP makes the client ask every time, so live updates need something more. Short polling asks on a timer, long polling asks and lets the server hold the question until there is news, server-sent events keep one connection open for the server to push down, and WebSockets keep a two-way connection. Choose the simplest one that fits: polling for slow dashboards, server-sent events for one-way feeds, WebSockets for two-way, fast exchange.
Your first hour with George Li (60 minutes by video, $250) ends with a written plan within one working day.
Book your first hour ($250)The problem: HTTP waits to be asked
In ordinary HTTP the browser asks and the server answers. That works for a page you load once. It is awkward for anything that changes while you watch: a shared document, a score, a chat, a feed of new posts. Using repeated plain requests to follow such data costs time and capacity, so four techniques exist, and a design answer should name the cheapest one that meets the need.
| Technique | Who starts each message | Connection | Best for |
|---|---|---|---|
| Short polling | The client, on a timer | A new request each time | A dashboard that can be a minute old |
| Long polling | The client, right after each reply | A request the server holds open until it has news | Updates now and then, with a simple setup |
| Server-sent events | The server, down one open connection | One-way, server to client | A feed or notifications |
| WebSockets | Either side | One open, two-way connection | Chat, shared editing and games |
Short polling
The client sends a request on a timer, say every 2 seconds, and the server answers with new data or with nothing. It is easy to build and every tool supports it because it is only HTTP. The cost is that most requests are wasted. If data changes five times in ten minutes and the client polls every 2 seconds, 295 of 300 requests come back empty, which is Problem 1. Lengthening the interval cuts the waste but delays updates by up to the interval.
Long polling
The client asks, and the server does not answer until it has something to say. As soon as the client receives a reply it asks again. The same five changes now take 6 requests, as Problem 2 counts, because each request ends with news. The server must be able to hold many waiting requests, which uses memory and connections, and it needs a plan for data that arrives in the gap between one reply and the next request. A held request is eventually closed by a timeout, so some requests are empty. Long polling suits updates that arrive occasionally, such as a dashboard that refreshes when a report finishes, but each cycle still repeats the full request headers.
Server-sent events
With server-sent events the browser opens one HTTP connection through the `EventSource` interface and the server keeps writing to it as events occur. There is no repeated asking, the browser reconnects on its own if the line drops, and the code on both sides is short. It is one way only: if the client must send frequent messages, it sends them with ordinary requests, or you choose WebSockets.
MDN states the main limitation. When the connection is not HTTP/2, a browser allows only 6 open server-sent event connections per browser and domain, which hurts when a user opens several tabs of the same site, and the issue is marked "Won’t fix" in Chrome and Firefox. Problem 3 counts how many of nine tabs go without a stream. Over HTTP/2 the limit is much higher, so the practical advice is to serve over HTTP/2.
WebSockets
A WebSocket starts as an HTTP request that asks to upgrade the connection, and after that both sides send messages whenever they like over one long-lived connection on top of TCP, with no repeated headers. MDN describes the API as one that opens a two-way interactive session between the browser and a server without polling for replies. This is the right tool for a multiplayer game or a shared editor, where updates flow both ways and delay matters. It costs more to build: you handle reconnection yourself, some corporate networks and proxies block or cut long-lived connections, and many open sockets need servers that can hold them and a way to route a message to the right socket when there are many servers.
| Technique | Setup effort | Server load | Main limit |
|---|---|---|---|
| Short polling | Lowest | High: a request every interval whether or not there is news | Delay equals the interval |
| Long polling | Low | Medium: many requests held open, then one per event | Ordering and lost messages if several requests are open at once |
| Server-sent events | Low | Low once connected | One way; 6 connections per browser and domain without HTTP/2 |
| WebSockets | Highest | Low per message, but each client holds a socket | More to build, and some networks block them |
Problem 4 compares the header bytes of polling with a persistent connection. Its numbers are assumptions, not measurements, so what matters is the method: count the requests, multiply by the overhead each carries, and compare with a connection set up once.
A decision rule you can say aloud
- Does the data need to be fresher than your polling interval can give? If not, poll.
- Does only the server send updates? Then use server-sent events.
- Do both sides send often, or is delay critical? Use WebSockets.
- Whatever you pick, say how a client recovers after a dropped connection and how many clients one server can hold.
A note on who needs this. Our system design for interns guide found no system design round named in our program records, and a first internship interview is mostly coding, your projects and behavioral questions. This page is for students who are curious, who have a design question in a course or a project, or who are looking ahead to a new-graduate loop. If a recruiter says there will be a design round, ask what scale it expects and prepare for that.
Practice problems
These problems are our own, written for this site. Every answer is recomputed when the site is built, and a wrong answer would stop the build. Try each before you open the steps. Read how the problems are checked.
Problem 1. A client polls a server every 2 seconds for 600 seconds, which is 300 polls. New data appears at the seconds 31, 95, 211, 340 and 505, each one in a different 2 second window, and each is returned by the first poll after it appears. How many of the 300 polls return no new data? Give an integer.
Show the answer and the working
Answer: 295
- Each of the 5 pieces of data is returned by exactly one poll, so 5 polls return something.
- The other 300 - 5 = 295 polls return nothing.
- Every one of those 295 still costs a request and a response, which is the overhead that long polling and the other two methods try to avoid.
Problem 2. A client uses long polling. It sends a request at time 0 and the server holds it until data is available. When the response arrives, the client sends the next request at once. New data appears at the same 5 times as in the polling example, at the seconds 31, 95, 211, 340 and 505, and the window ends at 600 seconds. How many requests does the client send in the window, counting the one still waiting at the end? Give an integer.
Show the answer and the working
Answer: 6
- The first request is sent at time 0.
- Each of the 5 pieces of data ends the request being held, and the client sends a new request right after, which makes 5 more.
- The total is 1 + 5 = 6 requests, against 300 for polling every 2 seconds. A real server also closes held requests after a timeout, which adds some requests.
Problem 3. A browser allows at most 6 server-sent event connections to one domain when the connection is not HTTP/2. A user opens 9 tabs of the same site, and each tab opens one such connection. How many tabs are left without a stream? Give an integer.
Show the answer and the working
Answer: 3
- The first 6 tabs each get a connection and use up the limit.
- The 7th, 8th and 9th tabs cannot open one.
- 9 - 6 = 3 tabs have no stream. The limit is per browser and domain, so a fourth tab of another site would not be affected.
Problem 4. Polling every 2 seconds for 600 seconds sends 300 requests, and assume each request and response pair carries 900 bytes of headers. A single persistent connection costs 900 bytes to set up and then 6 bytes of framing for each of the 5 messages sent during the window. How many fewer header bytes does the persistent connection use over the 600 seconds? Give an integer.
Show the answer and the working
Answer: 269070
- Polling uses 300 x 900 = 270,000 bytes of headers.
- The persistent connection uses 900 + 5 x 6 = 930 bytes.
- 270,000 - 930 = 269,070 bytes saved. The numbers are assumptions chosen for the exercise, not measurements of any protocol.
Questions
When should I use server-sent events instead of WebSockets?
Use server-sent events when the server sends updates and the client mostly listens, such as a feed of new posts. Use WebSockets when both sides send often, such as a shared editor or a multiplayer game.
Why is long polling better than short polling?
It sends far fewer requests for the same data, because the server holds each request until there is something to return. Problem 2 counts the difference for one example.
What is the limit on server-sent events in a browser?
MDN documents a limit of 6 open connections per browser and domain when the page is not served over HTTP/2, which hurts when a user opens several tabs.
How a session helps
In a session George can work a small design question with you aloud, ask where each of your numbers came from and mark where a cost was left unsaid. You say each design in your own words; nothing here is written for you to recite. See the packages page for the sessions and book a session when you have a date to work toward.
Want help with your own applications? See internship interview coaching, a career coach for college internships, or resume review for internships.