Skip to content

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.

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.

Traffic
Pattern
12 per second

The same number of requests every second.

Limiter
Limiter shown
5 per second

Requests a second the limit allows on average. A window allows this times its length: 50 in 10 seconds.

50

Tokens in the token bucket, and places in the leaky bucket's queue. The three window limiters do not use it.

10 s

Seconds in a window, for the three window limiters. Windows start at 0 and then at every multiple of their length.

Gateways
Gateways
3
Counters
0 s

Seconds by which a gateway reads the shared counter late. It decides on the counter as it was then, plus what it has let in itself since.

A balancer sends each request to one gateway at random. Changing the rate, the bucket, the window or the gateways starts the run again from second 0, so that all five limiters are compared from the same start.

Time controls
Time: 40 s

One second of model time passes each half second while the lab runs. Step and Skip move time on by hand.

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
Requests each second, accepted and rejected
01020040 seconds

Accepted Rejected (hatched) Rate limit, 5 a second

The five limiters on the same requests, after 40 s
LimiterAcceptedRejectedMost in one secondMost in 10 sKept in memory
Token bucket4800121203 (the tokens left)
Leaky bucket (queue)48001412512 (requests waiting in the queue)
Fixed window4800121203 (the count of this window)
Sliding window log (shown)480012120120 (a timestamp for each accepted request)
Sliding window counter4773121206 (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.

Requests by gateway, Sliding window log, after 40 s
GatewayArrivedAcceptedRejected
Gateway 11481480
Gateway 21681680
Gateway 31641640
The chart as a table
The last 20 seconds, Sliding window log
SecondArrivedAcceptedRejectedReached the service behind
201212012
211212012
221212012
231212012
241212012
251212012
261212012
271212012
281212012
291212012
301212012
311212012
321212012
331212012
341212012
351212012
361212012
371212012
381212012
391212012

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.

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.

Traffic
Pattern
12 per second

The same number of requests every second.

Limiter
Limiter shown
5 per second

Requests a second the limit allows on average. A window allows this times its length: 50 in 10 seconds.

50

Tokens in the token bucket, and places in the leaky bucket's queue. The three window limiters do not use it.

10 s

Seconds in a window, for the three window limiters. Windows start at 0 and then at every multiple of their length.

Gateways
Gateways
3
Counters
0 s

Seconds by which a gateway reads the shared counter late. It decides on the counter as it was then, plus what it has let in itself since.

A balancer sends each request to one gateway at random. Changing the rate, the bucket, the window or the gateways starts the run again from second 0, so that all five limiters are compared from the same start.

Time controls
Time: 40 s

One second of model time passes each half second while the lab runs. Step and Skip move time on by hand.

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
Requests each second, accepted and rejected
01020040 seconds

Accepted Rejected (hatched) Rate limit, 5 a second

The five limiters on the same requests, after 40 s
LimiterAcceptedRejectedMost in one secondMost in 10 sKept in memory
Token bucket24523512951 (the tokens left)
Leaky bucket (queue)24523555050 (requests waiting in the queue)
Fixed window20028012501 (the count of this window)
Sliding window log (shown)200280125050 (a timestamp for each accepted request)
Sliding window counter18529512502 (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.

Requests by gateway, Sliding window log, after 40 s
GatewayArrivedAcceptedRejected
Gateway 11485197
Gateway 21687593
Gateway 31647490
The chart as a table
The last 20 seconds, Sliding window log
SecondArrivedAcceptedRejectedReached the service behind
201212012
211212012
221212012
231212012
24122102
25120120
26120120
27120120
28120120
29120120
301212012
311212012
321212012
331212012
34122102
35120120
36120120
37120120
38120120
39120120

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.

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.

Traffic
Pattern
12 per second

The same number of requests every second.

Limiter
Limiter shown
5 per second

Requests a second the limit allows on average. A window allows this times its length: 50 in 10 seconds.

50

Tokens in the token bucket, and places in the leaky bucket's queue. The three window limiters do not use it.

10 s

Seconds in a window, for the three window limiters. Windows start at 0 and then at every multiple of their length.

Gateways
Gateways
3
Counters
3 s

Seconds by which a gateway reads the shared counter late. It decides on the counter as it was then, plus what it has let in itself since.

A balancer sends each request to one gateway at random. Changing the rate, the bucket, the window or the gateways starts the run again from second 0, so that all five limiters are compared from the same start.

Time controls
Time: 40 s

One second of model time passes each half second while the lab runs. Step and Skip move time on by hand.

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
Requests each second, accepted and rejected
01020040 seconds

Accepted Rejected (hatched) Rate limit, 5 a second

The five limiters on the same requests, after 40 s
LimiterAcceptedRejectedMost in one secondMost in 10 sKept in memory
Token bucket262218121171 (the tokens left)
Leaky bucket (queue)26221855067 (requests waiting in the queue)
Fixed window28419612721 (the count of this window)
Sliding window log (shown)282198127272 (a timestamp for each accepted request)
Sliding window counter24024012762 (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.

Requests by gateway, Sliding window log, after 40 s
GatewayArrivedAcceptedRejected
Gateway 11489058
Gateway 216810068
Gateway 31649272
The chart as a table
The last 20 seconds, Sliding window log
SecondArrivedAcceptedRejectedReached the service behind
20120120
21120120
2212393
231212012
241212012
251212012
261212012
271212012
2812848
29120120
30120120
31120120
32120120
33120120
341212012
351212012
361212012
371212012
381212012
391212012

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.

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.