Polling
Polling is a technique where a client sends repeated requests to a server at regular intervals to detect changes — such as a task completing, a file becoming available, or new data arriving. It’s the simplest way to monitor asynchronous work without webhooks or WebSockets.
How Polling Works
- Client sends a request to check the status of a resource
- Server responds with the current status
- If not done, client waits a fixed interval and checks again
- Once the status indicates completion, client stops polling
const pollForResult = async (jobId) => {
const res = await fetch(`/api/jobs/${jobId}`);
const data = await res.json();
if (data.status === 'completed') {
return data.result;
} else if (data.status === 'failed') {
throw new Error('Job failed');
}
// Wait and try again
await new Promise(r => setTimeout(r, 3000));
return pollForResult(jobId);
};
Polling vs. Webhooks
| Aspect | Polling | Webhooks |
|---|---|---|
| Latency | Up to one polling interval | Near-instant |
| Server load | Higher (many repeated requests) | Lower (one request on completion) |
| Complexity | Simple client-side loop | Requires a public endpoint |
| Reliability | Very reliable — client controls the flow | Webhook delivery can fail |
Use polling when the client is a browser or mobile app without WebSocket infrastructure. Use webhooks when the consumer is a server that can receive HTTP callbacks.
Best Practices
- Use exponential intervals: Start at 1 second, increase to 2s, 4s, 8s… to reduce server load
- Set a maximum attempts limit: Don’t poll forever — give up after a reasonable number of attempts
- Return progress information: Include a percentage or step indicator so the client can show meaningful progress
- Consider long polling or SSE: For lower latency without WebSocket complexity