Polling, long polling and Server-Sent Events
Get live updates from a server: the cost of short polling, how long polling works, Server-Sent Events with EventSource (the stream format, auto-reconnect, Last-Event-ID), SSE over HTTP/2, and SSE for streaming LLM tokens.
Comes up in: Frontend system design, Web fundamentals
Learn#
Rahul’s cricket score page asks the server for the score every 2 seconds. With 50,000 fans watching, that is 25,000 requests a second, and most answers are “no change”.
Polling means asking repeatedly on a timer. It is simple, but it wastes requests and adds delay up to the polling interval. Long polling holds each request open until there is news, then the client asks again at once.
Server-Sent Events (SSE) keep one HTTP response open and let the server write events into it. The browser’s EventSource handles the parsing and reconnecting:
HTTP/1.1 200 OK
Content-Type: text/event-stream
Cache-Control: no-cache
id: 41
event: score
data: 182/3
: keep-alive comment
id: 42
event: score
data: 186/3On the client: new EventSource("/score/ind-aus").addEventListener("score", (e) => show(e.data)).
Network
- GET /score/ind-aus (stays open)
Console
empty
The page opens new EventSource('/score/ind-aus'). The browser sends one GET with Accept: text/event-stream.
Read the steps as text
- The page opens new EventSource('/score/ind-aus'). The browser sends one GET with Accept: text/event-stream.
- The server writes an event whenever the score changes: id, event name, data, then a blank line.
- The user's train enters a tunnel. The connection drops. EventSource waits, then reconnects on its own.
- The reconnect carries Last-Event-ID: 41, so the server sends only what was missed.
Predict#
Predict first
Your SSE stream drops and EventSource reconnects. How does the server know which events the client missed?
If events carry id: lines, EventSource remembers the last one and sends it as Last-Event-ID when reconnecting. The server can then resend everything after it. Without ids, missed events are lost, which people often discover in production.
Interview angle#
Say it like this — about 30 seconds
“Short polling asks on a timer: simple but wasteful and laggy. Long polling holds the request until there is news, then asks again. Server-Sent Events keep one HTTP response open and stream text events; EventSource reconnects on its own and sends Last-Event-ID so the server can resend missed events. SSE is one-way, server to client, and plain HTTP, so it works well with HTTP/2 and CDNs. WebSockets are for two-way, low-latency traffic like chat or games.”
Follow-up Compare polling, long polling, SSE and WebSockets.
Follow-up Why might SSE be a better choice than WebSockets for some features?
Follow-up How do LLM chat apps stream tokens?
Practice#
On your own
Write the parser at the heart of EventSource: fields, comments, multi-line data and the last event id.
Done when
- Events end at a blank line; comments are ignored.
- Multi-line
datais joined with newlines; the type defaults to “message”. - The last
idcarries over to later events.
Remember#
Try to answer each card before you open it. Then add them to your reviews.
What content type does an SSE response use?
text/event-stream
Which header helps an SSE client resume after a drop?
Last-Event-ID.
What ends an SSE event in the stream?
A blank line.
Is SSE one-way or two-way?
One-way: server to client.
Go deeper
Four layers. Stop at the one your interview needs; each ends with a quick check.
The core ideaEvery round
The four fields are data, event, id and retry (reconnect delay in milliseconds). Lines starting with : are comments, often sent as keep-alives so proxies do not close an idle connection Source: HTML Standard: Server-sent events.
Check yourself: What does retry: 5000 in an SSE stream do?
Sets the reconnection delay to 5 seconds.
How it works underneathSenior rounds
Check yourself: Why do proxies sometimes break SSE?
They buffer or compress the response, so events arrive late or all at once.
Edge cases and trapsWhere people slip
EventSource cannot set custom headers; use cookies or a token in the URL.Check yourself: Why did SSE suffer on HTTP/1.1 with many tabs?
Each stream holds one of about six connections per origin.
In productionStaff-level answers
fetch and a stream reader, because the request is a POST with headers, which EventSource cannot send Source: MDN: Using server-sent events.Check yourself: Why do LLM apps often use fetch streaming instead of EventSource?
EventSource only does GET without custom headers; fetch can POST a prompt with auth headers.
Finished the five steps? Mark it, and its cards join your reviews.
Last checked 2026-11-01. Spot a mistake? Use “Report an error” below — fixes are listed on the corrections page.
Interview questions this answers
- Compare polling, long polling, SSE and WebSockets.
- Why might SSE be a better choice than WebSockets for some features?