NOTES & IDEAS
Token Bucket Rate Limiting: Separate the Average Rate from the Burst
A 100-request-per-minute refill rate does not define the burst size or guarantee a strict rolling-minute ceiling. Work through both parameters explicitly.
An API permits an average rate of 100 requests per minute but should allow controlled bursts. My answer to the multiple-choice question was “token bucket.”
The algorithm name is useful. The next question is what the bucket actually allows.
There are two settings to explain: how quickly tokens return and how many tokens can accumulate.
Give the bucket a capacity
For an illustrative API policy, suppose:
- Tokens refill at 100 per minute.
- The bucket holds at most 20 tokens.
- Each accepted request spends one token.
- A new bucket starts full.
The refill rate is approximately 1.67 tokens per second. Capacity is 20 requests of accumulated credit.
A client returning after a long idle period can spend those 20 tokens quickly. It does not keep earning unlimited credit while absent, because the bucket stops filling at capacity.
This separation between refill rate and bucket size appears in the AWS API Gateway documentation. The numbers here are chosen for explanation, not copied from a service configuration.
Refill only what has elapsed
A request-based implementation can update the balance when a request arrives rather than scheduling a background refill job.
The conceptual calculation is:
elapsed = max(0, now - last_update)
available = min(capacity, previous_tokens + elapsed * rate)
if available >= cost:
accept and subtract cost
else:
reject
The values of time and rate must use matching units.
For example, an empty bucket in this model gains five tokens after three seconds. If no other request spends them, five requests can then be accepted. That calculation follows from the chosen rate; it is not a benchmark.
RFC 2697 describes a networking meter built with token buckets. It is useful background for rate and burst accounting, but its packet-coloring rules are not the same as this simplified API admission example.
Average does not mean every rolling minute
In the idealized model, a full bucket gives the client initial credit in addition to tokens earned during an interval.
Ignoring endpoint timing and rounding details, a useful upper-bound model over an interval of length T is:
accepted request cost <= bucket capacity + refill rate * T
For our chosen settings, an interval can include the initial 20 tokens plus credit earned at 100 per minute. That is why “100 per minute” should not be presented as “at most 100 in every possible rolling minute.”
If that stricter rule is the actual requirement, the policy needs a different design or additional constraints.
Decide how enforcement works
Before implementing the example, I would document the bucket key: a user, API key, tenant, or another explicitly chosen identity.
I would also decide how multiple application instances share the decision. A separate full bucket on each instance is a different policy from one global bucket. Concurrent read-and-update operations need coordination so they do not spend the same credit.
These are implementation design questions, not proof that a particular storage backend enforces them correctly.
AWS explicitly describes its own throttles as best-effort targets rather than guaranteed ceilings. That limitation belongs to the service, and should not be confused with the idealized arithmetic above. See API Gateway throttling.
Explain the policy with the answer
The answer I would give now is: token bucket supports controlled bursts by accumulating bounded credit while refilling at a configured rate. To know what traffic it permits, ask for both the refill rate and the capacity.
That is the difference between naming the algorithm and explaining its behavior.