Formula: Token Bucket Rate Limit Systems Engineering Formula
- bucketCapacityTokens: Bucket Capacity Tokens (tokens)
- refillRateTokensPerSec: Refill Rate Tokens Per Sec (tokens/s)
- incomingSpikeRps: Incoming Spike Rps (req/s)
Model token-bucket API rate limiters under burst traffic spikes.
Multiply the spike rate by the burst duration. At 250 requests per second for 3.33 seconds that is a little over eight hundred requests, all accepted. That total is the 500 tokens that were in the bucket plus the refill that arrived during those seconds. After that point only 100 requests per second, the refill rate, are accepted while the spike continues.
Convert everything to seconds before entering it. Divide a per minute refill rate by 60 to get tokens per second, and do the same with your request rate. The bucket capacity is a count of tokens, not a rate, so it stays as published. Mixing a per minute refill with a per second spike is the most common way to get a burst duration that is off by a factor of sixty.
It applies to whatever the bucket is keyed on. If each API key has its own bucket, the 40% figure describes one client that is sending 250 requests per second against its own limit. If one bucket guards an entire endpoint, the ratio describes all traffic combined. The arithmetic is the same either way; only the meaning of the spike rate changes.
Only per node. If traffic is spread evenly over several nodes and each holds a separate bucket, a client effectively gets the sum of all the buckets and all the refill rates. Enter the per node values with the per node share of the spike to see one node's view, but expect the total allowance to be larger than a single bucket suggests unless the buckets share state.
Because it answers a specific question: how long must a client stay completely silent to regain its full burst. If requests keep arriving below the refill rate, the bucket fills at the difference between the two rates instead, which takes longer. For example, a client still sending 50 requests per second would refill the default bucket at a net 50 tokens per second, taking 10 seconds rather than 5.
Not if more than one client shares that backend. The refill rate is what one bucket can sustain forever, so the sum of refill rates across every active client must fit within what the backend can handle. Set per client refill from that budget, then use bucket capacity to tolerate short legitimate spikes. Sizing each refill to the full backend capacity lets a few busy clients overload it.
Partly. Once the bucket is empty, tokens appear at the refill rate, so a single retry waiting one divided by the refill rate seconds, 10 milliseconds on the default, will usually find a token. The real risk is many clients retrying together. Exponential backoff with random jitter spreads those retries out, and pacing new requests to the refill rate stops the bucket emptying in the first place.
A bucket with no capacity cannot hold even one token, so there is no burst to measure, and the calculator asks for a capacity above zero. A very small bucket is the realistic version of that idea: it behaves almost like a strict fixed rate cap. Most APIs allow at least a modest bucket so that ordinary clumping of requests arriving together does not trigger errors.
Replace capacity with the tokens actually in the bucket. The burst is then current tokens divided by the net drain. If a previous spike left the default bucket with 300 tokens, a new 250 request spike lasts 2 seconds instead of 3.33. You can model this by entering 300 as the capacity, since the calculator always assumes the bucket starts full.
They tell you how often a client can repeat a burst. On the defaults a client can burst for 3.33 seconds, then must stay quiet for 5 seconds to recover fully. If your legitimate traffic produces a spike more often than the refill time allows, the second spike starts on a partly empty bucket and throttles sooner, so size capacity against the busiest realistic pattern.
The default limiter is a bucket that holds 500 tokens and refills at 100 tokens per second. A client then starts sending 250 requests per second, and every request spends one token. The bucket gains 100 tokens a second and loses 250, so it drains at a net 150 tokens per second. That is the first supporting line, and it is the only rate that matters while the burst lasts.
Divide the 500 tokens by that net drain of 150 and you get 3.33 seconds. For those few seconds every request is accepted, even though the client is sending two and a half times the sustained limit. Once the bucket hits zero the limiter can only pass requests as fast as new tokens drip in: 100 out of every 250, which is the 40% steady-state acceptance ratio.
The last supporting line, 5 seconds to refill an empty bucket, is a separate question. It asks how long the client must go completely quiet before the full 500 token burst is available again. It is simply capacity divided by refill rate, and it assumes no requests at all arrive while the bucket refills.
The burst formula is short enough to check on a napkin, and doing so is a good habit before you trust any limiter config. Take the spike rate, subtract the refill rate, and divide the bucket capacity by what is left.
With the default refill of 100 and spike of 250, a 300 token bucket gives 300 / 150 = 2 seconds, and a 750 token bucket gives 750 / 150 = 5 seconds. Those match the lower and higher presets exactly. Burst time is directly proportional to capacity, so doubling the bucket doubles the burst, as long as nothing else changes.
The spike side does not behave so neatly, because it sits in the denominator after a subtraction. Doubling the spike from 250 to 500 requests per second does not halve the burst. The net drain nearly triples, and the burst falls from 3.33 seconds to 1.25 seconds, a cut of well over half. At a spike of ten times the refill rate the same bucket lasts only 0.56 seconds. The closer the spike sits to the refill rate, the more sensitive the answer becomes: a spike just above the refill rate can run for a very long time on a modest bucket.
The acceptance ratio is refill divided by spike. At ten times the refill rate the limiter accepts 10%, and at six times it accepts 16.67%, with a burst of exactly 1 second because the net drain then equals the 500 token capacity every second.
It is tempting to fix throttling by making the bucket bigger. The calculator shows why that only solves part of the problem. Compare two changes that both stretch the default burst to 5 seconds.
Raising capacity to 750 tokens gives a 5 second burst, but the acceptance ratio after the burst stays at 40%, and the idle refill time grows to 7.5 seconds. The client gets a longer honeymoon and then exactly the same throttling as before.
Raising the refill rate to 150 tokens per second, with the bucket left at 500, also gives a 5 second burst, because the net drain falls to 100 tokens per second. But the steady-state acceptance climbs to 60%, and the empty bucket now refills in 3.33 seconds. That is a real increase in sustained capacity, and it costs the backend more work every second the spike continues.
So the two inputs answer different questions. Capacity decides how long a short spike can be absorbed without anyone noticing. Refill rate decides what fraction of a long overload gets through. If your spikes are brief and rare, a bigger bucket is cheap and effective. If the overload is sustained, no bucket size helps for long; only the refill rate, or the client slowing down, changes the outcome.
If you enter a spike rate equal to or lower than the refill rate, the bucket never drains. Mathematically the burst duration is infinite, because you would be dividing by zero or by a negative number, so there is no finite answer to show.
The calculator says so directly: instead of a number it returns a message that the spike is at or below the refill rate, so the bucket never empties and nothing is throttled. That message is the answer. Any traffic at or under the refill rate passes indefinitely, and a partly empty bucket slowly fills back up at the difference between the two rates. To see a burst duration at all, the spike must be above the refill rate.
The opposite edge case is a refill rate of zero. That describes a fixed quota rather than a rate limit: the client gets the bucket once and then nothing. The burst duration is still correct, capacity divided by the spike, so 500 tokens at 250 requests per second lasts 2 seconds. The idle refill line disappears in that case, because a bucket that never refills never comes back.
Every figure on the page assumes the bucket is full at the moment the spike starts and that the spike arrives at a perfectly steady rate. Real traffic rarely does either.
If the client has been sending at, say, 80% of the refill rate before the spike, the bucket will be full, because traffic under the refill rate lets it top up. But if a previous burst ended only a second or two ago, the bucket may be half empty. In that case the burst you actually get is the tokens currently in the bucket divided by the net drain. For a bucket at half of 500 tokens, the default spike would be absorbed for about 1.67 seconds, not 3.33.
Traffic also arrives in clumps. Many limiters count tokens continuously but check them per request, so a cluster of requests arriving within a few milliseconds can empty a small bucket far faster than the average rate suggests. The calculator gives the average picture. Leave headroom in capacity for that clumping rather than sizing the bucket exactly to the formula.
Finally, the calculator assumes one token per request. Some APIs weight requests, charging more tokens for an expensive call than a cheap one. If every request in your spike costs the same number of tokens, divide the bucket capacity and refill rate by that cost before entering them, or multiply your request rate by it.
The page title mentions the leaky bucket as well, and the same three inputs can describe one. A leaky bucket used as a meter is mathematically the same as a token bucket: requests that would overflow the bucket are rejected. Everything on the page applies unchanged.
A leaky bucket used as a queue behaves differently. Instead of rejecting the excess, it holds requests in a queue and releases them at a fixed leak rate. Read capacity as the queue length and refill rate as the leak rate. The queue then fills at spike minus leak, so the burst duration is how long the spike can run before the queue is full and requests start being dropped. On the defaults that is still 3.33 seconds.
The difference is latency. A token bucket answers accepted requests immediately. A leaky queue delays them, and a request arriving at a full queue waits for everything ahead of it. The worst wait is queue length divided by leak rate, which is the same arithmetic as the idle refill line: 500 queued requests leaking at 100 per second means the last one waits 5 seconds. If your clients time out sooner than that, a full queue just turns rejections into timeouts.
Run the formula backwards to size a limiter. Decide the spike rate you want to tolerate and for how long, then capacity equals the net drain multiplied by the burst seconds.
Suppose you want the default 250 request spike, against a refill of 100, to pass untouched for 10 seconds. The net drain is 150 tokens per second, so you need 150 multiplied by 10, or fifteen hundred tokens. Entering that capacity confirms a 10 second burst. It also shows the cost: an idle client now needs 15 seconds to regain its full allowance, and a client that has stayed quiet can fire off fifteen hundred requests almost at once. Make sure the backend can actually survive that instant load before you grant it.
Then decide the refill rate from what your backend can sustain per client, not from the burst you want. The two numbers should come from different places: refill from capacity planning, bucket size from how bursty legitimate clients are.
If you are on the calling side of a published rate limit, enter the provider's burst allowance as capacity and their sustained rate as refill. Then enter the rate your job would naturally send at. The burst duration tells you how long the job can run flat out before it starts getting rate limit errors, and the acceptance ratio tells you how much of your traffic would be refused after that.
The practical move is to pace the client at or a little under the refill rate once the burst is spent. A job that keeps sending at 250 requests per second against a refill of 100 has 60% of its requests rejected, and if it retries them immediately, the retries add to the spike and make the rejection rate worse. Pacing to the refill rate gets the same work done with no errors at all.
For a big batch job, the bucket matters less than you might think. A 500 token bucket saves only a few seconds on a job of many thousands of requests. The sustained refill rate sets the real finishing time.