SkillSprout
Chapter 28 · Lesson 1
Intermediate
3 min read

Polling and Long Polling

Overview

A normal web request works like a question and an answer: the client asks, the server replies. But chat messages, notifications and live scores need the server to tell the client something new. Polling and long polling are the simplest tricks to get near real-time updates using ordinary requests.

Detailed Notes

What is polling

In normal HTTP the client always starts. The server cannot send anything unless asked. So how does a page learn about a new message?

Polling means the client simply asks again and again on a timer: "Anything new?" every few seconds.

The problem it solves

Without some technique, users would have to refresh the page to see updates. Polling gives updates with no special server features. Its weakness is waste: if updates are rare, most requests return "nothing new", yet each one still costs a network round trip and server work. A shorter interval means fresher data but even more waste; a longer interval means slower updates.

How it works

Short polling: ask every N seconds. Simple, but delay up to N seconds and many empty responses.

Long polling: the client sends a request, and the server holds it open without answering until something new happens (or a timeout, such as 30 seconds, passes). When data arrives, the server responds at once. The client processes it and immediately sends a new request. At any moment there is one waiting request per client.

Practical details:

  • Send a cursor (such as the last event ID) with each request, so no update is missed or repeated between requests.
  • Keep the server timeout shorter than the idle limits of any proxy or load balancer in the path, or they will cut the request.
  • If a request fails, retry with backoff (waiting longer each time) so thousands of clients do not stampede the server.

A simple example

Think of waiting for a package. Short polling is walking to the door every 5 minutes to check. Long polling is standing at the door until the courier arrives, then going back to wait for the next one.

System design example

A support-ticket page needs a notification when an agent replies. With long polling, the browser sends GET /updates?after=812. The server waits. When a reply with ID 813 is stored, the server returns it; the browser shows it and immediately asks after=813. It works through every proxy and firewall, since it is just HTTP. The cost: each waiting client holds a connection open, so servers must be built to hold many idle connections cheaply, and each message costs a full new request.

Summary

  • Plain HTTP lets only the client start a conversation.
  • Short polling asks on a timer: simple but wasteful and delayed.
  • Long polling holds the request until data arrives: faster updates, still plain HTTP.
  • Use a cursor so no events are lost, and timeouts below proxy limits.
  • Good for infrequent updates or restricted networks.

Architecture Diagram

Long polling cycle
Architecture
Client
Load Balancer
App Serverwaits for event
Event Source

The server holds the request until an event arrives.

  1. 1The client sends a request with a cursor and the server holds it.
  2. 2An event arrives from a queue or another service.
  3. 3The server responds immediately with the event.
  4. 4The client processes it and sends the next request right away.

Common Interview Questions

Finished this lesson?

0 of 91 lessons done · 0% of the course