Distributed Redis rate limiter for Python APIs using sliding window algorithm
Project description
RateGuard
A distributed rate limiting library for Python APIs using Redis.
Features
- Sliding Window rate limiting with millisecond precision
- Redis Sorted Sets — no member collisions, accurate counts
- FastAPI Middleware — global limits applied to every route
- Per-route decorator — override limits on specific endpoints
- Distributed — works across multiple API servers via shared Redis
- Fail open — if Redis is unreachable, requests are allowed through
How It Works
RateGuard uses the Sliding Window algorithm with Redis Sorted Sets.
- Each request is stored with a millisecond timestamp + unique ID
- Requests older than the window are removed on every check
- The remaining count determines whether the request is allowed
- Standard rate limit headers are returned on every response
Client → FastAPI → RateGuard Middleware → Redis → Decision
Installation
pip install rate-guardian
Or for local development:
pip install -r requirements.txt
Quick Start
Copy .env.example to .env and fill in your Upstash Redis credentials.
import os
from fastapi import FastAPI, Request
from rateguard import RateGuard, RateLimitMiddleware, rate_limit
app = FastAPI()
limiter = RateGuard(
redis_url=os.environ["UPSTASH_REDIS_REST_URL"],
redis_token=os.environ["UPSTASH_REDIS_REST_TOKEN"],
)
# Global: 10 requests per 60 seconds per IP
app.add_middleware(RateLimitMiddleware, limiter=limiter, limit=10, window=60)
@app.get("/")
async def home():
return {"message": "API is protected by RateGuard"}
# Per-route override: tighter limit on an expensive endpoint
@app.get("/search")
@rate_limit(limiter, limit=5, window=60)
async def search(request: Request, q: str = ""):
return {"query": q, "results": []}
Run the Example
cp .env.example .env # add your Redis credentials
uvicorn examples.fastapi_example:app --reload
Response Headers
Every response includes standard rate limit headers:
| Header | Description |
|---|---|
X-RateLimit-Limit |
Maximum requests allowed in the window |
X-RateLimit-Remaining |
Requests remaining in the current window |
X-RateLimit-Reset |
Seconds until the oldest request expires |
Retry-After |
Seconds to wait before retrying (only on 429) |
Running Tests
pip install pytest httpx
pytest tests/ -v
License
MIT
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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file rate_guardian-0.1.1.tar.gz.
File metadata
- Download URL: rate_guardian-0.1.1.tar.gz
- Upload date:
- Size: 4.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.8.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e60d06bf01d5266577f35f84b5eaf7ca79354a2b112bc8830fbde1cb0ceb0797
|
|
| MD5 |
0d661137d1aa83f5f037d148092eb6a1
|
|
| BLAKE2b-256 |
35f20a4179f28bd787441c6df312e4dfdc1e75a456ee49ab2905723856b744e8
|
File details
Details for the file rate_guardian-0.1.1-py3-none-any.whl.
File metadata
- Download URL: rate_guardian-0.1.1-py3-none-any.whl
- Upload date:
- Size: 6.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.8.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
4eb67813b85c940a0ec19878a3cef5f800710fde79acecacd44383bf98707a7d
|
|
| MD5 |
c2dd362e48070deed62c602cce8606f4
|
|
| BLAKE2b-256 |
68fc39b8a8cdc3f044f67641fb98edf8b8bd496ce54572bd041fce6f159aa943
|