Skip to content
matthewcm.dev

    ↑ ↓ move · ↵ open · tab scopeopen graph →

    AJAX Polling

    The simplest form of "real-time" — ask the server every N seconds. Wasteful at scale, perfectly fine more often than people admit, and the baseline every other technique is compared against.

    Growingtended planted 1 min read
    On this page

    Short polling is setInterval(fetch, n): the client repeatedly asks “anything new?” and the server replies, usually with nothing. Latency is up to n seconds and most responses are empty. And yet it’s often the right call.

    When polling is fine

    • Updates are rare or freshness isn’t critical — a job status that changes every few seconds, a dashboard refreshed every minute.
    • The infrastructure doesn’t support long-lived connections.
    • You want the simplest thing that works and can be cached, retried and load-balanced like any other request.

    Making it cheap

    The cost is easy to estimate: requests per second ≈ clients ÷ interval. A hundred thousand clients polling every five seconds is twenty thousand requests a second of mostly nothing. So:

    • Use conditional requests (ETag / If-None-Match) so unchanged data returns a tiny 304 Not Modified.
    • Back off when the tab is hidden (document.visibilityState).
    • Add jitter so clients don’t synchronise into spikes.
    • Let a data library do it: TanStack Query’s refetchInterval or SWR’s refreshInterval handle most of this for you.

    The four techniques side by side

    TechniqueDirectionLatencyServer costUse when
    Short pollingclient asksup to the intervalmany empty requestsrare updates, simplest infrastructure
    HTTP Long Pollingserver answers when readylowheld connectionsfallback, legacy, job queues
    Server-Sent Events (SSEs)server → clientlowone connection per clientfeeds, progress, notifications, token streams
    WebSocketsboth wayslowestone stateful connectionchat, collaboration, games, trading

    In practice

    On a workflow platform I worked on, process status views were a textbook polling case: status changed every few seconds, nobody needed millisecond freshness, and polling with a sensible interval was far simpler to operate than a socket infrastructure. Pick the technique from the freshness your users actually need, not from what sounds most impressive.

    • #polling
    • #real-time
    • #http
    • #system-design

    ↪ Links to (3)