Skip to main content

FastAPI middleware for IP-based rate limiting using Redis, with configurable request limits and time windows.

Project description

FastAPI Rate Limiter Middleware

Project Description

This project implements a Rate Limiting Middleware for a FastAPI application. The middleware restricts how many requests a client (identified by IP address) can make within a defined time window. It helps protect your API from abuse, excessive traffic, and denial-of-service (DoS) attacks by throttling requests at the middleware level before they reach the main application logic.


Key Features

  • 🔒 Per-IP Rate Limiting: Limits each unique IP address to a fixed number of requests within a given time window.
  • ⏱️ Configurable Limits: Easily set the number of allowed requests and time window (e.g., 5 requests per 60 seconds).
  • 🧠 Smart Storage with Redis: Uses Redis to efficiently store and manage request counters with TTL (time-to-live).
  • 📉 Fail-Open Strategy: If Redis is unavailable, the middleware gracefully lets requests through to avoid blocking legitimate users.
  • 🔄 Auto Reset: Counters reset automatically after the time window expires.
  • 📦 Reusable Component: Designed as a plug-and-play middleware class for any FastAPI project.

How It Works

  1. When a request comes in, the middleware checks the client’s IP address.
  2. It creates or updates a Redis key that tracks the number of requests from that IP.
  3. If the request count exceeds the allowed limit:
    • The request is rejected with HTTP 429 Too Many Requests.
    • A message is returned including the retry_after_seconds field indicating how long to wait before retrying.
  4. If under the limit, the request proceeds normally to the API endpoint.

Tech Stack

Technology Purpose
FastAPI Web framework for building APIs
Redis In-memory data store for counters
Uvicorn ASGI server to run the FastAPI app
Starlette Base for FastAPI and middleware

Example

# main.py
from fastapi import FastAPI
from fastapi_rate_limiter import RateLimiterMiddleware

app = FastAPI()

# Add middleware with Redis URL
app.add_middleware(
    RateLimiterMiddleware,
    redis_url="redis://localhost:6379",  # Adjust to your Redis config
    max_requests=5,
    window_seconds=60
)

@app.get("/")
async def root():
    return {"message": "Hello, World!"}

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

fastapi_rlr-0.1.1.tar.gz (2.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

fastapi_rlr-0.1.1-py3-none-any.whl (3.2 kB view details)

Uploaded Python 3

File details

Details for the file fastapi_rlr-0.1.1.tar.gz.

File metadata

  • Download URL: fastapi_rlr-0.1.1.tar.gz
  • Upload date:
  • Size: 2.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.7.21

File hashes

Hashes for fastapi_rlr-0.1.1.tar.gz
Algorithm Hash digest
SHA256 ffdf839262516edcca01efb6b4210c19847212a5eed34bfc2fc3ef54db1afa63
MD5 f2148417ed4727eee380fdd42c2591dd
BLAKE2b-256 eb070a58f5200c640f802d3c731da60804a505360636c1926f29dbf25ba580c1

See more details on using hashes here.

File details

Details for the file fastapi_rlr-0.1.1-py3-none-any.whl.

File metadata

File hashes

Hashes for fastapi_rlr-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 99c0fd72ba6fde4fc7b41e8cf1bc0559d639333e676d2a7dc2f52b2014c2d1ac
MD5 e18c01f51bd5b921d66f23738647c80b
BLAKE2b-256 b8263b20a78c899880ef7ec781bd5552f6dcb5522c239202f20da35c9453c9fb

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page