Short Polling vs. Long Polling: A Deep Dive into Polling Techniques.
Continuing my exploration of Backend Communication Patterns, this blog delves into the differences and intricacies of Short Polling and Long Polling.
In Short Polling the server responds immediately with the data or no data.
Whereas in Long Polling is a communication method where the client requests data from the server, and the server keeps the request open until there’s new data or a timeout.
This process allows the client to receive updates almost instantly without constantly making new requests, unlike short polling. Although not a true persistent connection like WebSockets, it provides a similar experience for applications where WebSocket support isn’t feasible.
Why Use Short Polling?
Short polling can be particularly useful in situations where:
- Real-time updates are not strictly necessary: For example, checking if a lengthy server-side process (like file processing or data analysis) has completed.
- Handling resource-intensive operations: If a server-side process is resource-heavy, the client can use short polling to receive updates about the operation’s progress without overwhelming the server with constant requests.
- Intermittent updates are sufficient: In scenarios where instant updates aren’t critical, short polling provides an effective way to update the client periodically.
Why Use Long Polling?
Long polling can be particularly useful in situations where:
- Live Notifications: Delivering updates like alerts, news, or social media notifications.
- Collaborative Applications: Synchronizing actions in shared tools like document editors or project boards.
Steps for Setting Up Short Polling
- The client initiates a request to the server to check if there is any new data.
- The server responds with any available data (or with an empty response if there’s no new data) immediately.
- The client repeats this request at regular intervals, refreshing the data in the user interface as necessary.
You can use any language to implement the frontend logic of polling every x seconds, here I am using Angular with Rxjs timer function.
return timer(0, 10000).pipe( // Poll every 10 seconds
switchMap(() => this.http.get<Status>(this.url + '/status', { headers }))
);Steps for Setting Up Long Polling
- The client initiates a request to the server to check if there is any new data but with extra steps .
- The Client modifies the header by setting
keep-Aliveset to larger timeout, let’s say 60 sec. - The client and Server hold the request until the server has any data or this 60s timeout occurs.
- Once the Client received either the data or timeout error, the client immediately sends another request to the server to maintain the connection.
- This repetitive cycle continues, giving a pseudo sense of real time communication.
For Long Polling Implementation, Use Thread.sleep or any language specific codes to pause the process to simulate the Long Polling.
And Use the following code, to Test your simulation
while ($true) {
curl.exe -X GET `
-H "Accept: application/json" `
-H "Connection: Keep-Alive" `
-H "Keep-Alive: timeout=60, max=1000" `
http://localhost:8080/long-polling
Start-Sleep -Seconds 60
}Pros of Short Polling
- Easy to implement with existing HTTP infrastructure.
- Useful for periodic updates without needing persistent connections.
- Simplicity reduces complexity for certain applications.
Cons of Short Polling
- High server load and wasted bandwidth with frequent requests.
- Delayed updates and poor experience for real-time needs.
Pros of Long Polling
- Supports near real-time updates with broad compatibility and simpler setup.
Cons of Long Polling
- Consumes more server resources and bandwidth, with scalability challenges.
Note: If there is option to go for WebSockets, Always prefer WebSockets. Long Polling will always be alternative to WebSockets.
Short Polling is fine to implement, But we must make sure that we are not going for infinite loop. If we upload a video and have a background process to finish. Once it’s finish we must stop polling.
