Token bucket
We separate a sustained replenishment rate from a permitted burst. A saved allowance lets a quiet user act quickly without granting unlimited long-term work. We will refill lazily, debit atomically, derive the burst bound, and handle weighted costs and clock changes.
#Section 17Tokens represent spendable allowance
A viewer was idle, then opens several score pages together. A token bucket stores a bounded allowance that replenishes with time. Each admitted request spends tokens; capacity limits how much allowance an idle user can save.
The illustrative Heron bucket holds at most 100 tokens and refills at 100 divided by 60 tokens per second. A cost-one request requires at least one token. The algorithm can admit a short burst from saved allowance while enforcing a sustained refill rate across a long interval. Capacity and refill rate are different policy parameters.
Read this as available tokens equal the smaller of bucket capacity and old balance plus refill rate times elapsed time . A request of cost is admitted only if , then the new balance becomes .
#Section 18The full intermediate token trace
Start with 100 tokens at illustrative time zero. A burst costing 80 leaves 20. After 6 seconds, refill adds 6 times 100 divided by 60, or 10 tokens, so the balance becomes 30. A request group costing 15 leaves 15.
An immediate group costing 20 cannot pass. It lacks 5 tokens, and refill rate is 100 divided by 60, so the minimum modeled wait is 5 divided by that rate, or 3 seconds. Concurrent spend can make the actual future decision different; the wait is advice rather than a reservation.
Do not set the balance negative in this policing variant. A virtual-time or debt variant can have different semantics, but it must bound debt and waiting. Name which construction you use before interpreting a negative stored number.
#Section 19The burst-plus-rate bound
The bucket begins with at most tokens. During an interval of duration , refill contributes at most more. Therefore the admitted cost in that interval cannot exceed the initial saved allowance plus added allowance, under one correctly serialized bucket and its clock assumptions.
Read this as admitted cost over duration is at most capacity plus refill rate times the duration. For capacity 100 and 60 seconds at refill 100 divided by 60, the upper accounting bound is 200 tokens. Arrival placement and the endpoint convention affect whether the bound is reached exactly.
Refill under a cap can discard credit when the bucket is full, making actual admitted cost lower. This does not invalidate the bound. The proof also exposes the distributed failure risk: creating independent fresh buckets gives each replica its own , and restoring a stale balance can recreate already-spent credit.
#Section 20Weighted cost and time monotonicity
One cheap score read can cost one token while a conversion costs more. For an illustrative upload cost rule of one token per 100,000,000 bytes, a 5,000,000,000-byte upload costs 50 tokens. This is an assigned policy unit, not a claim that CPU cost scales exactly with byte size.
For costs 20, 30, and 50 with no refill, the total is 100 and exhausts the full bucket. Another cost-one request would bring the total to 101 and must be rejected. Validate that each cost is positive and within policy range. Negative or malformed cost must never create allowance.
Clock rollback must not add negative refill or move retained time backward. Use nonnegative elapsed time and retain a nondecreasing accounting timestamp. Across replicas, one authoritative time source or explicit skew bounds are needed. Process-local monotonic clocks are excellent for elapsed local waits but cannot be copied blindly as a shared epoch.