Rate Limiting Algorithms
Token Bucket: Tokens accumulate in a bucket at a fixed rate; requests consume tokens; permits bursts up to bucket capacity. Leaky Bucket: Requests enter a queue and process at a strictly constant rate, smoothing traffic spikes. Sliding Window Log/Counter: Tracks timestamped requests in memory to enforce precise limits over rolling time boundaries.
Distributed Rate Limiting with Redis
In multi-instance microservices, centralized rate limits use Redis with atomic Lua scripts. Using Redis pipelines or Redis cells avoids race conditions and network round-trips when checking and decrementing client IP quotas.
Circuit Breakers (Closed, Open, Half-Open)
To prevent cascading failures when a downstream dependency is failing, a circuit breaker wraps calls: Closed (normal operations), Open (dependency has failed threshold; calls fail immediately without touching the downstream service), and Half-Open (allows trial requests through to test recovery).