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": []}
Metadata
Release files for fastapi-sluice 0.1.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | |
|---|---|---|---|---|
| fastapi_sluice-0.1.0.tar.gz | 6.9 kB | With provenance | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| fastapi_sluice-0.1.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 18.7 kB
Release files / fastapi_sluice-0.1.0.tar.gz
| Download URL | fastapi_sluice-0.1.0.tar.gz |
|---|---|
| Size | 6.9 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
dedf9bc7821f5560c9ea16a8841c37bfc985ab13dff15fded609fdaf2f2950e8
|
|
BLAKE2b-256 checksum How to use checksums |
c0d7e7d96b33167d096bf54fd6229595011914b8d9e034e276f26b18a5285799
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.13.12
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Jun 29, 2026.
Transparency logRelease files / fastapi_sluice-0.1.0-py3-none-any.whl
| Download URL | fastapi_sluice-0.1.0-py3-none-any.whl |
|---|---|
| Size | 11.8 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
1905f423758b8551a71634de6832e36e6ff44f759d2cec89c553e452b1069ae9
|
|
BLAKE2b-256 checksum How to use checksums |
b6a30c7c7d4b389041f92b934c7fe2418d96e24a4b074ac0d45ad4a8ccdf6f2a
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.13.12
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Jun 29, 2026.
Transparency logSecurity for fastapi-sluice 0.1.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
Full provenance
All 2 files in this release have been uploaded with provenance metadata.
Attestation for fastapi_sluice-0.1.0.tar.gz
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Jun 29, 2026.
Transparency logA note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.