Definition
A token bucket is a rate limiter §9.38: a mechanism that caps how much work a source may have admitted per unit of time. It holds a balance of credit, called tokens, that grows at a fixed rate up to a capacity . A request is admitted only if the balance covers its cost, which is then subtracted. The capacity sets the largest burst, the work admitted at once. The arrival envelope bounds the work admitted in any interval of length .
Where it fits
Main entry: Rate limiting (§9.38).
The Rate limiting entry derives the envelope and covers the bucket in full. The two parameters limit different things. The rate limits the long-run average: in any interval of length , the admitted work divided by is at most , which approaches as grows. The capacity limits stored credit, and stored credit can be spent all at once. Nothing in the bucket limits how fast admitted work runs.
Example. A bucket limits a worker to 25% of the processor. Cost is counted in execution-milliseconds, milliseconds of CPU time §18.1.5 (time the processor spends running the worker), and time in wall-milliseconds, milliseconds of wall time (time on an ordinary clock). The rate is execution-milliseconds per wall-millisecond and the capacity is execution-milliseconds. The bucket starts full, and each operation’s cost is known in advance and charged when the operation is admitted. An operation costing 4 execution-milliseconds arrives. The balance of 4 covers it, so the operation is admitted, the balance drops to 0, and the operation runs for 4 wall-milliseconds at full speed, using 100% of the processor. During those 4 wall-milliseconds the bucket earns execution-millisecond for later work. In any 100 wall-milliseconds the bucket admits at most execution-milliseconds, 29% of the interval; in any 1000 wall-milliseconds at most , which is 25.4%.
How long full speed can last follows from the envelope. Assume that operations start as soon as they are admitted, run one at a time, and use exactly their charged cost. A stretch of continuous running that begins with the worker idle runs only work admitted during the stretch, so a stretch of wall-milliseconds needs , which gives wall-milliseconds. Operations of 4, 1, 0.25, execution-milliseconds, each paid with the credit earned while the one before ran, approach this limit: .
To limit how fast a burst runs, a design adds a second limit, for example a second bucket with a small capacity that every request must also pass. RFC 2215 describes network traffic in these terms: a token rate and a bucket depth , plus a peak rate . In network calculus, the theory of delay bounds in networks, a function that bounds the traffic admitted in every interval of length is called an arrival curve; is the token bucket’s arrival curve, here called the arrival envelope.
Limits and common mistakes
The envelope holds only when every input is stated: the unit of cost, the clock (a monotonic clock §9.35, which never runs backwards), the capacity, the starting balance (a full bucket admits at once), every caller that uses the resource (one that bypasses the bucket is not limited), and the moment at which cost is charged.
Charging at admission needs the cost in advance. Charging after completion, with the measured cost, lets the balance go negative and raises the bound by up to one operation’s cost; the Post-charge debt and a one-operation overshoot bound §19.4.2 entry derives that bound.
The envelope limits admitted work, not response time §9.32: it says nothing about how long one request waits. The Duty-cycle bound versus per-request response-time bound §19.4.6 entry treats that difference.
Questions answered
Why can a 25% rate still allow a short full-speed burst?
A 25% rate still allows a short full-speed burst because a token bucket limits the average amount of admitted work over long intervals, not the speed of work over short ones. A token bucket holds credit up to a capacity and refills it at the rate , here 25% of the processor; a request runs only if the credit covers its cost. Credit saved while the worker was idle, up to , can be spent at once, and the admitted work then runs at full speed.
With execution-milliseconds (milliseconds of processor time) and execution-milliseconds per wall-millisecond (millisecond of real time), a full bucket admits a 4-millisecond operation that runs at 100% of the processor for 4 wall-milliseconds. The rate still holds on average: in any interval of wall-milliseconds at most execution-milliseconds are admitted, so at most in 100 wall-milliseconds and at most in 1000, which tends to 25% as the interval grows. A stretch at full speed lasts at most wall-milliseconds. To limit the speed of bursts as well, lower or add a second limit with a small capacity.
Related entries: Post-charge debt and a one-operation overshoot bound (§19.4.2), Duty-cycle bound versus per-request response-time bound (§19.4.6), Policing, shaping, and admission control (§19.4.5). Further reading: [90].