Distributed rate limiting for Python on Redis. Every process that shares a Redis server shares the limit: fixed window, sliding window or GCRA, each one atomic Lua script, for async and sync code alike.
from redis.asyncio import Redis
from ratelimit_io import Key, Limit, RateLimiter
limiter = RateLimiter(Redis(), limit=Limit(100, minutes=1))
@limiter(key=Key('user:{user_id}'))
async def search(user_id: int, query: str) -> list[str]: ...The 101st call in a minute for one user raises RateLimitExceededError, and
its retry_after, rounded up, fills an HTTP Retry-After header.
Documentation: https://bagowix.github.io/ratelimit-io/
- Three algorithms: fixed window (the default), an exact sliding window, and GCRA, a token bucket in constant memory.
- Incoming and outgoing requests.
max_wait=0refuses an over-limit request at once. With a wait budget the request reserves the next free slot in the same round trip and sleeps until it: no polling, first come first served across processes, and an early failure when the slot lies beyond the budget. - Async and sync in one class:
await limiter.acquire()withredis.asyncio.Redis,limiter.acquire_sync()withredis.Redis, and a decorator for both that keeps the function's signature. - Strict typing: mypy, pyright and pyrefly in strict mode.
- Hashed keys: a bucket name holds the SHA-256 of its key, so raw keys stay out of Redis and every name has the same length.
- Tested on real servers: Python 3.11 to 3.15 against Redis 7, plus Redis 5, Redis 6.2 and Valkey 8, including races between threads and tasks for one bucket.
pip install ratelimit-ioRequires Python 3.11+, redis-py 5.0.1+ and a Redis 5.0+ or Valkey server.
from redis import Redis
from ratelimit_io import Algorithm, Limit, RateLimiter, RateLimitExceededError
# Incoming requests: refuse at once.
api = RateLimiter(Redis(), limit=Limit(10, seconds=1))
try:
api.acquire_sync('203.0.113.7')
except RateLimitExceededError as error:
print(f'429, retry after {error.retry_after:.1f}s')
# Outgoing requests: wait up to 30 s for a slot, never exceed 5000 an hour.
github = RateLimiter(
Redis(),
limit=Limit(5000, hours=1),
key='github',
algorithm=Algorithm.SLIDING_WINDOW,
max_wait=30,
)
github.acquire_sync()Guides: algorithms, keys and buckets, waiting, FastAPI, Litestar, Django and Flask. Upgrading from 0.x? Read the migration guide.
See CONTRIBUTING.md.