Problem
Clients need near-real-time updates, but short polling wastes requests and still adds latency. Streaming protocols may be unavailable through some proxies or legacy clients.
Also known as: comet, hanging-get
Approximate server push by holding a request open until data is available or a timeout expires.
Clients need near-real-time updates, but short polling wastes requests and still adds latency. Streaming protocols may be unavailable through some proxies or legacy clients.
Long polling is an HTTP technique where the server delays its response until it has something to say. The client re-requests right after each reply, creating a loop that feels like push while using ordinary requests.
The client keeps a cursor representing the last event it processed.
loop:
GET /events?after=105&wait=30
server: if events after 105 exist -> return them now
else park request up to 30s
on new event -> return it
on timeout -> return 204 empty
client: update cursor, repeat immediately
The cursor is essential: events that occur between a response and the next request are returned immediately on the next call. The server parks requests in an event loop or async handler and wakes them through an in-memory registry or pub/sub channel. Hold time should sit safely below load balancer idle timeouts, for example 30 seconds against a 60 second limit.
Low - plain HTTP servers
Medium - connection capacity planning
Low - simple idea
Events lost in the gap between responses without a cursor
Proxy timeouts cut requests shorter than the hold time
Thundering herd when many waiters wake together