Long polling
A way to receive updates over HTTP: the server holds a client request until new data arrives or a timeout expires. Once the response finishes, the client sends another request.
Also known as: SSE, Server-Sent Events
What long polling does
Imagine a page waiting for an export to finish. With regular polling, the browser checks every few seconds and the server answers immediately, even if nothing has changed. With long polling, the browser asks once and the server waits until it has an update or reaches a time limit. If the export finishes while a request is pending, the server can report it right away instead of waiting for the browser's next scheduled check.
One request at a time
Say the browser has processed event 41. It sends that ID as a cursor, a marker telling the server where to resume:
GET /jobs/17/events?after=41 HTTP/1.1
Host: example.test
If a newer event is already available, the server returns it immediately. Otherwise, it waits for up to, say, 25 seconds. This endpoint might return 200 OK with a JSON array: one or more events if data arrived, or an empty array if the wait expired. Once the response ends, the browser makes another request with the latest cursor it has processed. A new request does not necessarily mean a new TCP connection: the underlying connection may be reused.
The cursor matters because events can arrive between requests, including while the browser is offline. If the server retains recent events, it can return anything after the last processed ID on the next request. Retrying with an older cursor can replay events, so clients should be able to recognize duplicates by ID. If the cursor is older than the retained history, the client may need to reload the current state before resuming.
When to use it, and what it is not
Long polling works well for occasional updates, such as job status changes or notifications, especially when the API already uses ordinary HTTP request-response handling. Compared with short polling, it usually sends fewer empty requests during quiet periods. With frequent updates, however, the client may spend more time repeatedly issuing requests, making a streaming approach worth considering.
Server-Sent Events (SSE) is not another name for long polling. SSE streams multiple events in a single HTTP response with the text/event-stream media type. Long polling ends the response after an event or batch and relies on the client to ask again. A WebSocket is different from both: it provides a channel on which either side can send messages without starting a new HTTP request for each one.
What interviewers look for
If asked how to support thousands of waiting clients, distinguish request rate from the number of in-flight requests. Asynchronous I/O can avoid tying up one thread per client, but each pending request still consumes resources and counts toward infrastructure limits. Set the application's wait limit below the shortest relevant intermediary timeout so a quiet period produces a normal response rather than a gateway error. Reissue the request promptly after a normal response, but use backoff with a random spread after failures so clients do not all retry at once.