Enrichment · STEM Explorations · A token bucket against a steady stream

A full bucket
buys a short burst.

A token bucket lets a client send a burst up to the bucket size and then only as fast as tokens refill. Each request costs one token. Give the bucket size, the refill rate, the rate the client sends at and a window of seconds, starting with a full bucket. The page works out when the bucket runs dry, how many requests get through and how many are turned away.

The API rate limit simulation tool

The worked example is A 20-token bucket refilling at 5 per second, a client sending 12 per second for 10 seconds. Nothing you type leaves this browser.

Loading the calculator. If nothing appears, turn on JavaScript.

What you get

Enrichment · STEM ExplorationsOpen to everyone · no sign-in

What you finish with
When a token bucket first refuses a request, how many requests it accepts and refuses over a window, the rate it settles to and a table by send rate.
Who it is for
Students learning about networks, web services and queues, and the teachers and developers who explain rate limits and why services return a too-many-requests response.
Format
Browser calculator for a phone, tablet or computer, by touch or keyboard. Prints a summary; downloads a spreadsheet (.csv); export and import a file.
Access
Open to everyone with the link: no account, no sign-in, nothing to buy and nothing to install. Everything you type stays in this browser; nothing is sent to Caveat.
Where it lives
The STEM Explorations space in Caveat Enrichment.
Support
Questions, a correction or a request: team@caveatacademic.com.

How to use it

  1. Name the limiter.
  2. Type the bucket size and the refill rate.
  3. Type the rate the client sends at and the window.
  4. Read when the bucket runs dry and how many requests are refused.
  5. Scan the table to see how other send rates fare.

Run the limiter

How the figures are worked out

Tokens in, tokens out.

The worked example: 12 requests a second into a 20-token bucketSent12012 × 10 sRuns dry2.86 s20 ÷ 7Accepted7020 + 5 × 10Refused50120 − 70The worked example: 12 requests a second into a 20-token bucketSent12012 × 10 sRuns dry2.86 s20 ÷ 7Accepted7020 + 5 × 10Refused50120 − 70
The bucket drains at 7 tokens a second, so it is empty after 2.86 seconds.

How a token bucket works

A bucket holds up to a set number of tokens and gains tokens at a steady rate. Each request removes one. If the bucket is empty the request is refused. A quiet client builds up a full bucket and can then send a burst, while a heavy client is held to the refill rate.

Why services use it

It protects a shared service from being overwhelmed while still allowing short bursts that real applications produce. Many web services answer a refused request with the status code 429, too many requests, and often say how long to wait.

Sources

  1. Tanenbaum, A. S., & Wetherall, D. J. (2011). Computer Networks (5th ed.). Pearson, section 5.3: the leaky bucket and token bucket algorithms.
  2. Nottingham, M., & Fielding, R. (2012). RFC 6585: Additional HTTP status codes, section 4: 429 Too Many Requests. IETF. rfc-editor.org/rfc/rfc6585.
  3. Turner, J. S. (1986). New directions in communications (or which way to the information age?). IEEE Communications Magazine, 24(10), 8-15.
  4. Mozilla Developer Network. HTTP 429 Too Many Requests and the Retry-After header. developer.mozilla.org.

Figures worked from your own numbers. Nothing you type is sent to Caveat. The worked example is made up. Your entries stay in this browser. Support: team@caveatacademic.com.

Written with AI, then checked before it is published. How we checkFound a mistake? Report it and we fix it, or refund you.Cancel anytime.