Skip to main content

p-redis-limiter

Redis token bucket rate limiter for Python and FastAPI.

Installation

pip install git+https://github.com/mehranpng/p-redis-limiter.git

Usage

1. Global Middleware (All Endpoints)

Apply a rate limit across all endpoints:

from fastapi import FastAPI
from redis import Redis
from p_redis_limiter import RateLimitMiddleware, Rate

app = FastAPI()
r = Redis(host="localhost", port=6379, decode_responses=True)

# 40 requests per 60 seconds
app.add_middleware(
    RateLimitMiddleware,
    redis=r,
    rates=Rate(40, 60),
)

@app.get("/")
def index():
    return {"ok": True}

2. Specific Endpoint Only

To rate limit a specific route instead of the whole application, use RateLimiter with Depends:

from fastapi import FastAPI, Depends
from redis import Redis
from p_redis_limiter import RateLimiter, Rate

app = FastAPI()
r = Redis(host="localhost", port=6379, decode_responses=True)

# 5 requests per 60 seconds for login only
login_limiter = RateLimiter(r, Rate(5, 60), prefix="rate:login")

@app.get("/")
def home():
    return {"message": "unlimited"}

@app.post("/login", dependencies=[Depends(login_limiter.as_dependency())])
def login():
    return {"message": "login successful"}

3. Multiple Rate Limits

You can define multiple rules (e.g. 5 req/sec burst limit and 100 req/min). If one rule fails, tokens are not deducted from the others:

rates = [
    Rate(5, 1),    # 5 requests per 1 second
    Rate(100, 60), # 100 requests per 60 seconds
]

# Or string shorthand:
rates = ["5/s", "100/m"]

4. Cache TTL

By default, Redis keys expire automatically after max(window * 2, 60) seconds. You can specify a custom TTL:

# Per rate rule:
Rate(40, 60, ttl=120)

# Disable TTL (persist in Redis forever):
Rate(40, 60, ttl=-1)

# Or globally on middleware / limiter:
RateLimitMiddleware(redis=r, rates=Rate(40, 60), ttl=120)

5. Manual Usage (Specify IP Yourself)

If you prefer to get the IP yourself, simply pass your IP variable directly:

from p_redis_limiter import RateLimiter, Rate

limiter = RateLimiter(r, Rate(5, 60))

@app.post("/login")
def login(request: Request):
    user_ip = get_my_ip(request)  # your own IP variable

    # Simple boolean check:
    if not limiter.is_allowed(user_ip):
        return JSONResponse({"detail": "Too many requests"}, status_code=429)

    return {"ok": True}

Or get full details (remaining, retry_after):

result = limiter.check(user_ip)
if not result.allowed:
    print(f"Blocked! Retry after {result.retry_after}s")

Options

Parameter Type Default Description
redis Redis Required Redis client instance (redis.Redis or redis.asyncio.Redis)
rates Rate / list / str Required Rate rules, e.g. Rate(40, 60) or ["5/s", "100/m"]
ttl int Auto Custom Redis key TTL in seconds
prefix str "rate" Prefix for Redis keys
identifier Callable / str Client IP Custom function or header name (auto-detects Cloudflare, Nginx, ALB, direct IP)
exclude_paths list[str] None List of paths to exclude from rate limiting
error_detail str "Too many requests" Response detail message on 429

Release files for p-redis-limiter 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for p-redis-limiter 0.1.0
File Size Uploaded
p_redis_limiter-0.1.0.tar.gz 10.4 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for p-redis-limiter 0.1.0
File Interpreter ABI Platform
p_redis_limiter-0.1.0-py3-none-any.whl Python 3 none any Details

Total release size: 20.9 kB

Release files / p_redis_limiter-0.1.0.tar.gz

Download URL p_redis_limiter-0.1.0.tar.gz
Size 10.4 kB
Tags Source
SHA-256 checksum
How to use checksums
490fdb2813d2090ad71aa6aa4051fdf80f071cfc4682d4a75012b8c566cb3943
BLAKE2b-256 checksum
How to use checksums
51d229798ff3193d9fd0294c52990a986ab3eb1626cbdda1135ad0c30e017fd9
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.14.4

Release files / p_redis_limiter-0.1.0-py3-none-any.whl

Download URL p_redis_limiter-0.1.0-py3-none-any.whl
Size 10.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
580d1b1fe6567a6c02757c14ab4658127bf6ceb8a1666b1ed8d851258cd9b81b
BLAKE2b-256 checksum
How to use checksums
546dd5d0c6f183411efb949ed3b68e24b14565cb3a9e6fd4a6637bf4ca81a5a8
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.14.4

Release history Release notifications | RSS feed

0.1.2

2 release files

0.1.1

2 release files

This release

0.1.0 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page