skip to content

How do short polling and long polling differ in what the HTTP server does with each request for updates?

level: juniorimportance: must knowfreq 84%

answer

  1. two ways to fake push
  2. who waits: client timer or server
  3. empty answer versus parked request
  4. one response per request, always
  5. hold expires, client re-issues at once

basics

~20 s

Short polling answers every request immediately, empty or not, so the client re-asks on a fixed timer. Long polling holds each request open until an update exists or the server's hold period expires, then answers once.

solid answer

~50 s

Both are ordinary HTTP request/response exchanges; they differ in who waits. In **short polling** the client owns a timer and asks every N seconds; the server answers at once even when there is nothing, so an empty response is normal and freshness is bounded by the interval. In **long polling** the client sends the same request but the server parks it until something is published or its own bounded hold period expires, then answers; the client re-issues immediately on receiving anything. That gets an update out roughly one network hop after publication instead of waiting for a timer, and it costs fewer requests while idle — fewer, not none, since every hold expiry still costs an exchange. What it does not change: one response per request, and a gap between the response and the next request in which nothing of that client is open.

code

http · 9 lines
http
GET /pivot/42/events?since=1732 HTTP/1.1
Host: fleet.example.net
Accept: application/json

HTTP/1.1 200 OK
Content-Type: application/json
Content-Length: 13

{"events":[]}

go deeper

for a junior

Recall the one-line difference: with short polling the client waits on a timer and the server answers at once; with long polling the server waits and answers when there is news or its hold expires.

for a middle

Explain what each shape costs while idle — requests equal to clients divided by the interval, versus clients divided by the hold period — and why long polling still pays a full exchange per cycle.

for a senior

Demonstrate that you know long polling is a request sequence, not a channel: one response per request, a gap after every response, and server state parked for each idle client.

for a principal

Frame the choice as a bet on update rate and operational appetite, and be able to say what the older techniques still buy you on paths that refuse to carry anything else.

Long before any protocol let a server speak first on its own initiative, applications faked live updates with ordinary HTTP requests. Two shapes emerged. They use the same exchange, the same fields and the same status line, and they differ in exactly one place: **who waits**. ## Short polling: the client waits The client asks on a fixed timer — every five seconds, every minute — and the server answers **immediately**, whether or not there is anything new. An empty answer is a normal answer: a status line, the response fields, and a body that says there is nothing. - The only clock that matters is the client's interval. - Worst-case freshness is one full interval plus a round trip: an update published the instant after a poll waits for the next one. - On a workload that changes rarely, nearly every request is wasted work that still pays the full price of an exchange in both directions. - Nothing about the exchange is unusual, which is the point — every intermediary, cache, log and client on the path already understands it. ## Long polling: the server waits The client sends the same kind of request, but the server does **not** answer an empty result straight away. It parks the request, registering it as interested in a channel, and returns only when an update is published or its own bounded hold period expires. On receiving any response at all, the client issues the next request immediately. 1. The client sends a request carrying the position it has already consumed. 2. The server checks for anything published after that position; if there is something, it answers at once. 3. If there is nothing, it holds the request, up to a hold period that is commonly tens of seconds. 4. On a publish it answers with what it has; on hold expiry it answers with an empty result. 5. The client processes the response and re-issues immediately. The cycle repeats. The latency win is real: an update published while a request is parked goes out roughly one network hop later rather than waiting for a timer. The cost is that the server now holds state for every idle client — the parked request, its channel registration, and whatever runtime resource the request occupies. ## Side by side | | short polling | long polling | |---|---|---| | who waits | the client, on its own timer | the server, holding the request | | answer when nothing is new | immediate and empty | delayed until the hold period expires, then empty | | freshness | up to one interval late | about one round trip after publication | | requests while idle | clients divided by interval | clients divided by hold period | | server state per idle client | none between polls | one parked request | ## What long polling does not change This is where candidates over-claim. Long polling is not a persistent channel; it is a sequence of ordinary requests with a delayed answer. - **One response per request, always.** The server cannot send a second update down a response it has already completed. The next update needs the next request. - **There is a gap.** Between the response landing and the next request arriving, nothing of that client's is open on the server, and anything published in that window has to be retained somewhere or it is gone. - **The per-exchange overhead is still paid.** Every cycle carries a request line, the client's whole field set, a status line and response fields — just less often than a short poll on a tight interval. - **The client's immediate re-issue is not a retry.** It is the normal cycle; treating it as error recovery leads to backing away from perfectly healthy empty answers. ## Where the names come from The family of techniques was called **Comet** in the mid-2000s, as a label for making a web page look live over a protocol that had no way of doing so. `RFC 6202` is the document that surveys the field and gives the two techniques their standard names: **HTTP Long Polling**, the shape described above, and **HTTP Streaming**, in which the server keeps one response body open and writes into it repeatedly. Two protocol families of that era, **Bayeux** and **BOSH**, were built on those transports — the first a general publish/subscribe protocol carried over HTTP, the second a way of carrying an always-on messaging session over it. The lineage matters for one practical reason: these are still the techniques you fall back to when something on the path refuses to carry anything more exotic.

  • What do the terms Comet, HTTP Long Polling and HTTP Streaming refer to, and how do they relate?
    Comet was the mid-2000s umbrella name for making a page look live over plain HTTP. `RFC 6202` surveys that work and names two techniques: HTTP Long Polling, where the server delays the answer to a request, and HTTP Streaming, where it keeps one response body open and writes into it repeatedly. Bayeux and BOSH are protocol families built on those transports.
  • Under the HTTP/1.1-era guidance that a client keep at most two connections per host, what did a parked long poll cost?
    It occupied one of the two, leaving a single connection for everything else that host served. A second parked request could consume both and leave nothing for ordinary loads, so implementations of that era went to some trouble to keep exactly one long poll outstanding per host.
  • Can a client switch from short to long polling without any server change?
    No. The holding is entirely a server behaviour — it must park the request and register interest in a channel. The client's side of the change is small by comparison: drop the timer and re-issue as soon as each response lands, carrying the position the previous response returned.

Short polling is ringing the shop every ten minutes to ask whether the parcel arrived. Long polling is staying on the line until they have an answer or the receptionist's patience runs out, then calling straight back.

saying these in an interview costs you the question

  • Says long polling keeps one connection permanently open like a socket
  • Thinks short polling only sends a request when something changed
  • Believes the server can push a second update onto an already-answered request
  • Claims long polling removes per-request overhead rather than reducing it
  • Assumes a long poll never comes back empty
  • Confuses the client's poll interval with the server's hold period