What is Short Polling
Short polling is the simplest (and most "old-school") way for a client (like your web browser) to get updates from a server.
In this model, the client repeatedly asks the server, "Do you have new data for me yet?" at fixed intervals (e.g., every 5 seconds). The server responds immediately, even if there is nothing new to report.
How it Works
Think of short polling like a kid in the backseat of a car on a road trip. Every two minutes, they ask, "Are we there yet?" * If the answer is No, the parent says "No" immediately.
If the answer is Yes, the parent says "Yes."
Either way, the kid waits another two minutes and asks the exact same question again.
The Workflow
Request: The client sends an HTTP request to the serve
Immediate Response: The server checks its database or state.
If there is new data, it sends it back (200 OK).
If there is no data, it sends back an empty response (204 No Content or 200 OK with an empty list).
Wait: The client waits for a predefined "sleep" period.
Repeat: The process starts over from Step 1.
Pros and Cons
| Pros | Cons |
| Simple to implement: Uses standard HTTP requests; no complex libraries needed. | High Overhead: Each request requires a new connection (headers, handshakes), wasting bandwidth. |
| Server-side Simplicity: The server doesn't have to "hold" connections open; it just answers and forgets. | Latency: If data arrives 1 second after a poll, it sits there until the next poll occurs. |
| Wide Support: Works on every browser and network since the beginning of the internet. | Scaling Issues: 1,000 clients polling every 2 seconds creates 500 requests per second, even if nothing is happening. |
When should you use it?
Short polling is generally considered inefficient for modern, high-scale apps, but it is still useful for:
Low-frequency updates: Checking if a long-running background job (like a PDF extraction!) is finished once every minute.
Simple Admin Dashboards: Where real-time speed isn't critical.
Environments with strict firewalls: Where persistent connections (WebSockets) are blocked.
Comments
Post a Comment