DB / HTTP/Web / 429
WARNING
HTTP/Web Too Many Requests
The HTTP 429 status code indicates the client has sent too many requests in a given amount of time (rate limiting). The server is temporarily refusing to process the request.
Common Causes
- Exceeding the API's rate limit (requests per second/minute/hour).
- A misconfigured or overly aggressive client application or script.
- Distributed Denial of Service (DDoS) attack traffic or unintentional traffic spikes.
How to Fix
1 Implement Exponential Backoff
Retry the failed request after a progressively longer wait time. This is the standard, polite way to handle rate limits.
BASH
$ // Pseudo-code for exponential backoff
let delay = 1000; // Start with 1 second
const maxDelay = 60000;
while (attempts < maxAttempts) {
try {
await makeRequest();
break; // Success
} catch (error) {
if (error.status === 429) {
await sleep(delay);
delay = Math.min(delay * 2, maxDelay); // Double the delay
attempts++;
} else {
throw error; // Re-throw non-429 errors
}
}
} 2 Check and Respect Rate Limit Headers
Inspect the HTTP response headers for rate limit information (e.g., Retry-After, X-RateLimit-*) and adjust your request timing accordingly.
BASH
$ # Example using curl to check headers
curl -I https://api.example.com/endpoint
# Look for headers like:
# Retry-After: 120
# X-RateLimit-Limit: 1000
# X-RateLimit-Remaining: 23
# X-RateLimit-Reset: 1640995200 3 Optimize and Throttle Client Requests
Reduce request frequency by implementing client-side throttling, caching responses, or batching multiple operations into a single request where the API supports it.
BASH
$ # Example using a simple token bucket algorithm in Python
import time
class RateLimiter:
def __init__(self, rate, per):
self.rate = rate
self.per = per
self.allowance = rate
self.last_check = time.time()
def can_call(self):
current = time.time()
time_passed = current - self.last_check
self.last_check = current
self.allowance += time_passed * (self.rate / self.per)
if self.allowance > self.rate:
self.allowance = self.rate
if self.allowance < 1.0:
return False
else:
self.allowance -= 1.0
return True