Polling
Asking again and again.
A customer has ordered dinner and stares at the tracking screen. The order status changes from 'preparing' to 'picked up' to 'arriving' on the server, but a web page can only show what it knows. The simplest way to learn about changes is to ask: every few seconds the page sends a request, 'any news on order 812?'. This is polling, and it is the first thing to try because it needs nothing beyond ordinary HTTP.
async function pollOrder(id: string) {
while (true) {
const res = await fetch("/orders/" + id + "/status");
render(await res.json());
await sleep(5000); // ask again in 5 seconds
}
}Polling has real strengths. Every request is independent, so any server behind the load balancer can answer it. Caches and CDNs work. Failures heal themselves: a missed poll is followed by another. For a status page that changes a few times an hour, polling every 10 to 30 seconds is perfectly sensible.
The costs come from arithmetic. If 100,000 customers have a tracking screen open and each polls every 5 seconds, the servers see 100,000 / 5 = 20,000 requests per second. Most answers are 'no change', so nearly all of that work is wasted. Shorten the interval to 1 second for snappier updates and the load becomes 100,000 requests per second. Lengthen it to 30 seconds and the load drops to about 3,300, but the screen can be up to 30 seconds late.
Making polling cheaper
Step 1 of 4
Use conditional requests
Send the last known version (an ETag or If-Modified-Since). If nothing changed, the server replies 304 with no body, saving bandwidth and rendering work.
Your turn
You add a 'driver location' map that must update within 3 seconds for 20,000 active deliveries. Estimate the polling load and say whether you would still poll.
Stuck? Worst-case delay is the interval, so the interval is at most 3 s.
What to remember
Polling means the client asks repeatedly over plain HTTP.
Load equals clients divided by interval; most responses carry nothing new.
Use conditional requests, caching, backoff and jitter to cut cost.
It is the right first choice for slow-changing, low-scale data.