Definition
Post-charge debt is the negative balance that a token bucket §19.4.1 can reach when it charges each operation after the operation finishes. A token bucket holds credit that refills at a fixed rate up to a cap. Under post-charging, an operation may start whenever the balance is zero or more, and its measured cost is subtracted when it ends, possibly taking the balance below zero. The one-operation overshoot bound states that if at most one operation runs at a time and no cost exceeds , the debt never exceeds .
The idea
A token bucket that charges in advance needs each operation’s cost before the operation starts. Many costs are known only at the end: the processor time needed to parse a message depends on the message, and the time to erase a block of flash memory §6.6 varies from chip to chip. Charging every operation its largest possible cost in advance is safe, but wastes credit whenever an operation is cheaper. Post-charging avoids the estimate: admit the operation if the balance is not negative, run it, measure what it cost, and subtract that.
The price of this choice is debt. An operation admitted on a balance of 0 that costs 3 units leaves the balance at , and the next operation waits until refill has repaid the 3 units. An overdraft rule is a fair picture. A customer may start a purchase only while the account is not overdrawn, and the price is known only at the till, so one purchase can overdraw the account by at most its own price. That limit needs a price ceiling and one purchase at a time: a second customer using the same account at the same moment doubles the possible overdraft. The picture stops being accurate in one respect: the bucket’s income arrives continuously at a fixed rate, and stops while the balance is at the cap.
How it works
The bucket keeps five values.
The balance , in cost units, which may be negative.
The capacity , the largest balance allowed.
The refill rate , in cost units per second.
The time of the last update, read from a monotonic clock §9.35, a clock that never runs backwards.
An in-flight flag, which records that an operation has been admitted and not yet charged.
Refill. At every event, add to the rate times the time elapsed since the last update, lower the result to if it is larger, and record the current time as the last update. A negative balance rises only through refill; it is never reset to 0.
Admission. Refill, then admit the operation only if and the in-flight flag is clear, and set the flag. The test and the setting of the flag form one atomic step: no other caller can run between them.
Completion. Refill, then subtract the operation’s measured cost from , and clear the flag. The cost must satisfy .
Waiting. A request that finds or the flag set waits or is refused. From a debt of units, refill brings the balance back to 0 after seconds.
Why the debt stays within one operation. When an operation is admitted, . While it runs, only refill changes , and refill never lowers it. At completion falls by , so afterwards . No operation is admitted again until refill has raised to 0 or more. So always lies between and .
Why the charged work exceeds the usual limit by at most one operation. The balance changes only through refill, which adds, and charges, which subtract. So the charges made during a stretch of seconds equal the starting balance, at most , plus the refill, at most , minus the final balance, at least : at most . A bucket that charges in advance never goes below 0, so the same argument gives it .
Worked example
Take capacity units, refill rate unit per second and largest cost units. Each operation runs for 0.5 seconds. Watch the interval from time 0 to time 4 seconds, so seconds. At time 0 the balance is 1.5 units and the worker is idle.
Time 0: operation A is admitted, since .
Time 0.5: A ends. Refill adds units, giving , which does not exceed , so no refill is lost. The measured cost of A is 2 units: . Since , operation B is admitted at once.
Time 1: B ends. Refill adds 0.5 units, giving 0.5. The measured cost of B is 3 units: . The debt, 2.5 units, is within .
Refill repays the debt in seconds, so at time the balance is 0 and operation C is admitted.
Time 4: C ends. Refill adds 0.5 units, giving 0.5. The measured cost of C is 3 units: .
Check the bookkeeping. The charges in the interval are units. Refill credited units, none of it lost, because the balance never went above 2. Starting balance plus refill minus final balance is units, which matches. The bound is units, and . A bucket with the same and that charged in advance could admit at most units in any 4-second interval. Post-charging let units more through, within the allowance of one operation, 3 units. The bound of 9 is not reached here because the interval started at 1.5 units, not a full 2, and ended at , not .
Now break the one-at-a-time rule. Three callers P, Q and R each test the balance while it is 0, and the test is not atomic with setting the flag, so all three pass before any flag is set. This is a race condition §9.2: the outcome depends on how the callers’ steps interleave. Each operation costs the maximum, 3 units. After the three charges the balance is units, three times the one-operation debt of 3. The bound for the 4-second interval becomes units instead of 9.
Stated precisely
Let be the capacity, the refill rate in cost units per second, the largest cost that any operation can be charged, and the largest number of operations that may be in flight at once. Let be the balance at time after every refill and charge due at . The bucket follows four rules.
Between events, rises at rate while it is below , and never exceeds .
An operation is admitted only at a time when and fewer than operations are in flight; the test and the admission form one atomic step.
Each operation is charged exactly once, at its completion, with its measured cost , where .
Nothing else changes , and .
Two statements follow. Balance invariant: for every . At the last admission at or before , the balance was at least 0, and since then only the at most operations in flight at that moment can have been charged; if no admission has happened, no charge has either, and . Charged-work envelope: for times with , let be the sum of the charges made in , the times after up to and including , and let be the refill credited for that time. Conservation of credit gives and since , and , With this is the one-operation overshoot bound.
Two points of bookkeeping matter. First, counts each cost whole, at the moment it is charged. It is not the work done inside the interval: an operation charged just after counts in full although it ran mostly before , and an operation still running at does not count at all. Second, counts only refill earned between and . A program that computes refill only at events credits, at its first event after , refill that was partly earned before . In the argument, that earlier part belongs to , the balance with all refill due by applied.
Limits and common mistakes
The bound limits the total charged work, not the time any single request waits. With a positive refill rate and no intervening charge, a debt of units clears after seconds. That is the earliest the balance can permit another admission. A request may wait longer for service. In the single-worker example, the largest debt takes seconds to clear. If , a negative balance never recovers under these rules. The Duty-cycle bound versus per-request response-time bound §19.4.6 entry treats response time §9.32.
Forgiving debt breaks conservation. A program that resets a negative balance to 0 hands out the forgiven units as free credit, and they appear in no term of the bound. In the example, resetting to 0 at time 1 would have admitted C at once instead of at time 3.5.
The maximum cost must be a real maximum. The largest cost seen in tests is not one. is either proved, as a worst-case execution time §9.33 (an upper bound on running time that holds for every input), or enforced, for example by stopping an operation whose cost reaches it. Cost must also be measured in the unit the bound uses: wall time §18.1.5, the time that passes on an ordinary clock, includes time in which other code interrupted the operation, so it can exceed a maximum derived for CPU time, the time the processor spent on the operation itself.
The capacity is essential. Without it, has no upper limit, and a worker that was idle for an hour could spend an hour’s refill at once.
Refill must not create credit. Rounding a fractional refill up, crediting the same elapsed time twice, or reading a clock that can jump forward each makes exceed . Rounding down is safe but lowers the real rate; the Fixed-point units and fractional-refill loss §19.4.3 entry treats this.
Every caller must go through the bucket. Work started on a path that skips admission or skips the charge is not limited at all.
Questions answered
What breaks the one-operation allowance?
The one-operation allowance breaks whenever one of the three facts behind it fails: the balance at the start of an interval is at most the capacity ; the refill credited during the interval is at most the refill rate times the interval’s length ; and the balance never falls below , where is the largest cost of one operation. The allowance is the extra of charged work, beyond in any interval of seconds, that a token bucket, whose credit refills at rate up to , permits when it charges each operation after completion with its measured cost and admits an operation whenever the balance is not negative.
The usual ways to break it are these.
Several operations in flight. If three callers all test a balance of 0 before any of them is charged, and each then charges units, the balance falls to units, three operations of debt.
Callers that bypass the bucket, whose work is never admitted or never charged.
A cost with no proved or enforced maximum, or a cost measured in a different unit from .
Forgiven debt: resetting a negative balance to 0.
Refill errors: a missing cap, rounding up, crediting the same time twice, or a clock that jumps forward.
The repair is to make testing the balance and marking an operation as in flight one atomic step that allows one operation at a time, to route every caller through that step, and to keep the debt until refill repays it. A design that must allow operations at once can use the bound instead.
Related entries: Rate limiting (§9.38), Fixed-point units and fractional-refill loss (§19.4.3), Duty-cycle bound versus per-request response-time bound (§19.4.6), Token bucket: rate, burst, and the arrival envelope (§19.4.1).