Long Polling
Polling in computing refers to the act of repeatedly checking a resource for
updates. A client sends requests at regular intervals to inquire if new
information is available.
While conceptually simple, this method becomes problematic when scaled.
1. Chattiness: Frequent, unnecessary network requests result in high
overhead and low utility, as most responses are often empty.
2. Scalability Issues: When applied to large-scale systems, frequent polling
can overwhelm backend resources, leading to degraded performance.
3. Latency: Infrequent polling may cause delays in delivering updates, making
it unsuitable for real-time systems.
Polling highlights a fundamental tradeoff: request frequency versus
responsiveness. This sets the stage for long polling, which resolves these
challenges.
Long polling is a refinement of the polling paradigm. Instead of returning
immediately with an empty response if no data is available, the server holds the
request open until new data arrives or a timeout occurs. The client remains in
an idle, asynchronous state during this period.
1. Client Request: The client sends a request to the server asking for updates.
2. Server Wait: The server retains the connection, monitoring for updates.
3. Response: If new data becomes available within the wait period, the server
responds immediately. Otherwise, it times out gracefully.
4. Repeat: The client immediately sends a new request upon receiving a
response.
Benefits
Long Polling 1
1. Reduced Chattiness: By responding only when necessary, long polling
minimizes redundant network requests.
2. Improved Scalability: Fewer requests reduce the load on backend
resources, enabling systems to serve more clients efficiently.
3. Enhanced User Experience: Long polling provides near-real-time updates
without the latency inherent in traditional polling.
Challenges
1. Timeouts and Middleboxes: Intermediate network devices or proxies may
terminate idle connections prematurely, resulting in gateway timeouts.
2. Connection Management: Maintaining open connections for numerous
clients requires careful resource allocation on the server.
3. Queue Management: Efficiently managing pending and processed
requests in the backend ensures predictable performance.
Long polling can be applied beyond simple data retrieval. Complex tasks like
report generation, batch processing, or query-intensive operations can benefit
by decoupling client interactions from synchronous backend processing.
Steps:
1. Job Queueing: Incoming requests are stored as tasks in a queue for
asynchronous processing.
2. Task Processing: Worker threads or processes handle tasks from the
queue.
3. Response Notification: Once processed, results are stored, and clients are
notified through their open long-polling connections.
Example:
Client submits a request to generate a report for a specific date range.
The server places the request in a queue and responds with a job ID.
The client periodically polls the server with the job ID to check the status.
Long Polling 2
When the report is ready, the server sends the data in response to the
client’s polling request.
Long polling elegantly bridges the gap between traditional polling and push-
based communication. By intelligently waiting for events, it optimizes resource
utilization and improves the user experience in real-time applications. However,
its effective implementation requires careful consideration of network
constraints, timeout handling, and scalability.
Long Polling 3