Engrain by ByteRush

WEB.16.01 Mid-level· 11 minCore — asked often

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/3

On the client: new EventSource("/score/ind-aus").addEventListener("score", (e) => show(e.data)).

A live score over SSE, with a dropped connectionStep 1 of 4

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
    1. The page opens new EventSource('/score/ind-aus'). The browser sends one GET with Accept: text/event-stream.
    2. The server writes an event whenever the score changes: id, event name, data, then a blank line.
    3. The user's train enters a tunnel. The connection drops. EventSource waits, then reconnects on its own.
    4. 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?

    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.”

    Stays on your device. Nothing is uploaded.
    Follow-up Compare polling, long polling, SSE and WebSockets.
    Polling: periodic requests. Long polling: a held request per update. SSE: one-way stream over HTTP with auto-reconnect. WebSocket: full-duplex messages over an upgraded connection.
    Follow-up Why might SSE be a better choice than WebSockets for some features?
    For one-way updates (scores, notifications, LLM tokens) it is plain HTTP: simpler servers, works with HTTP/2, proxies and auth cookies, and reconnects for free.
    Follow-up How do LLM chat apps stream tokens?
    Usually SSE or a streamed fetch response: the server writes each token chunk as it is generated (AIF.01.01).

    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 data is joined with newlines; the type defaults to “message”.
    • The last id carries over to later events.
    Your solution
    Ctrl/⌘ + Enter

    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

    Long polling costs one request per update and needs care with timeouts.
    SSE needs servers and proxies that flush each event immediately; buffering proxies (and some compression settings) deliver events in bursts.

    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

    On HTTP/1.1, each SSE stream holds one of the roughly six connections per origin, so many tabs can starve other requests. HTTP/2 multiplexes streams on one connection, so this limit disappears.
    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
    LLM APIs stream tokens in the SSE format, but apps often read them with 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?