Stackbook Logo
distribution-communicationlegacy · low operational burden

Long Polling

Also known as: comet, hanging-get

Intent

Approximate server push by holding a request open until data is available or a timeout expires.

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.

Forces

  • •Updates arrive at unpredictable times
  • •Must work on plain HTTP everywhere
  • •Request volume and server connections both cost money
  • •Events must not be missed between requests

Solution

Overview

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.

How it works

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.

Pitfalls

  • Missing cursor. Without it, any event in the reconnect gap vanishes.
  • Blocking threads. Use async I/O or waiting clients will exhaust the thread pool.
  • Hot loops on errors. Apply exponential backoff with jitter when requests fail.
  • Wake-all stampedes. Wake only the waiters interested in the changed topic.
  • Using it for streams of many events per second; the per-request overhead dominates.

Key takeaways

  • Hold the request until data or timeout, then re-request with a cursor.
  • Keep hold time under proxy idle limits.
  • Use async servers and targeted wake-ups.
  • Prefer SSE or WebSockets where available; keep long polling as a fallback.

✓ When to Use

  • •Fallback when SSE or WebSockets are unavailable
  • •Low-frequency events needing prompt delivery
  • •Legacy or restrictive network environments

✗ When Not to Use

  • •High-frequency event streams
  • •Environments where SSE or WebSockets work fine
  • •Thread-per-request servers with many waiting clients

Pros

  • +Works over any HTTP infrastructure
  • +Lower latency than short polling
  • +Simple to add to an existing API

Cons

  • −Reconnect overhead per event batch
  • −Holds server resources while waiting
  • −Ordering and gap handling are manual

Cost Profile

Infrastructure

Low - plain HTTP servers

Operational

Medium - connection capacity planning

Cognitive

Low - simple idea

Failure Modes

  • 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

Real-World Examples

Alternatives

    Related Patterns

    • webhook

    Competency Domains

    distribution communicationreliability ops