What Is Long Polling? Receive Server Updates by Holding an HTTP Request
Long polling is a technique where a client sends an HTTP request and the server holds its response until new data is available or a waiting period ends. After receiving the response, the client typically sends another request promptly to keep a poll pending. It can reduce empty responses compared with frequent short polling, but each event is delivered in a separate HTTP response and each pending request consumes server and proxy capacity. Consider it for low-latency, one-way updates such as notifications when SSE or WebSocket is not readily available.
Long polling is a technique where a client sends an HTTP request and the server holds its response until new data is available or a waiting period ends. After receiving the response, the client typically sends another request promptly to keep a poll pending. It can reduce empty responses compared with frequent short polling, but each event is delivered in a separate HTTP response and each pending request consumes server and proxy capacity. Consider it for low-latency, one-way updates such as notifications when SSE or WebSocket is not readily available.
How does the request and response cycle work?
With ordinary polling, a client checks at intervals and the server returns an empty response immediately when there is no data. With long polling, the server defers its response until an event, a state change, or a server-side wait limit occurs. When an event arrives, the server completes the response; the client processes it and starts the next long-poll request. RFC 6202 describes this as an HTTP-based server-push technique, but it does not define a new HTTP protocol or a shared message contract.
Client ── GET /updates?cursor=42 ──▶ Server
(request pending)
Client ◀── 200 + event + cursor=43 ─ Server
Client ── GET /updates?cursor=43 ──▶ ServerHere, cursor is an application-defined example. Even if no event or result is available, the server can return an empty result when its own wait limit expires. The client treats that as a normal “no new data yet” result and sends another pending request. Define empty timeout responses separately from error responses in your contract.
Design cursors and duplicate handling first
There is a short gap after one response and before the next request. If an event occurs during that gap, an update may be missed unless the server stores events or can query them by cursor. The client should include its last-seen event ID or cursor in the next request, and the server should return events after that position. Validate the cursor against the authenticated user and subscription; do not let arbitrary IDs expose another user's data.
The same event may be received again after an interrupted request. Give events stable IDs and make client processing idempotent or safely ignore duplicates. Long polling itself does not guarantee event storage, ordering, replay duration, or exactly-once processing. Define server retention and recovery for old cursors, such as a full state resynchronization.
Coordinate timeouts and retry behavior
If the wait is too short, timeouts and new requests happen frequently, increasing header and request-handling overhead. If it is too long, a browser, proxy, load balancer, or server may close the request first. RFC 6202 documents observations from its time; it does not define one safe timeout for all modern infrastructure. Check the client, proxy, load-balancer, and app-server limits in the actual deployment, then set the server's long-poll wait limit with margin below the shortest intermediary limit so it can return a normal empty result.
Treat a wait timeout, an ordinary empty response, authentication failure, and server overload as distinct outcomes. Immediate unlimited retries after network errors or 5xx responses can worsen an outage; use exponential backoff with jitter and a cancellable retry policy. Retry-After can tell clients how long to wait after relevant temporary unavailability or throttling responses. Cancel pending work when the request is aborted or the view is closed.
Manage server and intermediary capacity
Each pending request consumes an HTTP request slot, memory, a registered waiter, and sometimes connection resources. RFC 6202 warns that outstanding requests can become a capacity limit, especially with synchronous servers or proxies. Async I/O does not remove every limit. Design and monitor caps for concurrent pending requests, queue length, subscriptions per connection, cancellation, and fair resource allocation. With multiple server instances, shared storage or a pub/sub mechanism may be needed so event notification and cursor lookup are consistent across instances.
Intentionally disable caching for long-poll requests and responses so an intermediary does not reuse an old empty or event response. RFC 6202 recommends Cache-Control: no-cache. Check that proxy, load-balancer, and application idle timeouts are coordinated. HTTP connection reuse and the lifetime of a long-poll request are separate concerns. An HTTP/1.1 persistent connection can be reused after a response completes, while a pending request still occupies work and capacity until it completes.
Compare short polling, SSE, and WebSocket
| Method | Behavior | Advantage | Main cost or constraint |
|---|---|---|---|
| Short polling | Repeat requests and responses at an interval | Simple and easy to observe per HTTP request | Requests occur even with no data; latency and request rate depend on the interval |
| Long polling | Server waits for an event or timeout, then the client requests again | Fewer empty responses while retaining prompt event responses | Pending-request capacity, timeout coordination, gaps between requests, repeated response headers |
| SSE | Server continues sending events in one HTTP response stream | Browser EventSource plus reconnect and event-ID mechanisms | One-way; requires proxy-buffering and long-lived stream operations |
| WebSocket | Both parties initiate messages over a persistent connection | Bidirectional messaging over the same connection | More application responsibility for reconnect, message contract, and flow control |
The right choice depends on request rate, latency goals, concurrent clients, authentication, and operational infrastructure. Do not select using unmeasured benchmarks; observe request volume, pending requests, latency, retries, and resource usage under representative traffic.
Implementation checklist
Define how each request's authenticated user and subscription relate to its cursor, and verify cursor ownership.
Specify distinct responses for a new event, an empty wait timeout, an invalid cursor, authentication failure, and server error.
Give every pending request a server wait limit, cancellation, and resource cleanup. Keep the server limit safely below intermediary limits.
Include the last event ID/cursor in the response and require the client to send it on the next request. Define recovery when event history is unavailable or a cursor expires.
Use event IDs to detect duplicates and make client business processing idempotent. Account for uncertainty about whether a disconnected request was processed before an automatic retry.
Disable HTTP caching and verify proxy/load-balancer timeouts and connection behavior in the deployed configuration.
Cancel server waiters when clients leave or the network disconnects; limit concurrent pending requests and retry surges.
Sources
[RFC 6202: Known Issues and Best Practices for the Use of Long Polling and Streaming in Bidirectional HTTP](https://www.rfc-editor.org/rfc/rfc6202.html)
[RFC 9110: HTTP Semantics](https://www.rfc-editor.org/rfc/rfc9110.html)
[RFC 9112: HTTP/1.1](https://www.rfc-editor.org/rfc/rfc9112.html)