The leaky bucket: holding the burst back
Two things called a leaky bucket
Section titled “Two things called a leaky bucket”The name leaky bucket covers two designs, and a lot of confusion comes from mixing them up. Wikipedia’s article describes both.
- As a meter. A counter, separate from the traffic, is raised by each request and drains at a fixed rate. It plays the part of the water level in a bucket with a hole. A request that would make the bucket overflow is non-conforming. The article points out that this is the mirror image of the token bucket: adding water exactly mirrors removing a token, and with the same parameters the two accept and refuse the same traffic. A leaky bucket used as a meter is a token bucket seen from the other side, so it adds nothing new to the last lesson.
- As a queue. Requests that arrive are put in a first-in, first-out queue, and the queue is served at a fixed rate. The article says this enforces a rigid output pattern at the average rate, however bursty the input is. A request that finds the queue full is dropped.
The lab models the second one, and its name says so: Leaky bucket (queue). The rest of this lesson is about that variant, which is the one that changes what the service behind sees. It shapes the traffic, where the token bucket only polices it.
nginx works this way. The documentation for ngx_http_limit_req_module says that the module uses the leaky bucket method to limit the rate of requests for a key. A rate such as 1r/s is the drain rate, and the burst parameter is the size of the queue. Requests that exceed the rate are delayed, up to the burst, and a request beyond the burst is refused. With nodelay the requests within the burst are not held back.
Mental model. The token bucket is a doorman who lets you in while he has tickets. The leaky bucket is a corridor that is only wide enough for one person at a time: everyone gets through, in single file, and if the corridor is full the rest are turned away.
Try it: the same burst, held back
Section titled “Try it: the same burst, held back”The lab sends the same traffic as before: 30 requests in each of two seconds, every 20 seconds, 3 a second on average, against a Rate limit of 5 a second. The Bucket size is 60, which for this limiter is the number of places in the queue. Press Pause and then Reset, so that the lab stands at second 40.
The same burst goes into a queue of 60 places that drains at 5 a second. Nothing is rejected, and the service behind never sees more than 5 a second.
What is happening
The limit is 5 requests a second on average: 50 in 10 seconds.
Leaky bucket, as a queue: accepted requests wait in a queue of 60 places and leave it at 5 a second. A request that finds the queue full is rejected, and the service behind sees 5 a second at most.
Traffic is bursty: nothing arrives, except 30 requests in each of two seconds, every 20 seconds. The average is 3 a second.
Leaky bucket: the service behind never sees more than 5 requests in a second (5 at most so far), because the queue passes requests on at a constant rate. The queue is what absorbs a burst.
- Arrived
- 1200 in the last second
- Accepted
- 120100% of the requests
- Rejected
- 00% of the requests
- Most in 10 s
- 50the limit is 50
- Most in one second
- 5the rate is 5 a second
Accepted Rejected (hatched) Rate limit, 5 a second Reaches the service behind
| Limiter | Accepted | Rejected | Most in one second | Most in 10 s | Kept in memory |
|---|---|---|---|---|---|
| Token bucket | 120 | 0 | 30 | 60 | 1 (the tokens left) |
| Leaky bucket (queue) (shown) | 120 | 0 | 5 | 50 | 10 (requests waiting in the queue) |
| Fixed window | 120 | 0 | 30 | 60 | 1 (the count of this window) |
| Sliding window log | 100 | 20 | 30 | 50 | 20 (a timestamp for each accepted request) |
| Sliding window counter | 100 | 20 | 30 | 50 | 2 (this window's count and the last one's) |
All five see exactly the same requests. The most in 10 s is the most that reached the service behind in any 10 consecutive seconds; the limit is 50.
The chart as a table
| Second | Arrived | Accepted | Rejected | Reached the service behind |
|---|---|---|---|---|
| 20 | 0 | 0 | 0 | 5 |
| 21 | 0 | 0 | 0 | 5 |
| 22 | 0 | 0 | 0 | 0 |
| 23 | 0 | 0 | 0 | 0 |
| 24 | 0 | 0 | 0 | 0 |
| 25 | 0 | 0 | 0 | 0 |
| 26 | 0 | 0 | 0 | 0 |
| 27 | 0 | 0 | 0 | 0 |
| 28 | 0 | 0 | 0 | 0 |
| 29 | 30 | 30 | 0 | 0 |
| 30 | 30 | 30 | 0 | 5 |
| 31 | 0 | 0 | 0 | 5 |
| 32 | 0 | 0 | 0 | 5 |
| 33 | 0 | 0 | 0 | 5 |
| 34 | 0 | 0 | 0 | 5 |
| 35 | 0 | 0 | 0 | 5 |
| 36 | 0 | 0 | 0 | 5 |
| 37 | 0 | 0 | 0 | 5 |
| 38 | 0 | 0 | 0 | 5 |
| 39 | 0 | 0 | 0 | 5 |
Illustrative values, not benchmarks of any real product.
Model notes
Assumptions
- Time moves in whole seconds, read from a clock that only moves when the lab says so. In each second a limiter first sees the new time (a refill, a drain, a new window) and then decides on every request that arrives in that second, all at the same instant.
- The three patterns are built from the average load. Steady sends the load every second. Bursty sends nothing, except 10 times the load in each of two seconds (seconds 9 and 10) of every 20. Spiky sends half the load, or 5 times the load in about one second in eight, chosen by a seeded random draw. All five limiters see the same requests.
Simplifications
- The leaky bucket is the queue variant: accepted requests wait in a queue of the bucket's size and leave it at the rate, one second after they arrive at the earliest. The meter variant, which rejects a request when a level that leaks away would overflow, behaves like a token bucket and is not modelled apart.
- Windows start at multiples of their length (0, 10, 20 s and so on), not at a client's first request. A window limiter allows the rate times the window length, and counts only the requests it accepted.
- The sliding window counter keeps this window's count and the last one's, and estimates the span as the last count times the share of that window still inside the span, plus this count, rounded down in whole requests. It assumes the last window's requests were spread evenly, so it can allow a little more or less than the exact log.
- With several gateways a balancer sends each request to one gateway at random (seeded). With local counters each gateway enforces the whole limit alone. With a shared counter there is one counter; with a delay, a gateway decides on the counter as it was that many seconds ago plus what it let in itself since, and the real counter then goes past its limit (a token bucket owes tokens). Within a second the gateways take turns going first.
- Not modelled: the cost and latency of reaching a shared store (the delay stands for them), the failure of a gateway or of the store (fail open or fail closed), one limit for each client, a cost that differs by request, clients that retry after a rejection, and the 429 response with Retry-After. The numbers are floors and illustrations, not measurements of a product.
Parameter values are illustrative. They are not benchmarks of any real product.
All 120 requests are Accepted, none is Rejected, exactly as for the token bucket of 60. The difference is in the figures about the service behind. Most in one second is 5: the queue lets out 5 requests a second and no more, where the token bucket let out 30 in its worst second. Most in 10 s is 50, which is the limit for 10 seconds, where the token bucket’s was 60. The comparison table shows both limiters on the same requests, one under the other. The column Kept in memory shows what each one stores: 1 for the token bucket (the tokens left) and 10 for the leaky bucket, which is the number of requests still waiting in the queue at second 40, with the status line “(requests waiting in the queue)”.
The burst did not disappear. It was spread out. Two bursts of 30 are 60 requests, and at 5 a second the queue takes 60 / 5, which is 12 s to let them all out. The lab does not draw the waiting time, but the arithmetic is the one from Little’s Law: the request at the back of a full queue of 60 waits 60 / 5, which is 12 s before it reaches the service. A queue of 20 places gives 20 / 5, which is 4 s.
What happens if the queue is smaller?
Section titled “What happens if the queue is smaller?”Lower the Bucket size to 20. The run starts again, and the queue only has 20 places: each burst of 30 puts 20 in the queue and refuses the rest. The figures are 50 Accepted and 70 Rejected, and the service still sees 5 a second at most, with 25 in the worst 10 seconds. These are the same numbers of accepted and rejected requests that the token bucket gave at a size of 20 in the last lesson. The two limiters agree on who is let in. They differ in when the request reaches the service: at once for the token bucket, and later, at a steady pace, for the queue.
What the queue costs, and what it buys
Section titled “What the queue costs, and what it buys”The queue buys a constant rate for whatever sits behind it. That matters when the service behind cannot take a spike at all, for example a downstream call that has its own strict limit, a database that slows down when too many queries arrive together, or a device that handles one request at a time. A token bucket in front of such a service passes the burst through; a leaky bucket does not.
It costs two things.
- Latency. A request that waits in the queue is a request whose client is waiting too. A queue of 60 at 5 a second means up to 12 s of extra delay, and a client with a timeout shorter than that gives up while its request is still in line, which wastes the place. The queue should be short enough that the wait is still useful. This is the same trade-off as the bounded queue in the lesson on backpressure: a bound turns a growing delay into a visible refusal.
- Memory and state. The token bucket stores one number. The queue stores every waiting request, or at least a reference to it.
Which one to choose depends on what the requests are. For an API that answers a person, a fast refusal with a retry delay is usually better than a long wait, which favours the token bucket. For background work that only needs to keep to a rate, holding it back costs nothing, which favours the queue. Stripe’s article describes request rate limiters that refuse at once, and a separate kind of limiter for concurrent requests.
- A leaky bucket is either a meter, which is a token bucket seen from the other side, or a queue, which holds requests and releases them at a constant rate. The lab models the queue.
- With the same burst, the queue and the token bucket accept the same number of requests. The queue lets out 5 a second at most, where the token bucket’s worst second is 30.
- The queue turns a burst into delay. A full queue of 60 at 5 a second makes the last request wait 12 s.
- A queue of 20 accepts 50 of the 120 requests and rejects 70, as a token bucket of 20 does.
- nginx’s
limit_reqis a leaky bucket: the rate is the drain, the burst is the queue, and requests beyond it are refused.
Next: fixed windows, the simplest counter, and the burst that it lets through at the boundary.
Your progress
Built by GhaziAlibi.
© ShardCraft. All rights reserved.