System Design Fundamentals
Vote

0% completed

What is Long-Polling?

How Long-Polling Works

Strengths of Long-Polling

Weaknesses of Long-Polling

Where Long-Polling Is Used

Long-polling is a technique where the client asks the server for data, and the server holds the request open until it actually has data to send. Instead of answering "nothing yet" every few seconds, the server stays quiet until there is real news.

Think of calling a support hotline. Regular polling is calling every five minutes to ask "any update?" and hearing "no" almost every time. Long-polling is calling once and staying on hold. The moment there is an update, the agent tells you.

Because the request just hangs there waiting for an answer, long-polling is sometimes called a "Hanging GET". (A GET is the standard HTTP request type for fetching data.)

How Long-Polling Works

The life cycle of a long-polling application looks like this:

  1. The client sends a request to the server using regular HTTP.
  2. The server does not respond right away. It holds the request open, waiting for new data.
  3. When new data becomes available, the server sends a complete response to the client.
  4. The client immediately sends a new request. This way, the server almost always has a waiting request it can use to deliver the next update instantly.
  5. Every request has a timeout. If the timeout passes with no data, the server sends an empty response, and the client simply opens a new request.
Long-polling: the server holds each request open until it has data or the request times out
Long-polling: the server holds each request open until it has data or the request times out

The result feels close to real time. An update leaves the server the moment it exists, because a request is usually already waiting for it.

Strengths of Long-Polling

  • It is just HTTP. Every browser, firewall, proxy, and load balancer already understands it. Nothing special to install or configure.
  • Near real-time updates. The server pushes data as soon as it is available, instead of waiting for the next poll.
  • Simple to build. It reuses the plain request and response model that every web developer already knows.

Weaknesses of Long-Polling

  • One answer per request. After every response, the client must open a new request. Each round trip repeats the full HTTP overhead, and there is a small gap where an update has no waiting request to ride on.
  • Many open connections. The server holds a waiting request for every connected client. Holding thousands of open requests consumes server memory and connection capacity.
  • Ordering can get tricky. If a client has more than one request in flight, messages can arrive out of order, and the application has to deal with that.

Where Long-Polling Is Used

Long-polling is a solid choice for notification delivery and simple chat, and it shines as a fallback: when a corporate network or an old proxy blocks fancier options, long-polling almost always still works. Many real-time libraries try WebSockets first and quietly fall back to long-polling when the connection fails.

In the next lesson, we will look at What is WebSocket?, a protocol that removes the reconnect-after-every-message limitation entirely.

On This Page

How Long-Polling Works

Strengths of Long-Polling

Weaknesses of Long-Polling

Where Long-Polling Is Used