Skip to content
TheChatArchiveThe Chat Archive
Grok 4.1 Fast· xaiHugging Face Frontier Reasoning Traces

Implement a rate limiter middleware in FastAPI that uses Redis as a backend.

  1. User

    Implement a rate limiter middleware in FastAPI that uses Redis as a backend.

  2. Assistant
    import time
    from typing import Optional
    
    from redis.asyncio import Redis
    from starlette.middleware.base import BaseHTTPMiddleware
    from starlette.requests import Request
    from starlette.responses import JSONResponse
    
    class RateLimiterMiddleware(BaseHTTPMiddleware):
        def __init__(
            self,
            app,
            redis_url: str,
            rate_limit: int = 100,
            window_size: int = 60,
            prefix: str = "rate_limit",
        ):
            super().__init__(app)
            self.redis = Redis.from_url(redis_url, decode_responses=True)
            self.rate_limit = rate_limit
            self.window_size = window_size
            self.prefix = prefix
    
        async def _is_rate_limited(self, ip: str) -> bool:
            current_time = time.time()
            window_start = int(current_time // self.window_size) * self.window_size
            key = f"{self.prefix}:{ip}:{window_start}"
            time_elapsed = current_time - window_start
            expire_seconds = max(1, int(self.window_size - time_elapsed))
    
            pipe = self.redis.pipeline()
            pipe.incr(key)
            pipe.expire(key, expire_seconds)
            results = await pipe.execute()
            new_count = int(results[0])
            return new_count > self.rate_limit
    
        async def dispatch(self, request: Request, call_next):
            client_ip = request.client.host
            if await self._is_rate_limited(client_ip):
                return JSONResponse(
                    status_code=429,
                    content={"detail": "Rate limit exceeded"},
                    headers={"Retry-After": str(self.window_size)},
                )
            response = await call_next(request)
            return response
    

    Usage Example

    from fastapi import FastAPI
    
    app = FastAPI()
    
    app.add_middleware(
        RateLimiterMiddleware,
        redis_url="redis://localhost:6379/0",  # Adjust Redis URL as needed
        rate_limit=100,  # Max requests per window
        window_size=60,  # Window size in seconds
        prefix="api_rate_limit",
    )
    

    Key Features

    • Fixed window counter using Redis INCR and EXPIRE for efficiency.
    • Atomic operations via Redis pipeline to minimize race conditions.
    • Automatic expiration calculated precisely for the window end (clamped to at least 1 second).
    • Per-client IP rate limiting using request.client.host.
    • 429 Response with Retry-After header on limit exceed.
    • Async-friendly using redis.asyncio.Redis.
    • Configurable limit, window size, Redis URL, and key prefix.

    Installation

    pip install fastapi "redis[asyncio]" starlette
    

    Notes

    • Ensure Redis is running (e.g., docker run -p 6379:6379 redis).
    • For production, consider trusted proxies (e.g., parse X-Forwarded-For) and connection pooling/shutdown handling.
    • Customize IP extraction if behind a proxy/load balancer.
    • Lua scripts could make it fully atomic for high contention, but this is efficient for most use cases.

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.