/ THE IDEA
The system keeps a counter of available tokens, where a token is permission for a unit of work. Tokens are added at a chosen refill rate, up to a fixed capacity. Each accepted request removes one token in the simplest version. When the counter is empty, a new request is rejected or delayed according to policy. Unused permission can accumulate, but only up to the capacity. This lets a quiet client save enough permission for a brief burst without earning unlimited future traffic. The refill rate controls long-run replenishment; capacity controls how much accumulated permission can be spent immediately.
THE FORMAL IDEA
tokens_before = min(B, tokens_after_previous + rΔt)
| B is bucket capacity in tokens; r is refill rate in tokens per second; Δt is elapsed seconds. | | min selects the smaller value. An accepted one-token request subtracts one after this update. These are permissions, not security login tokens. |
|
RUN THE TINY EXAMPLE
Change one thing. Predict what happens.
Toy bucket: B = 10, r = 2 tokens/s; start full. Eight simultaneous one-token requests leave 10 − 8 = 2 tokens. Wait three seconds with no requests: min(10, 2 + 2 × 3) = 8 tokens. Change only the wait to one second: min(10, 2 + 2 × 1) = 4 tokens, so only four immediate one-token requests can now pass.
|
The eight-request opening burst exceeds the refill rate over that instant, yet it is allowed because permission had accumulated. Continuing at eight requests every second would drain the bucket faster than two tokens per second can replenish it.
/ SO WHAT?
This separates two design questions: how much sudden legitimate work can the service absorb, and how much continuing work should one caller receive? Raising capacity helps the first without changing the refill rate. Raising the refill rate changes sustained allowance. Requests with different costs can consume different token amounts, preventing an expensive operation from being treated as equal to a trivial check.
ONE CAVEAT |
| Distributed enforcement, clock behaviour and refill granularity affect exact limits. Some products implement best-effort targets rather than strict ceilings; rate limiting also does not decide whether a caller is authorised to perform an operation. |
KEEP THIS
Bucket capacity permits accumulated bursts, while refill rate limits how quickly permission returns after those bursts are spent.
|
NEXT: How a microphone turns moving air into a voltage
|