Physical Address
304 North Cardinal St.
Dorchester Center, MA 02124
Physical Address
304 North Cardinal St.
Dorchester Center, MA 02124
Exponential Backoff is a retry strategy used in computing and network systems to manage transient errors by gradually increasing the wait time between retry attempts. This approach helps prevent overwhelming the server with a high volume of immediate retry requests and increases the chances of success by spacing out the retries.
In the provided example below, we demonstrate exponential backoff using a FastAPI server to simulate Server unavaibility and a Python client retrying to get a successful response from the clinet. Here’s how it is implemented:
server.py
):
from fastapi import FastAPI, HTTPException
app = FastAPI()
failed_attempts = 0
threshold_failures = 10
@app.get("/process")
async def process_request():
global failed_attempts
if failed_attempts < threshold_failures:
failed_attempts += 1
raise HTTPException(status_code=503, detail="Service Unavailable")
return {"message": "Request processed successfully"}
client.py
):tenacity
library to implement exponential backoff. The @retry
decorator is configured to handle retries with increasing delays.import httpx
import logging
from tenacity import retry, stop_after_attempt, wait_exponential, before_sleep_log, after_log
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
@retry(
wait=wait_exponential(multiplier=1, min=1, max=60),
stop=stop_after_attempt(10),
before_sleep=before_sleep_log(logger, logging.INFO),
after=after_log(logger, logging.INFO)
)
async def call_api():
async with httpx.AsyncClient() as client:
response = await client.get("http://127.0.0.1:8000/process")
response.raise_for_status()
return response.json()
async def main():
try:
result = await call_api()
logger.info(f"API call succeeded: {result}")
except httpx.HTTPStatusError as exc:
logger.error(f"API call failed: {exc.response.status_code} {exc.response.text}")
if __name__ == "__main__":
import asyncio
asyncio.run(main())
This example showcases how exponential backoff can effectively manage transient errors, ensuring that the client retries requests intelligently without overwhelming the server, and ultimately succeeding when the server becomes available.