What is long polling in system design?

Mondo Technology Updated on 2024-01-30

When developing web applications that process real-time data, it's important to consider how to effectively deliver the data to the client. One option is long polling.

Quality Author List Let's start by looking at what polling is and how it scales to long polling. Polling is a technique that allows a server to send information to a client. Long polling, on the other hand, is a variation of traditional polling that enables the server to send data to the client when it becomes available. The client sends a request for information to the server, but the server may not respond immediately. Instead, it waits until the data is available and then sends a full response to the client. Long polling is an effective way to send information to clients because it reduces the number of HTTP requests required to transmit the same amount of data. However, it can be a challenge for servers to manage unfulfilled client requests and process new information that is available before a client makes a new request.

One of the advantages of long polling is that it is part of the HTTP protocol, which makes it widely accepted and produces less bandwidth than short polling. Overall, long polling is a reliable way to keep customers informed of new information in real time. Here's the basic flow for an application that uses HTTP long polling:

The client sends an HTTP request and waits for a response from the server.

When an update is available, the server provides a complete response to the client.

The client then sends a new long polling request immediately or after a pause to allow for an appropriate delay.

Set a timeout for each long polling request. If the connection is lost due to a timeout, the client must re-establish the connection periodically.

Long polling is a more efficient version of traditional polling because it reduces the number of requests that need to be made to the server. In traditional polling, each request needs to establish a new connection, parse HTTP headers, issue new data queries, and generate and deliver responses. The connection will then terminate and clean up the resources.

Long polling allows the server to keep client connections open for as long as possible, responding only when data is available or when a timeout threshold is reached. This avoids the need to repeat the process for each client before new data is available, which helps save resources.

In long polling, most of the work is done on the server side. On the client side, only one request to the server needs to be managed. When the client receives a response, it can make a new request and repeat the process as needed. From a client's perspective, the main difference between basic and long polling is that a client performing basic polling may deliberately leave a small window of time between each request to reduce server load. It may also handle timeouts differently than servers that don't support long polling.

For example, with long polling, the client can be configured to allow longer timeouts while waiting for a response. This is done to avoid problems with communication with the server, which may be identified by a timeout period.

In addition to these considerations, basic polling already covers other things that the client needs to do. On the other hand, the server has to manage the state of multiple unresolved connections. When using multiple servers and load balancers, you may need to create a solution to preserve session state. The server must also gracefully handle connection timeouts, which are more common in long polling than in private protocols.

Building real-time interactivity into an application using HTTP long polling requires a variety of considerations, including development and scaling aspects. Some questions to consider include:

As usage grows, how will you manage the real-time backend?

Does long polling automatically re-establish connectivity when a mobile device switches between networks or loses connectivity, or when an IP address changes?

Can you manage message queues and make up for lost messages with long polling?

Does long polling provide load balancing and failover support across multiple servers?

Keep in mind that long polling is only a temporary version of the underlying request-response mechanism, which adds complexity to its implementation. So, these are important factors to consider when using long polling in your application.

When building a real-time application with HTTP long-polling server pushes, you must develop a communication management system. This means that you will be responsible for updating, maintaining, and scaling your backend infrastructure.

Long polling can create challenges for reliable message ordering. This is because the same client may make multiple HTTP requests at the same time. For example, if a client opens two browser tabs and consumes the same server resources, or if a client implementation uses multiple connections at once, there may not be a guarantee that duplicate data will be written only once.

Another problem is that the server may send a response, but a network or browser issue may prevent the message from being received successfully. If the message receipt acknowledgment process is not implemented, subsequent calls to the server may result in message loss.

One of the big challenges with using long polling is that it can be difficult to scale due to its complexity. Maintaining the session state of a given client requires sharing it among all servers behind the load balancer, or routing subsequent client requests in the same session to the same server that processed the original request. This can result in uneven load distribution and can lead to overloading of individual servers in the cluster.

HTTP long polling is a way for a server to send data to the client independently, allowing the server to push data to the client in real-time when it is available. However, it is most effective when messages from the server are few and not too frequent.

Related Pages