Distributed limits: who is counting?
The limit belongs to the key, not to a server
Section titled “The limit belongs to the key, not to a server”Everything so far had one limiter. A real API runs behind several gateways or servers, with a load balancer sending each request to one of them. If every gateway counts the requests it sees, a client’s requests are split among several counters, and none of them sees the total. A limit of 50 in 10 seconds that is enforced by each of three gateways is a limit of up to 150 for the client.
Mental model. A rate limit is a promise about a client, and a counter is a fact about a server. When there are many servers, the promise only holds if the servers agree on the count, and agreeing takes time and a place to keep the count.
Try it: three gateways, three counters
Section titled “Try it: three gateways, three counters”The traffic is steady at 12 requests a second. The limit is 5 a second, which is 50 in 10 seconds, enforced by a sliding window log on each of three Gateways. The Counters setting is Each gateway counts on its own. A balancer sends each request to one gateway at random. Press Pause and then Reset, so that the lab stands at second 40.
Each of three gateways counts on its own and allows 50 requests in 10 seconds. At 12 requests a second shared between them nothing is rejected, although 120 in 10 seconds is far above the limit of 50.
What is happening
The limit is 5 requests a second on average: 50 in 10 seconds.
Sliding window log: the time of every accepted request is kept, and at most 50 are allowed in any 10 seconds. It is exact, and it stores one entry for each accepted request.
Traffic is steady: 12 requests arrive every second.
3 gateways each count on their own and each allows 50 requests in 10 seconds, so together they can let through up to 150. The most that got through in any 10 seconds is 120.
- Arrived
- 48012 in the last second
- Accepted
- 480100% of the requests
- Rejected
- 00% of the requests
- Most in 10 s
- 120the limit is 50
- Most in one second
- 12the rate is 5 a second
Accepted Rejected (hatched) Rate limit, 5 a second
| Limiter | Accepted | Rejected | Most in one second | Most in 10 s | Kept in memory |
|---|---|---|---|---|---|
| Token bucket | 480 | 0 | 12 | 120 | 3 (the tokens left) |
| Leaky bucket (queue) | 480 | 0 | 14 | 125 | 12 (requests waiting in the queue) |
| Fixed window | 480 | 0 | 12 | 120 | 3 (the count of this window) |
| Sliding window log (shown) | 480 | 0 | 12 | 120 | 120 (a timestamp for each accepted request) |
| Sliding window counter | 477 | 3 | 12 | 120 | 6 (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.
| Gateway | Arrived | Accepted | Rejected |
|---|---|---|---|
| Gateway 1 | 148 | 148 | 0 |
| Gateway 2 | 168 | 168 | 0 |
| Gateway 3 | 164 | 164 | 0 |
The chart as a table
| Second | Arrived | Accepted | Rejected | Reached the service behind |
|---|---|---|---|---|
| 20 | 12 | 12 | 0 | 12 |
| 21 | 12 | 12 | 0 | 12 |
| 22 | 12 | 12 | 0 | 12 |
| 23 | 12 | 12 | 0 | 12 |
| 24 | 12 | 12 | 0 | 12 |
| 25 | 12 | 12 | 0 | 12 |
| 26 | 12 | 12 | 0 | 12 |
| 27 | 12 | 12 | 0 | 12 |
| 28 | 12 | 12 | 0 | 12 |
| 29 | 12 | 12 | 0 | 12 |
| 30 | 12 | 12 | 0 | 12 |
| 31 | 12 | 12 | 0 | 12 |
| 32 | 12 | 12 | 0 | 12 |
| 33 | 12 | 12 | 0 | 12 |
| 34 | 12 | 12 | 0 | 12 |
| 35 | 12 | 12 | 0 | 12 |
| 36 | 12 | 12 | 0 | 12 |
| 37 | 12 | 12 | 0 | 12 |
| 38 | 12 | 12 | 0 | 12 |
| 39 | 12 | 12 | 0 | 12 |
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.
The 480 requests that Arrived are all Accepted, none is Rejected, and Most in 10 s is 120 against a limit of 50. The status sentence explains: three gateways each count on their own and each allows 50 requests in 10 seconds, so together they can let through up to 150. The table of requests by gateway shows how the balancer split the traffic: 148, 168 and 164 requests. That is close to a third each, and not exactly. Each gateway saw about 4 requests a second, which is below its own limit of 5, so none of them ever refused one.
Nothing is broken in any gateway. Each did what it was asked. The limit was multiplied by three because no one counted the requests of the others. Add a gateway and the allowed total grows: set Gateways to 2 and the most in 10 seconds is 100, which is the ceiling of 2 × 50. At 6 gateways the ceiling is 300, and the lab shows 120, because that is all the traffic there is.
One fix is to divide the limit among the gateways, 17 or so each. That works only if the balancer shares the requests exactly. The table shows it does not: a gateway that happens to get 168 requests would refuse some while another, with 148, still has room, and the limit for the client is still not the number you meant.
One counter for everyone
Section titled “One counter for everyone”The other fix is a counter that all the gateways use. The next lab starts with Counters set to All gateways share one counter. Press Pause and then Reset in it, and in the one after it, before you read them.
The three gateways use one counter, so the limit of 50 in 10 seconds holds for all of them together, whatever share of the traffic each one gets.
What is happening
The limit is 5 requests a second on average: 50 in 10 seconds.
Sliding window log: the time of every accepted request is kept, and at most 50 are allowed in any 10 seconds. It is exact, and it stores one entry for each accepted request.
Traffic is steady: 12 requests arrive every second.
3 gateways share one counter, so the limit holds for all of them together. The most that got through in any 10 seconds is 50, and the limit is 50.
- Arrived
- 48012 in the last second
- Accepted
- 20042% of the requests
- Rejected
- 28058% of the requests
- Most in 10 s
- 50the limit is 50
- Most in one second
- 12the rate is 5 a second
Accepted Rejected (hatched) Rate limit, 5 a second
| Limiter | Accepted | Rejected | Most in one second | Most in 10 s | Kept in memory |
|---|---|---|---|---|---|
| Token bucket | 245 | 235 | 12 | 95 | 1 (the tokens left) |
| Leaky bucket (queue) | 245 | 235 | 5 | 50 | 50 (requests waiting in the queue) |
| Fixed window | 200 | 280 | 12 | 50 | 1 (the count of this window) |
| Sliding window log (shown) | 200 | 280 | 12 | 50 | 50 (a timestamp for each accepted request) |
| Sliding window counter | 185 | 295 | 12 | 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.
| Gateway | Arrived | Accepted | Rejected |
|---|---|---|---|
| Gateway 1 | 148 | 51 | 97 |
| Gateway 2 | 168 | 75 | 93 |
| Gateway 3 | 164 | 74 | 90 |
The chart as a table
| Second | Arrived | Accepted | Rejected | Reached the service behind |
|---|---|---|---|---|
| 20 | 12 | 12 | 0 | 12 |
| 21 | 12 | 12 | 0 | 12 |
| 22 | 12 | 12 | 0 | 12 |
| 23 | 12 | 12 | 0 | 12 |
| 24 | 12 | 2 | 10 | 2 |
| 25 | 12 | 0 | 12 | 0 |
| 26 | 12 | 0 | 12 | 0 |
| 27 | 12 | 0 | 12 | 0 |
| 28 | 12 | 0 | 12 | 0 |
| 29 | 12 | 0 | 12 | 0 |
| 30 | 12 | 12 | 0 | 12 |
| 31 | 12 | 12 | 0 | 12 |
| 32 | 12 | 12 | 0 | 12 |
| 33 | 12 | 12 | 0 | 12 |
| 34 | 12 | 2 | 10 | 2 |
| 35 | 12 | 0 | 12 | 0 |
| 36 | 12 | 0 | 12 | 0 |
| 37 | 12 | 0 | 12 | 0 |
| 38 | 12 | 0 | 12 | 0 |
| 39 | 12 | 0 | 12 | 0 |
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.
At second 40 the figures are 200 Accepted, 280 Rejected, and Most in 10 s is 50: the limit holds for the three gateways together, whatever share of the traffic each one gets. It is the figure that one gateway alone would give: set Gateways to 1 and the numbers are the same. This is the design Stripe describes: it implements its rate limiters with Redis, so that the bucket for a user lives in one place. Envoy offers it too, as a global rate limit: its proxies call a rate limit service, and its documentation names a reference implementation in Go that uses a Redis backend.
The shared counter has a cost that the lab does not draw: every request now needs a round trip to the store, and the store has to keep up with the traffic of all the gateways. The lab shows a second, subtler cost.
What happens if the counter is read late?
Section titled “What happens if the counter is read late?”A gateway cannot always read the shared counter at the moment it decides. A cached value, a replica that is behind, or a batch of updates sent every few seconds all make the gateway’s view of the count a little old. In the lab the Shared counter delay is the number of seconds by which a gateway reads the counter late: it decides on the counter as it was then, plus what it has let in itself since.
Each gateway reads the shared counter 3 seconds late, so requests that arrive together can all be let in, and more than 50 get through in 10 seconds.
What is happening
The limit is 5 requests a second on average: 50 in 10 seconds.
Sliding window log: the time of every accepted request is kept, and at most 50 are allowed in any 10 seconds. It is exact, and it stores one entry for each accepted request.
Traffic is steady: 12 requests arrive every second.
3 gateways share one counter, but each reads it 3 s late, so requests that arrive together can all be let in. The most that got through in any 10 seconds is 72, and the limit is 50.
- Arrived
- 48012 in the last second
- Accepted
- 28259% of the requests
- Rejected
- 19841% of the requests
- Most in 10 s
- 72the limit is 50
- Most in one second
- 12the rate is 5 a second
Accepted Rejected (hatched) Rate limit, 5 a second
| Limiter | Accepted | Rejected | Most in one second | Most in 10 s | Kept in memory |
|---|---|---|---|---|---|
| Token bucket | 262 | 218 | 12 | 117 | 1 (the tokens left) |
| Leaky bucket (queue) | 262 | 218 | 5 | 50 | 67 (requests waiting in the queue) |
| Fixed window | 284 | 196 | 12 | 72 | 1 (the count of this window) |
| Sliding window log (shown) | 282 | 198 | 12 | 72 | 72 (a timestamp for each accepted request) |
| Sliding window counter | 240 | 240 | 12 | 76 | 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.
| Gateway | Arrived | Accepted | Rejected |
|---|---|---|---|
| Gateway 1 | 148 | 90 | 58 |
| Gateway 2 | 168 | 100 | 68 |
| Gateway 3 | 164 | 92 | 72 |
The chart as a table
| Second | Arrived | Accepted | Rejected | Reached the service behind |
|---|---|---|---|---|
| 20 | 12 | 0 | 12 | 0 |
| 21 | 12 | 0 | 12 | 0 |
| 22 | 12 | 3 | 9 | 3 |
| 23 | 12 | 12 | 0 | 12 |
| 24 | 12 | 12 | 0 | 12 |
| 25 | 12 | 12 | 0 | 12 |
| 26 | 12 | 12 | 0 | 12 |
| 27 | 12 | 12 | 0 | 12 |
| 28 | 12 | 8 | 4 | 8 |
| 29 | 12 | 0 | 12 | 0 |
| 30 | 12 | 0 | 12 | 0 |
| 31 | 12 | 0 | 12 | 0 |
| 32 | 12 | 0 | 12 | 0 |
| 33 | 12 | 0 | 12 | 0 |
| 34 | 12 | 12 | 0 | 12 |
| 35 | 12 | 12 | 0 | 12 |
| 36 | 12 | 12 | 0 | 12 |
| 37 | 12 | 12 | 0 | 12 |
| 38 | 12 | 12 | 0 | 12 |
| 39 | 12 | 12 | 0 | 12 |
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.
With a delay of 3 s, Accepted is 282 and Rejected is 198, and Most in 10 s is 72, against a limit of 50. The status sentence reads: each gateway reads the shared counter 3 s late, so requests that arrive together can all be let in. Three gateways look at an old count, all see room, and all fill it. The shared counter did not fail; it was read too late to help.
The delay and the overshoot move together. With the delay set to 0 the most in 10 seconds is 50. At 1 s it is 54, at 2 s 64, at 3 s 72 and at 5 s 86. A longer delay lets more through. The overshoot needs requests that arrive together, so that several gateways decide on the same old count, and that is the moment a limit matters most.
What real systems do
Section titled “What real systems do”None of these designs is perfect, and they are combined.
- A local limit first, a global limit second. Envoy’s documentation describes using both: a local token bucket at each proxy can absorb very large bursts that might otherwise overwhelm the global rate limit service, and the global limit gives the fine-grained, shared count. The local limit is cheap and approximate, because by default it applies to each Envoy process separately; the global one is exact and costs a call.
- A counter in each point of presence. Cloudflare counts inside each of its points of presence and not in one central store. Traffic from one IP address normally reaches the same point of presence through anycast, so a local counter sees the whole of that client’s traffic. The design works because routing keeps a client’s requests together, and it is less exact when they are split.
- Send the same key to the same gateway. The same idea in another form: if the balancer always picks the gateway from the client’s key, a local counter is enough. It gives up even load across gateways, and when a gateway is removed or fails its keys move to others and their counts start from nothing.
What should a gateway do when the shared counter cannot be reached? Envoy’s rate limit filter allows the request through by default when the rate limit service errors, and it can be configured with failure_mode_deny to refuse with a 500 instead. The first choice is fail-open and the second is fail-closed, as in the lesson on the cache failing. Fail-open keeps the service up, and it means that when the store fails, the limit disappears at the moment the system may be least able to afford it. Fail-closed keeps the limit, and it means the limiter’s outage is the service’s outage.
- Counters that are local to each gateway multiply the limit by the number of gateways: three gateways with a limit of 50 let 120 through in 10 seconds in the lab, up to a ceiling of 150.
- Dividing the limit does not fix it, because the balancer does not split the traffic evenly: 148, 168 and 164 requests went to the three gateways.
- One shared counter holds the limit for all gateways together: 200 accepted, 50 at most in 10 seconds.
- A shared counter read 3 s late overshoots: 72 in 10 seconds. At 1 s, 54; at 2 s, 64; at 5 s, 86.
- Real systems combine a cheap local limit with a shared one, or keep a client’s requests together so a local counter is enough, and they choose between failing open and failing closed when the store is down.
Next: where to enforce a limit, at the gateway or in the service.
Your progress
Built by GhaziAlibi.
© ShardCraft. All rights reserved.