Rate Limiting Algorithms: Token Bucket, Leaky Bucket and Windows
Overview
A limiter must decide quickly and cheaply whether a request is allowed. Different algorithms trade off accuracy, burst handling and memory. This lesson covers the five you will meet most.
Detailed Notes
What is a rate limiting algorithm
It is the method the limiter uses to count requests and decide allow or reject. Which one you choose changes how bursts are handled, how exact the limit is, and how much memory each caller costs.
The problem it solves
"Allow 100 requests per minute" sounds simple, but what is a minute? Does the count reset on the clock, or slide continuously? Can a client send all 100 at once? And with millions of clients, we can't store much per client. Each algorithm answers these differently.
How it works
1. Token bucket. A bucket holds up to a capacity of tokens and is refilled at a steady rate. Each request takes one token; no token means rejected or delayed. Refill rate is the sustained rate (10 per second); capacity is the maximum burst (20). It needs only two numbers per key (tokens, last refill time) and allows controlled bursts. Very common in gateways.
2. Leaky bucket. Requests enter a queue and leak out at a constant rate to the service. If the queue is full, new requests are dropped. The output is smooth with no bursts, good for backends that need steady input. Bursts are delayed or dropped.
3. Fixed window counter. Split time into fixed windows (each minute) and keep one counter per key per window. Reject when it exceeds the limit; reset at the next window. Simplest and cheapest, but has the boundary problem: a client can send 100 at the end of one minute and 100 at the start of the next, so up to twice the limit passes in a short time.
4. Sliding window log. Store the timestamp of every request and count those within the last N seconds. Exact, but memory grows with request volume.
5. Sliding window counter. Keep counters for the current and previous windows and weight the previous one by how much it still overlaps. Example: limit 100/min, 15 seconds into the minute, previous window 80, current 30: estimate = 80 x (45/60) + 30 = 90, so allow. Nearly as accurate as the log with the memory of a fixed window (it assumes requests were evenly spread).
| Algorithm | Bursts | Accuracy | Memory per key |
|---|---|---|---|
| Token bucket | Allowed up to capacity | Good | Very low |
| Leaky bucket | Smoothed out | Good | Low (queue) |
| Fixed window | 2x at boundaries | Low | Very low |
| Sliding window log | Exact | Exact | High |
| Sliding window counter | Close to exact | High (approx.) | Low |
A tiny token bucket, in plain steps: on each request, add tokens for the time that has passed (never above capacity), then spend one token if at least one is available; otherwise reject.
A simple example
A token bucket is a jar of tickets that a machine refills slowly: spend several at once if the jar is full, then wait once it is empty. A leaky bucket is a funnel: however fast you pour, water drips out at one speed.
System design example
A public API uses a token bucket (10 tokens/second, capacity 20) per API key, so a page loading ten calls at once is fine but a sustained flood is not. The login endpoint, where precision matters and the limit is tiny (5 per minute), uses a sliding window log. A service that feeds a fragile legacy backend puts a leaky bucket in front so the backend sees a constant rate.
Summary
- Token bucket: steady refill plus capacity allows bursts; the default for APIs.
- Leaky bucket: constant output rate; smooths traffic.
- Fixed window is simplest but lets 2x through at boundaries.
- Sliding window log is exact but costly; sliding window counter is a cheap approximation.
Architecture Diagram
Tokens refill at a steady rate; each request spends one.
- 1Tokens are added at a steady rate up to the capacity.
- 2Each request tries to take one token.
- 3Token found: the request goes to the service.
- 4Bucket empty: the request is rejected or delayed.
Common Interview Questions
Finished this lesson?
0 of 91 lessons done · 0% of the course

