In this lesson · topic 2 of 7
Topic 2
Five ways to count
5 min read
The flaw to watch for
Step through one burst across a window edge. It shows the problem that most of these algorithms exist to solve.
A burst across a window edge
Step 1 / 5- limit
- 20 per window
Two windows, limit 20
A fixed window counter keeps one count per window and resets it at each edge. Here the limit is 20 requests per window, and nothing has arrived yet.
A burst at the end of window 1
Twenty requests arrive on the last tick of the window. The count goes from 0 to 20, so every one is admitted.
The edge
One tick later the window changes. The counter resets to zero. It has no memory of the burst that was one tick ago.
A second burst, admitted
Twenty more arrive on the first tick of window 2, and the fresh counter admits all of them. The client got 40 requests through in 2 ticks, twice the limit.
The sliding log, for comparison
A log of timestamps counts the last 20 ticks wherever the edge is. It still holds the first burst, so it rejects the second. The cost is one stored timestamp per admitted request.
- Two windows, limit 20. A fixed window counter keeps one count per window and resets it at each edge. Here the limit is 20 requests per window, and nothing has arrived yet. limit: 20 per window.
- A burst at the end of window 1. Twenty requests arrive on the last tick of the window. The count goes from 0 to 20, so every one is admitted. window 1 count: 20 of 20.
- The edge. One tick later the window changes. The counter resets to zero. It has no memory of the burst that was one tick ago. window 2 count: 0 of 20.
- A second burst, admitted. Twenty more arrive on the first tick of window 2, and the fresh counter admits all of them. The client got 40 requests through in 2 ticks, twice the limit. admitted in 2 ticks: 40.
- The sliding log, for comparison. A log of timestamps counts the last 20 ticks wherever the edge is. It still holds the first burst, so it rejects the second. The cost is one stored timestamp per admitted request. admitted in 20 ticks: 20.
The five algorithms
Fixed window counter. Keep a count and the start of the current window. Each admitted request adds one. When the clock passes the edge, reset the count. It is the cheapest option, two numbers per caller. Its flaw is the one you just stepped through: the window edge is a hole, and a caller who aims at it can get up to twice the limit through in a short span.
Sliding window log. Keep a timestamp for every admitted request. To decide, discard timestamps older than one window and count what is left. It is exact. In any stretch of one window, no more than the limit gets through. The price is memory. The log holds up to the limit in timestamps, for every caller.
Sliding window counter. Keep the count for the current window and for the one before it. Estimate the count over the last window as the previous count, weighted by how much of that window still overlaps, plus the current count. It assumes the previous window’s requests were spread evenly, which is not always true. In return it keeps three numbers per caller and is close to exact in practice.
Token bucket. Picture a bucket that fills with tokens at a steady rate up to a maximum size. Each request takes one token. If the bucket is empty, the request is rejected. The refill rate sets the long-run rate. The bucket size sets how big a burst can be: a full bucket lets that many requests through at once. Two numbers per caller, a token count and the time it was last topped up. Metering schemes in networking use this shape, for example the one in RFC 2697.
Leaky bucket, as a queue. Requests join a queue, and the queue drains at a fixed rate. If the queue is full, the request is rejected. A burst is not rejected but delayed, and what leaves is smooth. It needs the queue itself, so its memory grows with the bucket size, and a request that is admitted may wait.
Where the counter lives
A real limiter usually keeps its counters in a shared store, so that several servers see one count per caller. Two servers that read, add and write back can both read 19 and both admit. The usual fix is an atomic increment or a small script that runs in the store. Another option is to give each server a share of the limit and accept some slack. The lab has one limiter and no races, so it shows the algorithms and not this problem.
Choosing
Ask three questions. Is a short burst acceptable, or must the rate be smooth? How exact must the limit be at the edges? How many callers will you track? The next topics put numbers on those.