Decision guide
Rate-Limit Algorithms Compared
Which rate-limiting algorithm should you implement?
Side by side
| Criterion | Fixed window | Sliding window counter | Sliding log | Token bucket / GCRA | Leaky bucket (queue) |
|---|---|---|---|---|---|
| Accuracy | Boundary bursts (≤2×) | High (approx.) | Exact | High | Exact output rate |
| Burst behavior | Edge bursts | Smooth | Smooth | Bursts up to capacity | Smoothed |
| State per key | 1 counter | 2 counters | 1 entry/request | 2 values (GCRA: 1) | Queue |
| Retry-After calculation | Window end | Approximate | Oldest entry expiry | Natural | Queue position |
| Implementation | Trivial | Easy | Easy, memory-heavy | Lua script / library | Queue + workers |
| Typical use | Internal quotas | Gateways (Cloudflare-style) | Security-sensitive actions | API plans (AWS, Stripe) | NGINX limit_req, outbound calls |
When to choose each
Fixed window
Count requests per calendar window (INCR + EXPIRE).
Choose when
- Low-stakes limits
- Simplest possible implementation
Avoid when
- Login/OTP protection (boundary bursts)
Sliding window counter
Current window + weighted previous window.
Choose when
- General-purpose API limits
- Good accuracy at low cost
Avoid when
- You need exact counts
Sliding log
Store each request timestamp in a sorted set.
Choose when
- Small limits needing precision (e.g. 5 OTPs/hour)
Avoid when
- High limits (memory per request)
Token bucket / GCRA
Refill tokens at a rate; allow bursts up to capacity (GCRA: one timestamp).
Choose when
- Public APIs with bursty clients
- Cost-weighted limits
Avoid when
- Strict smoothing of outbound traffic
Leaky bucket (queue)
Queue requests and drain at a fixed rate.
Choose when
- Shaping calls to a rate-limited third party
Avoid when
- Low-latency APIs (adds queueing delay)
Verdict
Default to a token bucket (or GCRA) for API plans and a sliding window counter for simple per-key limits; use a sliding log only for small, security-sensitive limits, and a leaky bucket to shape your own outbound calls.
Learn more
- Rate Limiting, Quotas & Abuse Protection
- Web Security & WAF
- Performance & Scalability
- Rate limit FastAPI endpoints with Redis (FastAPI)
- Rate limit an Express API across instances (Node.js)
- Implement rate limiting in a PHP API (PHP)
- Rate limit a Spring Boot API with Redis (Spring Boot)