PYTHON
Robust API Rate Limit Handling with Exponential Backoff (Python)
Implement a resilient retry mechanism in Python for API requests that encounter rate limiting (HTTP 429), using exponential backoff with jitter.
import requests
import time
import random
def make_api_request_with_retry(url, headers=None, max_retries=5, initial_delay=1, backoff_factor=2):
for retry_num in range(max_retries + 1):
try:
response = requests.get(url, headers=headers, timeout=10) # Added timeout
response.raise_for_status() # Raise an HTTPError for bad responses (4xx or 5xx)
return response.json() # Assuming JSON response
except requests.exceptions.HTTPError as e:
if e.response.status_code == 429: # Too Many Requests
delay = initial_delay * (backoff_factor ** retry_num)
jitter = random.uniform(0.5, 1.5) # Add jitter to prevent thundering herd
sleep_time = delay * jitter
print(f"Rate limited. Retrying in {sleep_time:.2f} seconds... (Retry {retry_num+1}/{max_retries})")
time.sleep(sleep_time)
else:
print(f"HTTP Error: {e}")
raise # Re-raise other HTTP errors
except requests.exceptions.RequestException as e:
print(f"Request Error: {e}")
raise # Re-raise network or other request errors
raise Exception(f"Failed to fetch data from {url} after {max_retries} retries.")
# Example Usage:
# API_ENDPOINT = "https://api.example.com/data"
# AUTH_HEADERS = {"Authorization": "Bearer YOUR_API_TOKEN"}
# try:
# data = make_api_request_with_retry(API_ENDPOINT, headers=AUTH_HEADERS)
# print("Successfully fetched data:", data)
# except Exception as e:
# print("An error occurred during API request:", e)
How it works: This Python function demonstrates a robust way to handle API rate limits. It attempts a request and, if it receives an HTTP 429 (Too Many Requests) status code, it waits for an exponentially increasing delay before retrying. Jitter is added to the delay to prevent all clients from retrying simultaneously. It includes a maximum number of retries and error handling for other HTTP and request exceptions.