Sub topic 10
Choosing an algorithm: accuracy, memory, burst behavior, cost
This chapter assumes you have read the earlier chapters on the five algorithms: fixed window, sliding window log, sliding window counter, token bucket, and leaky bucket. You should be comfortable reading short Python. Here you compare them and pick one for a given job.
The four dimensions
Each algorithm trades off four things:
- Accuracy: how closely the number of accepted requests matches the stated limit over any period.
- Memory: how much state each key needs.
- Burst behavior: what the limiter does when many requests arrive at once.
- Cost: the work per request, and how hard it is to update safely in a shared store.
Side-by-side comparison
| Algorithm | Accuracy | State per key | Burst behavior | Per-request cost |
|---|---|---|---|---|
| Fixed window | Low. Up to 2x the limit across a boundary | One counter and a window index | Allows a full burst at each window start | Very low |
| Sliding window log | Exact | One timestamp per accepted request | Strictly capped in any window | High. Prune and count a list |
| Sliding window counter | Good estimate, assumes even spread in the previous window | Two counters | Smooth, with a small error | Low |
| Token bucket | Exact long-run rate, bursts up to capacity | Token count and last refill time | Allows bursts up to capacity, then the steady rate | Low |
| Leaky bucket | Exact output rate | A queue or a level and timestamp | Absorbs bursts into a queue and releases at a constant rate | Low, plus queue handling |
Reading the table
Memory scales with the limit for the log. A limit of 10,000 requests per hour means up to 10,000 timestamps for each key. With a million active keys, that is far too much. The other algorithms use a fixed, small amount of state regardless of the limit.
Fixed window is cheap but leaky. A client can send limit requests at the end of one window and limit more at the start of the next. The backend sees double the intended rate for a moment. This is acceptable for coarse quotas such as daily caps, where a boundary spike does not matter.
The sliding window counter fixes most of that cheaply. It stores two counters and weights the previous window. The estimate is wrong only if traffic in the previous window was heavily skewed. For most public APIs the error is small enough to ignore.
Token bucket is the usual default. It models how real clients behave: idle for a while, then a short burst. It needs two numbers per key, and its two parameters map to policy language: capacity is the burst allowance, and refill_rate is the sustained rate.
Leaky bucket protects the downstream. Use it when the thing behind the limiter cannot tolerate bursts, such as a legacy system or a third-party API with its own strict limit. The cost is added latency, because requests wait in a queue.
Common mistakes
- Choosing the log for a high-volume key because "exact" sounds safer. Memory then grows with traffic.
- Treating token bucket capacity as a minor setting. A capacity of 1000 with a refill rate of 1 per second allows 1000 requests instantly.
- Ignoring the shared store. An algorithm with a single read-modify-write step, such as token bucket, is easy to run as one atomic script. A log needs several operations, so it is harder to keep atomic and more costly.
Exercise
Your API has 2 million active keys, each limited to 1,000 requests per hour. Clients are mostly idle but send short bursts. Which algorithm fits, and why not the sliding window log?
Show answer
A token bucket fits: it stores two numbers per key and allows short bursts while holding the hourly average. The sliding window log could hold up to 1,000 timestamps for each of 2 million keys, which is up to 2 billion entries, so its memory cost is too high.