Distributed rate limiting algorithms for API throttling and traffic control.
Rate limiting protects services from overload by controlling request rates. This project implements token bucket, leaky bucket, sliding window, and distributed rate limiting algorithms.
Version: 0.1.0
- ✅ Token Bucket — Burst support with refill
- ✅ Leaky Bucket — Smooth rate limiting
- ✅ Sliding Window — Time-based windows
- ✅ Distributed Limiter — Redis-based coordination
from rate_limiter import TokenBucketLimiter
# 100 requests per second, burst of 10
limiter = TokenBucketLimiter(
rate=100,
capacity=10
)
# Check if request allowed
if limiter.allow_request():
process_request()
else:
return_429_error()limiter = TokenBucketLimiter(rate=10, capacity=20)
for i in range(25):
if limiter.allow_request():
print(f"Request {i}: Allowed")
else:
print(f"Request {i}: Rate limited")limiter = SlidingWindowLimiter(
window_size=60, # 60 seconds
max_requests=100
)from distributed_limiter import RedisRateLimiter
limiter = RedisRateLimiter(
redis_client=redis_client,
key="api:user:123",
max_requests=1000,
window=60
)
if limiter.allow_request():
# Process request
pass- API throttling
- DDoS protection
- Fair resource allocation
- Cost control
| Algorithm | Memory | Accuracy | Distributed |
|---|---|---|---|
| Token Bucket | O(1) | Exact | Challenging |
| Leaky Bucket | O(1) | Exact | Challenging |
| Sliding Window | O(W) | Exact | Yes (Redis) |
pip install git+https://github.com/navinBRuas/_DistributedSystems#subdirectory=rate-limitingSee the sections above and examples/ for usage patterns.
No mandatory configuration. Optional settings are documented in the package code and examples.
0.1.0 (see VERSION.md)
See CHANGELOG.md.
MIT License. See repo root LICENSE.