FastAPI rate limiter backed by Redis
Project description
FastAPI Sluice
Redis-backed, purely async rate limiting for FastAPI.
FastAPI Sluice is designed for modern async FastAPI applications, providing production-ready Redis-backed rate limiting with interchangeable algorithms and atomic Lua execution.
Features
- ⚡️ 100% async - built for FastAPI's async request lifecycle.
- 🌍 Global or per-route limits — protect your entire API or individual endpoints with the same API.
- 🔒 Atomic Redis operations — Every rate-limiting decision executes inside Redis using Lua, guaranteeing atomic updates under concurrent load without race conditions.
- 🔄 Three Interchangeable algorithms - Choose the algorithm that best matches
your traffic profile and resource constraints. Sluice ships out of the box with the
three widely used rate-limiting algorithms: Fixed window, sliding window log,
and token bucket, all swappable behind a single
RateLimiterAPI. - 🧩 Flexible identity — Limit by IP, API key, or any other custom attribute. You can also rate limit per-route or globally across your entire API.
Requirements
| Dependency | Supported Versions |
|---|---|
| Python | 3.10+ |
| FastAPI | 0.100+ |
| Redis server | 7.4+ |
| redis-py | 4.5+ |
Installation
Using uv (recommended):
uv add fastapi-sluice
Using pip:
pip install fastapi-sluice
Using Poetry:
poetry add fastapi-sluice
Usage
Start by connecting to a Redis server and creating a RateLimiter instance:
from redis.asyncio import Redis
from fastapi_sluice import RateLimiter, FixedWindow, SlidingWindow, TokenBucket
redis = Redis(host="localhost", port=6379)
limiter = RateLimiter(redis=redis)
Per-route limiting
Apply a limit to a specific route by passing limiter.limit() as a dependency.
Each route gets its own counter, keyed by IP address by default.
You can pass a scope string to group requests to use the same counter.
By default, the request path is used. Use scope when you want multiple
routes or parameterized URLs to share the same counter.
from fastapi import FastAPI, Depends
from fastapi_sluice import FixedWindow
app = FastAPI()
@app.get("/items")
async def get_items(_=Depends(limiter.limit(algorithm=FixedWindow(limit=5, window_seconds=60), scope="items"))):
return {"items": []}
Global limiting
Use RateLimitMiddleware to enforce an API-wide cap that applies to every route.
A single counter per identity is shared across all endpoints:
from fastapi_sluice import RateLimitMiddleware, SlidingWindow
app.add_middleware(
RateLimitMiddleware,
limiter=limiter,
algorithm=SlidingWindow(limit=500, window_seconds=60),
)
Choosing an algorithm
Each algorithm suits a different traffic profile:
from fastapi_sluice import FixedWindow, SlidingWindow, TokenBucket
# Fixed window — simplest, cheapest. Best for low-stakes limits.
FixedWindow(limit=100, window_seconds=60)
# Sliding window — accurate per-second fairness, no boundary bursts.
SlidingWindow(limit=100, window_seconds=60)
# Token bucket — sustain a rate while allowing controlled bursts.
TokenBucket(capacity=50, refill_rate=10) # 10 req/s, burst up to 50
| Algorithm | Performance / Memory | Precision & Fairness | Best Used For |
|---|---|---|---|
| Fixed Window | O(1) time and space |
Low — susceptible to boundary bursting | Standard API protection where perfection isn't critical |
| Sliding Window | O(N) space per user, higher memory cost |
Highest — accurate across shifting time windows | Critical or expensive endpoints (e.g. AI generation, payment processing) |
| Token Bucket | O(1) time and space |
High — allows controlled traffic bursts | General-purpose API protection and microservices |
Rate limit responses
When a client exceeds the limit, Sluice returns a 429 Too Many Requests response with the following headers:
| Header | Description |
|---|---|
Retry-After |
Seconds to wait before retrying |
X-RateLimit-Limit |
Maximum requests allowed in the window |
X-RateLimit-Remaining |
Requests remaining in the current window |
Example response:
HTTP/1.1 429 Too Many Requests
Retry-After: 30
X-RateLimit-Limit: 100
X-RateLimit-Remaining: 0
{"detail": "Too many requests"}
Custom Client Identifier
You can override the default IP-based key with any attribute like API key or user ID. The callable must return a unique string per identity since this value becomes the rate limit bucket key in Redis. Just create a sync or async callable:
from fastapi import Request
def get_api_key(request: Request) -> str:
api_key = request.headers.get("X-API-Key")
if api_key is None:
raise ValueError("X-API-Key header is missing")
return api_key
@app.get("/items")
async def get_items(_=Depends(limiter.limit(algorithm=FixedWindow(limit=30, window_seconds=60), key_func=get_api_key))):
return {"items": []}
Project details
Release history Release notifications | RSS feed
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 fastapi_sluice-0.1.0.tar.gz.
File metadata
- Download URL: fastapi_sluice-0.1.0.tar.gz
- Upload date:
- Size: 6.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
dedf9bc7821f5560c9ea16a8841c37bfc985ab13dff15fded609fdaf2f2950e8
|
|
| MD5 |
9b47ea9051fcd076c8d299ed8b09f11c
|
|
| BLAKE2b-256 |
c0d7e7d96b33167d096bf54fd6229595011914b8d9e034e276f26b18a5285799
|
Provenance
The following attestation bundles were made for fastapi_sluice-0.1.0.tar.gz:
Publisher:
publish.yml on dennis-nw/fastapi-sluice
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
fastapi_sluice-0.1.0.tar.gz -
Subject digest:
dedf9bc7821f5560c9ea16a8841c37bfc985ab13dff15fded609fdaf2f2950e8 - Sigstore transparency entry: 2011296963
- Sigstore integration time:
-
Permalink:
dennis-nw/fastapi-sluice@64fa682eb746d8b2016d07d04bd94623fb6f84fc -
Branch / Tag:
refs/tags/v0.1.0 - Owner: https://github.com/dennis-nw
-
Access:
private
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@64fa682eb746d8b2016d07d04bd94623fb6f84fc -
Trigger Event:
push
-
Statement type:
File details
Details for the file fastapi_sluice-0.1.0-py3-none-any.whl.
File metadata
- Download URL: fastapi_sluice-0.1.0-py3-none-any.whl
- Upload date:
- Size: 11.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
1905f423758b8551a71634de6832e36e6ff44f759d2cec89c553e452b1069ae9
|
|
| MD5 |
ec7d4c13402aa28d4d974e291d67c812
|
|
| BLAKE2b-256 |
b6a30c7c7d4b389041f92b934c7fe2418d96e24a4b074ac0d45ad4a8ccdf6f2a
|
Provenance
The following attestation bundles were made for fastapi_sluice-0.1.0-py3-none-any.whl:
Publisher:
publish.yml on dennis-nw/fastapi-sluice
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
fastapi_sluice-0.1.0-py3-none-any.whl -
Subject digest:
1905f423758b8551a71634de6832e36e6ff44f759d2cec89c553e452b1069ae9 - Sigstore transparency entry: 2011297099
- Sigstore integration time:
-
Permalink:
dennis-nw/fastapi-sluice@64fa682eb746d8b2016d07d04bd94623fb6f84fc -
Branch / Tag:
refs/tags/v0.1.0 - Owner: https://github.com/dennis-nw
-
Access:
private
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@64fa682eb746d8b2016d07d04bd94623fb6f84fc -
Trigger Event:
push
-
Statement type: