Control the rate of incoming requests using different algorithms. Compare Token Bucket, Leaky Bucket, Fixed Window, Sliding Window Log, and Sliding Window Counter approaches.
Tokens are added to a bucket at a fixed rate. Each request consumes one token. Requests are rejected when the bucket is empty. Allows bursts up to bucket capacity.
| Algorithm | Memory | Burst | Accuracy | Used By |
|---|---|---|---|---|
Token Bucket | O(1) | Yes | Good | AWS, Stripe |
Leaky Bucket | O(1) | No | Good | NGINX |
Fixed Window | O(1) | Edge | Low | Redis |
Sliding Log | O(n) | No | Exact | Finance |
Sliding Counter | O(1) | No | High | Cloudflare |
Rate limiting is essential in API gateways (Kong, AWS API Gateway), CDNs (Cloudflare, Akamai), social media platforms (Twitter/X rate limits), payment processors (Stripe), and any public-facing service. It prevents abuse, ensures fair usage, protects against DDoS attacks, and helps maintain service reliability under load. Most implementations use a combination of algorithms at different layers: token bucket for per-user limits and sliding window for global rate limiting.