Let’s think of building a news website. The website should be continuously updated with the latest news. How should this feature be designed and implemented?
The first approach involves continuously asking for updates at regular intervals, referred to as short polling.
Short Polling
My son is really impatient when it comes to food. Once he is hungry, the food should arrive at once else he keeps on saying, ‘where is food’… ‘where is food’…
Chapter 6 and Chapter 9 from my O’Reilly book ‘System Design on AWS’ captures in-depth knowledge on networking protocols and concepts. This article takes inspiration and contains a short summary from Chapter 6. Check out the book for end-to-end understanding and detailed trade-off analysis.
Most of the times the answer is NO, it’s almost prepared. Now compare this to the news example. Depending on the news domain, there might not be any new news but the client repeatedly sends regular HTTP requests every 5 seconds (short fixed intervals).
There are couple of disadvantages associated with this approach.
Waste of server resources, because most of the times the response is empty.
There is added latency because the client queries at fixed intervals. The update might have arrived at T=1sec but the client queried at T=5sec, adding a delay of 5 seconds.
It’s not all bad, there are good things about this approach as well.
Simple to implement and no overhead on server to manage the connections.
Caching implementation at proxy layer is easy.
To address the disadvantages, the next approach is long polling.
Long Polling
The patience level increases with age (my assumption :), I’ll let you know in couple of years). Instead of asking every second ‘where is food’ continuously, he asks one time and he is being told to wait for 5 minutes and don’t ask again till then.
This is long polling for you. Instead of polling at very short intervals, the client sends a request and the server holds the request for certain time say, a minute. If the response arrives within one minute then it’s returned to the client else the request times out. The client will reinitiate the request for timeout or on receiving response.
The advantages of this approach includes.
The behaviour is near real time as the latest news is returned the moment it arrives.
There are fewer network requests.
It’s never all 100% advantages, so what are the downsides?
There is connection overhead on the server.
There could be multiple idle connections consuming thread pools, causing resource contention.
Both short polling and long polling involves clients initiating a request and waiting for a response. What if the responsibility is handed over to the server? For any new updates, the server will send the data. The next two approaches involves communication from server to the client devices.



