Polling and long polling: asking until there is news
The server has news and cannot say it
Section titled “The server has news and cannot say it”HTTP is request and response: a server answers a request, and it has no way to start one. When a client wants to hear about something that happens later, such as a finished job, a new message or a changed price, it has to ask. RFC 9110’s 202 Accepted shows the same limit from the server’s side: the request has been accepted for processing but not completed, and there is no facility for the server to send a status code later.
The oldest answer is to keep asking. RFC 6455 describes how developers did this before WebSocket existed: HTTP polling, at the price of a high overhead, because each client-to-server message carries an HTTP header.
Mental model. Short polling is a child in the back of the car asking “are we there yet?” every minute. Long polling is asking once and being told “I’ll tell you when we are”: the answer comes when there is one, and then the child asks again.
Short polling
Section titled “Short polling”The client asks every interval, and the server answers at once, with news or with nothing. Two things follow from that.
First, the load does not depend on the news. With 10,000 clients that poll once a second, the server receives 10,000 requests a second. If a client has news once a minute on average, 1 answer in 60 carries something and the other 59 are empty. The server does the work of 10,000 requests a second to deliver about 167 updates a second.
Second, the delay depends on the interval. Whatever happens just after a poll waits for the next one, so the news arrives up to one interval late, and on average half an interval late. A shorter interval cuts the delay and multiplies the requests: polling every 100 ms is 10 times the load of polling every second.
Long polling
Section titled “Long polling”Long polling changes who waits. RFC 6202 describes it: the server defers its response until an event occurs or a timeout expires, and the client sends a new request as soon as it gets a response, so that a request is always pending and available for the server to answer. There are no empty answers in the ordinary case, and the news goes out as soon as it exists.
It has its own costs, and RFC 6202 lists them:
- The server holds one connection and one pending request for each client, and the RFC says the resources for the requests can be significant.
- Intermediaries may close a connection that stays quiet, so the server answers before that happens. The RFC says that 30 seconds is generally a safer value for the timeout, and that the long poll then ends with an empty answer and a new request.
- Each answer and each new request carries complete HTTP headers, which can be a large share of the bytes when the messages are small and rare.
- After an answer the client has to send a request before the next news can go out. RFC 6202 puts the maximal latency of a later event at over three network transits: the response, the next request and the next response.
Try it: two updates
Section titled “Try it: two updates”The flow below sends two updates to a client by four techniques. Two updates are waiting. The first is ready on the server as soon as the client’s first request has arrived, which for short polling is just after the server answered the first poll, the worst moment. The second is ready the moment the first reaches the client. Move Round-trip time and Polling interval, pick Short polling and Long polling, and step through the messages. This lesson reads the first two; the next lesson reads the others.
How an update reaches the client
The client asks every interval whether there is anything new, and the server answers at once, with or without an update. An update that appears just after a poll waits for the next one.
- Opening the channel40 msClient to Server: GET /updates (poll 1). Arrives at 40 ms.GET /updates (poll 1)
- 80 msServer to Client: 200, no update. Arrives at 80 ms.Processing: 1 s200, no update
- First update1.1 sClient to Server: GET /updates (poll 2). Arrives at 1.1 s.GET /updates (poll 2)
- 1.2 sServer to Client: 200 + update 1. Arrives at 1.2 s.Processing: 1 s200 + update 1
- Second update2.2 sClient to Server: GET /updates (poll 3). Arrives at 2.2 s.GET /updates (poll 3)
- 2.2 sServer to Client: 200 + update 2. Arrives at 2.2 s.200 + update 2
Time until the client has both updates: 2.2 s3 round trips on the wire · 2 s of processing
Model notes
Assumptions
- Each message takes half the round-trip time of the link it crosses. Messages are never lost, never queue and never overlap, and each leaves when the one before it has arrived, once its receiver has finished any processing.
- Two updates are waiting for the client. The first is ready at the server as soon as it has accepted the client’s first request; for polling, that is just after it answered the first poll, which is the worst moment for polling. The second becomes ready the moment the first reaches the client. The connection to the server is already open, as in the connection-setup flow.
Simplifications
- The wait between two polls is shown as processing time. The client waits for the interval after each answer; a client that polls on a timer measures the interval from the previous request instead.
- The headers of an event stream and its first event are drawn as one message, and so are the WebSocket 101 response and the server’s first frame. A server can send them in the same flight, and the client sees the update when they arrive.
- A long poll that waits too long is answered with nothing after a timeout and the client asks again; an event stream or a WebSocket that drops is reopened by the client after a delay; proxies that close idle connections are not drawn.
- Only time and the number of messages are counted. The size of the HTTP headers that every poll repeats, the cost of keeping a connection open per client at the server and the load of many short requests are not modelled.
Parameter values are illustrative. They are not benchmarks of any real product.
At a round-trip time of 80 ms and a polling interval of 1 s, short polling sends 6 messages. The first update reaches the client at 1.2 s, after the empty answer, the wait of one interval and the second poll. The client has both updates after 2.2 s: 3 round trips on the wire, which is 240 ms, and 2 s of processing, the two waits of one interval.
Long polling sends 4 messages. The first update arrives after 80 ms, one round trip from the first request, because the server was holding it. The second update has to wait for the client’s new request to cross to the server, and the client has both after 160 ms: 2 round trips on the wire.
Now cut the interval of short polling to 100 ms. The total falls to 440 ms, and it is still far from long polling’s 160 ms, with 10 times as many requests a second in a real system. Raise the round-trip time to 200 ms and the interval to 5 s: short polling needs 10.6 s, and long polling at 200 ms needs 400 ms. The interval is paid twice in short polling, and the round trips are all that long polling pays.
These are not measurements. The flow counts only the round trips and the wait that the interval adds. It never loses a message, never queues one and has no server work, so it shows the floor that the protocol’s messages set. A real long poll still has to hold a connection per client, and a real short poll still costs the server a request every interval; the flow does not charge either.
What happens if you poll ten times as often?
Section titled “What happens if you poll ten times as often?”The delay shrinks, and everything else grows. At a 100 ms interval, 10,000 clients send 100,000 requests a second, nearly all empty, each with its headers. The server has to answer them, and so do the load balancer and every proxy in between. The polling itself can become most of the requests the system serves. Long polling avoids this by keeping the request open, and pays with open connections. The right answer depends on how often there is news. If the news is frequent and a delay of an interval is acceptable, short polling at a long interval is the simplest. If the news is rare and should arrive at once, hold the request.
- A server cannot start a request. A client that wants news has to ask, and polling is how.
- Short polling asks every interval, answered at once. The load is clients divided by the interval: 10,000 clients every second is 10,000 requests a second, and if news comes once a minute, 59 answers in 60 are empty.
- Long polling holds the request until there is news, and the client asks again as soon as it gets an answer. It costs a pending request per client and a timeout, about 30 seconds in RFC 6202’s advice.
- In the flow, at 80 ms and 1 s, short polling needs 6 messages and 2.2 s for two updates, and long polling needs 4 messages and 160 ms.
- The flow is a floor from round trips and the interval, not a measurement.
Next: server-sent events and WebSockets, a channel that stays open.
Your progress
Built by GhaziAlibi.
© ShardCraft. All rights reserved.