Quick revision
The short answer
A token bucket starts full, so an idle client can send 100 requests at once and then 10 per second, which gives up to 100 + 10 x 10 = 200 requests in the first 10 seconds; it allows bursts up to the capacity while keeping the long-run average at the refill rate, and rejects requests when the bucket is empty. A leaky bucket used as a queue lets requests out at a constant 10 per second and queues the rest up to a limit, smoothing traffic at the cost of waiting time. Choose a token bucket for API limits where bursts are fine, and a leaky bucket in front of a fragile downstream, and document both the sustained rate and the burst size.
What to remember
In this answer, we saw that a token bucket of capacity 100 refilled at 10 per second lets an idle client send 100 requests at once and up to 200 in its first 10 seconds, while keeping the long-run rate at 10 per second and rejecting excess requests. A leaky bucket with the same rate queues requests and releases them at an even 10 per second, smoothing traffic at the cost of waiting. We chose the token bucket for API limits where bursts are fine, the leaky bucket in front of fragile downstreams, and documented both the sustained rate and the burst size.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
A token bucket starts full, so an idle client can send 100 requests at once and then 10 per second, which gives up to 100 + 10 x 10 = 200 requests in the first 10 seconds; it allows bursts up to the capacity while keeping the long-run average at the refill rate, and rejects requests when the bucket is empty. A leaky bucket used as a queue lets requests out at a constant 10 per second and queues the rest up to a limit, smoothing traffic at the cost of waiting time. Choose a token bucket for API limits where bursts are fine, and a leaky bucket in front of a fragile downstream, and document both the sustained rate and the burst size.
Why this question is asked
Every public API needs a rate limiter, and the two classic algorithms, the token bucket and the leaky bucket, are often confused because their names and parameters sound alike. The difference matters in practice: one lets a client burst and then slows it down, the other forces an even pace and makes requests wait. Interviewers like this question because the arithmetic shows immediately whether we understand the model. In this answer, we will recap what a rate limiter does, work through the token bucket with the numbers from the question, see how a leaky bucket treats the same traffic, compare policing and shaping, and decide when to use each.
1A Quick Recap of Rate Limiting
A rate limiter decides, for each request, whether it may go through now, based on how much traffic the same client has sent recently. Its purpose is to keep one client from using more than its share, and to keep a service within the load it can handle. Every algorithm has to answer two questions: what is the long-run rate a client may sustain, and how much may it exceed that rate for a short time? The second number, the burst, is where the token bucket and the leaky bucket differ.
2The Token Bucket, Step by Step
Picture a bucket that holds up to 100 tokens. Tokens are added at 10 per second, and any token that would overflow the full bucket is discarded. Each request takes one token. If a token is available, the request passes; if the bucket is empty, the request is rejected, typically with 429 Too Many Requests.
A client that has been idle for a while finds the bucket full, because tokens kept arriving and the bucket filled up to its capacity. So it can send 100 requests immediately. From then on, it can send only as fast as tokens arrive: 10 per second. Over the first 10 seconds, that is the initial 100 plus the 10 x 10 = 100 tokens added during those 10 seconds, so up to 200 requests. Over a long period, the average can never exceed 10 per second, because that is the rate at which tokens are created. The capacity sets the burst, and the refill rate sets the average.
An implementation does not need a timer that adds tokens. It stores the token count and the time of the last update, and on each request adds elapsed seconds times the rate, capped at the capacity.
| Time | Tokens before | Allowed in this interval | Total allowed |
|---|---|---|---|
| t = 0 s | 100 (full) | 100 | 100 |
| 0 to 1 s | 0 + 10 refilled | 10 | 110 |
| 1 to 5 s | 10 per second | 40 | 150 |
| 5 to 10 s | 10 per second | 50 | 200 |
3The Leaky Bucket With the Same Rate
The leaky bucket is easiest to understand as a queue with a hole in the bottom. Requests pour in at any speed and wait in the queue; a worker lets them out at a constant 10 per second. If the queue is full, for example at 100 waiting requests, new ones are dropped.
Now send the same idle client's burst of 150 requests. The first 100 are queued, the other 50 are dropped. The queued ones leave at exactly 10 per second, so the last of them leaves after 10 seconds. Over the first 10 seconds, the downstream sees 100 requests, perfectly evenly spread, never more than 10 per second. The client, however, waits: its hundredth request waits about 10 seconds before it is even processed.
That is the core difference. The token bucket polices: it lets traffic through at once as long as there is allowance, and rejects the rest. The leaky bucket shapes: it delays traffic to produce an even output rate. (There is also a 'leaky bucket as a meter' variant that rejects instead of queueing; it behaves like a token bucket with a small capacity.)
- Request arrives
- Queue has room?YesAdd it to the queueNoDrop it, return 429 or 503
- Worker takes the next requestexactly every 100 ms: 10 per second
- Downstream receives an even streamnever above 10 per second
4 steps in order. Play them, or step through them one at a time.
4Which One to Choose
For public API limits, the token bucket is usually right. Real clients are bursty: a page load fires twenty calls at once, then nothing for a minute. Rejecting those bursts would make the API feel broken, while the refill rate still protects the average. And clients get an immediate answer, success or 429, instead of hidden queueing delay.
The leaky bucket fits when the thing behind it cannot cope with bursts, for example a legacy system that handles 10 calls per second and falls over at 30, or an SMS provider with a strict per-second limit. Queueing in front of it turns the client's burst into the steady rate the downstream needs. The price is latency, so the queue should be short and the callers should be able to wait, typically background jobs rather than interactive requests.
Whichever we pick, the API documentation should state both numbers: the sustained rate (10 per second) and the burst size (100). A client that only knows the average cannot plan its batching, and one that only knows the burst will be surprised by 429s after its first big batch.
5Follow-up questions
What should the 429 response include?
A Retry-After header with the number of seconds until enough tokens are available, and rate limit headers such as the remaining allowance and the reset time, so clients can pace themselves instead of retrying blindly. With a token bucket, the wait for one token is 1 divided by the refill rate, here 100 milliseconds.
How do you store buckets for millions of clients?
Each bucket is two numbers, the token count and the last update time, so it is cheap. In a single process they live in a map with eviction of idle clients; across many nodes they usually live in Redis, updated with an atomic Lua script, or in a library such as Bucket4j with a distributed backend.
6Conclusion
In this answer, we saw that a token bucket of capacity 100 refilled at 10 per second lets an idle client send 100 requests at once and up to 200 in its first 10 seconds, while keeping the long-run rate at 10 per second and rejecting excess requests. A leaky bucket with the same rate queues requests and releases them at an even 10 per second, smoothing traffic at the cost of waiting. We chose the token bucket for API limits where bursts are fine, the leaky bucket in front of fragile downstreams, and documented both the sustained rate and the burst size.
Open with
A token bucket holds up to a hundred tokens and gets ten new ones per second. Each request takes a token, and if there are none, the request is rejected with 429.
Then, if they want more
A client that has been idle finds the bucket full, so it can send a hundred requests at once, and then ten per second as tokens arrive. In the first ten seconds that is a hundred plus ten times ten, so up to two hundred requests.
Over the long run it can never average more than ten per second, so the capacity controls the burst and the refill rate controls the average. A leaky bucket with the same rate behaves like a queue drained at a constant ten per second.
If the same client sends a burst, the requests wait in the queue and come out evenly, and anything beyond the queue size is dropped. So the downstream never sees more than ten per second, but the client pays in waiting time. In other words, a token bucket polices and a leaky bucket shapes.
For a public API I would use a token bucket, because real clients are bursty and they want an immediate yes or no. I would put a leaky bucket or queue in front of something fragile that needs an even rate, like a legacy system or an SMS provider. And I would document both numbers, the sustained rate and the burst size.
About this answer
- Last updated
- 13 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the official documentation for the version you use. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "With a token bucket of capacity 100 refilled at 10 tokens per second, how many requests...": https://scrivio-interview-library.netlify.app/architecture/api-design-at-scale/3ff560418e/