HTTP 429 Too Many Requests — Rate Limiting Guide
Handle HTTP 429 Too Many Requests errors correctly. Learn about rate limiting strategies, Retry-After headers, exponential backoff, and how to design rate-limit-friendly clients.
HTTP Status Codes
Detailed Explanation
HTTP 429 Too Many Requests
A 429 response indicates that the client has exceeded the rate limit imposed by the server. This is a normal part of API usage and should be handled gracefully.
Understanding Rate Limits
APIs impose rate limits to:
- Protect server resources from abuse
- Ensure fair usage across all clients
- Prevent DDoS-like traffic from misbehaving clients
- Manage costs for metered services
Common Rate Limit Headers
X-RateLimit-Limit: 100 # Max requests per window
X-RateLimit-Remaining: 0 # Requests remaining
X-RateLimit-Reset: 1672531200 # Unix timestamp when window resets
Retry-After: 60 # Seconds to wait before retrying
Implementing Backoff
Exponential backoff with jitter:
async function fetchWithRetry(url, maxRetries = 5) {
for (let i = 0; i < maxRetries; i++) {
const response = await fetch(url);
if (response.status !== 429) return response;
const retryAfter = response.headers.get('Retry-After');
const delay = retryAfter
? parseInt(retryAfter) * 1000
: Math.min(1000 * Math.pow(2, i) + Math.random() * 1000, 30000);
await new Promise(r => setTimeout(r, delay));
}
throw new Error('Rate limit exceeded after max retries');
}
Best Practices
- Always check Retry-After header before retrying
- Use exponential backoff with random jitter
- Cache responses to reduce unnecessary requests
- Batch requests where the API supports it
- Use webhooks instead of polling when available
- Implement request queues to spread load over time
Use Case
Handling 429 errors is essential for any application that integrates with third-party APIs. Whether you are building an API client library, a data pipeline, or a monitoring system, understanding rate limiting, backoff strategies, and queue-based throttling prevents your application from being blocked and ensures reliable data flow.