Section 19.4.2

Post-charge debt and a one-operation overshoot bound

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 CmaxC_{\max}, the debt never exceeds CmaxC_{\max}.

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 −3-3, 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.

  1. Refill. At every event, add to xx the rate rr times the time elapsed since the last update, lower the result to bb 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.

  2. Admission. Refill, then admit the operation only if x≥0x \ge 0 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.

  3. Completion. Refill, then subtract the operation’s measured cost cc from xx, and clear the flag. The cost must satisfy 0≤c≤Cmax0 \le c \le C_{\max}.

  4. Waiting. A request that finds x<0x < 0 or the flag set waits or is refused. From a debt of dd units, refill brings the balance back to 0 after d/rd / r seconds.

Why the debt stays within one operation. When an operation is admitted, x≥0x \ge 0. While it runs, only refill changes xx, and refill never lowers it. At completion xx falls by c≤Cmaxc \le C_{\max}, so afterwards x≥0−Cmax=−Cmaxx \ge 0 - C_{\max} = -C_{\max}. No operation is admitted again until refill has raised xx to 0 or more. So xx always lies between −Cmax-C_{\max} and bb.

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 TT seconds equal the starting balance, at most bb, plus the refill, at most rTr T, minus the final balance, at least −Cmax-C_{\max}: at most b+rT+Cmaxb + r T + C_{\max}. A bucket that charges in advance never goes below 0, so the same argument gives it b+rTb + r T.

Worked example

Take capacity b=2b = 2 units, refill rate r=1r = 1 unit per second and largest cost Cmax=3C_{\max} = 3 units. Each operation runs for 0.5 seconds. Watch the interval from time 0 to time 4 seconds, so T=4T = 4 seconds. At time 0 the balance is 1.5 units and the worker is idle.

  1. Time 0: operation A is admitted, since 1.5≥01.5 \ge 0.

  2. Time 0.5: A ends. Refill adds 1×0.5=0.51 \times 0.5 = 0.5 units, giving 1.5+0.5=21.5 + 0.5 = 2, which does not exceed b=2b = 2, so no refill is lost. The measured cost of A is 2 units: 2−2=02 - 2 = 0. Since 0≥00 \ge 0, operation B is admitted at once.

  3. Time 1: B ends. Refill adds 0.5 units, giving 0.5. The measured cost of B is 3 units: 0.5−3=−2.50.5 - 3 = -2.5. The debt, 2.5 units, is within Cmax=3C_{\max} = 3.

  4. Refill repays the debt in 2.5/1=2.52.5 / 1 = 2.5 seconds, so at time 1+2.5=3.51 + 2.5 = 3.5 the balance is 0 and operation C is admitted.

  5. Time 4: C ends. Refill adds 0.5 units, giving 0.5. The measured cost of C is 3 units: 0.5−3=−2.50.5 - 3 = -2.5.

Check the bookkeeping. The charges in the interval are 2+3+3=82 + 3 + 3 = 8 units. Refill credited 1×4=41 \times 4 = 4 units, none of it lost, because the balance never went above 2. Starting balance plus refill minus final balance is 1.5+4−(−2.5)=81.5 + 4 - (-2.5) = 8 units, which matches. The bound is b+rT+Cmax=2+1×4+3=9b + r T + C_{\max} = 2 + 1 \times 4 + 3 = 9 units, and 8≤98 \le 9. A bucket with the same bb and rr that charged in advance could admit at most 2+4=62 + 4 = 6 units in any 4-second interval. Post-charging let 8−6=28 - 6 = 2 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 −2.5-2.5, not −3-3.

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 0−3−3−3=−90 - 3 - 3 - 3 = -9 units, three times the one-operation debt of 3. The bound for the 4-second interval becomes 2+4+3×3=152 + 4 + 3 \times 3 = 15 units instead of 9.

Stated precisely

Let b≥0b \ge 0 be the capacity, r≥0r \ge 0 the refill rate in cost units per second, CmaxC_{\max} the largest cost that any operation can be charged, and k≥1k \ge 1 the largest number of operations that may be in flight at once. Let x(t)x(t) be the balance at time tt after every refill and charge due at tt. The bucket follows four rules.

  1. Between events, xx rises at rate rr while it is below bb, and never exceeds bb.

  2. An operation is admitted only at a time when x≥0x \ge 0 and fewer than kk operations are in flight; the test and the admission form one atomic step.

  3. Each operation is charged exactly once, at its completion, with its measured cost cc, where 0≤c≤Cmax0 \le c \le C_{\max}.

  4. Nothing else changes xx, and 0≤x(0)≤b0 \le x(0) \le b.

Two statements follow. Balance invariant: −kCmax≤x(t)≤b-k\,C_{\max} \le x(t) \le b for every tt. At the last admission at or before tt, the balance was at least 0, and since then only the at most kk operations in flight at that moment can have been charged; if no admission has happened, no charge has either, and x(t)≥x(0)≥0x(t) \ge x(0) \ge 0. Charged-work envelope: for times t0<t1t_0 < t_1 with T=t1−t0T = t_1 - t_0, let WW be the sum of the charges made in (t0,t1](t_0, t_1], the times after t0t_0 up to and including t1t_1, and let RR be the refill credited for that time. Conservation of credit gives W=x(t0)+R−x(t1),W = x(t_0) + R - x(t_1), and since x(t0)≤bx(t_0) \le b, R≤rTR \le r T and x(t1)≥−kCmaxx(t_1) \ge -k\,C_{\max}, W≤b+rT+kCmax.W \le b + r T + k\,C_{\max}. With k=1k = 1 this is the one-operation overshoot bound.

Two points of bookkeeping matter. First, WW counts each cost whole, at the moment it is charged. It is not the work done inside the interval: an operation charged just after t0t_0 counts in full although it ran mostly before t0t_0, and an operation still running at t1t_1 does not count at all. Second, RR counts only refill earned between t0t_0 and t1t_1. A program that computes refill only at events credits, at its first event after t0t_0, refill that was partly earned before t0t_0. In the argument, that earlier part belongs to x(t0)x(t_0), the balance with all refill due by t0t_0 applied.

Limits and common mistakes

The bound limits the total charged work, not the time any single request waits. With a positive refill rate rr and no intervening charge, a debt of dd units clears after d/rd/r 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 3/1=33/1=3 seconds to clear. If r=0r=0, 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 −2.5-2.5 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. CmaxC_{\max} 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, x(t0)x(t_0) 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 RR exceed rTr T. 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 bb; the refill credited during the interval is at most the refill rate rr times the interval’s length TT; and the balance never falls below −Cmax-C_{\max}, where CmaxC_{\max} is the largest cost of one operation. The allowance is the extra CmaxC_{\max} of charged work, beyond b+rTb + r T in any interval of TT seconds, that a token bucket, whose credit refills at rate rr up to bb, 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.

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 kk operations at once can use the bound b+rT+kCmaxb + r T + k\,C_{\max} 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).