Section 9.38

Rate limiting

Definition

Rate limiting is a policy that caps how much work a source can have accepted per unit of time, counted in a chosen unit such as requests, bytes or processing cost. A rate limiter enforces the policy: for each arriving request, it decides to admit, delay or reject the request according to what the same source, or all sources together, has already used. Rate limiting protects a shared resource against overload and slows attackers who make repeated attempts, such as guesses at a password.

The idea

A device accepts a 6-digit PIN (personal identification number) for a privileged operation. Without a limit, an attacker who can try 100 PINs per second tries all 10610^6 of them in 106/100=10,00010^6 / 100 = 10{,}000 s, under three hours. With a limit of 5 attempts per hour, the same search takes 106/5=200,00010^6 / 5 = 200{,}000 hours, about 22.8 years. Rate limiting does not make the PIN longer; it makes each guess slow.

A weekly allowance with a savings cap is the picture for the most common mechanism, the token bucket §19.4.1. Money arrives at a fixed rate, unspent money accumulates only up to the cap, and every purchase spends some. A saver can spend the whole cap in one burst, but over a long period cannot spend more than the rate. The picture stops being accurate in one respect. A rate limiter can tell sources apart only by the identity each source presents, and an attacker who can appear as many sources gets a separate allowance for each.

How it works

Four mechanisms are common:

A token bucket needs no timer. Store the balance and the time of the last update, and on each request:

  1. read a monotonic clock §9.35, a clock that never moves backward, so that a change of the date and time can neither refill nor empty the bucket;

  2. add rr times the time elapsed since the last update to the balance, cap the balance at bb, and record the new update time;

  3. if the balance is at least the request’s cost, subtract the cost and admit the request; otherwise reject it, or tell the sender when enough tokens will exist.

Steps 2 and 3 must together form one atomic operation §9.8, one that no other thread can interleave with. Two threads that both read a balance of 1 and both subtract 1 admit two requests on one token.

Scope: what counts as one source. The limiter keys each budget by an identity, and that choice decides what is protected:

Real systems combine scopes: a per-identity limit sized for one legitimate user, plus a per-device or global limit sized for what the resource can absorb.

Charge the unit that the resource actually spends. If a token buys one request, a request with a 1 MiB payload costs the same as one with 10 bytes. A limiter that protects bandwidth or memory charges bytes. RFC 2215, which describes network traffic with a token bucket, measures the rate rr in bytes per second and the bucket depth bb in bytes. It also charges every packet smaller than a minimum policed unit mm as if the packet were mm bytes long, so that the per-packet cost of many small packets stays bounded.

Worked example

Take a token bucket with capacity b=4b = 4 tokens and refill rate r=2r = 2 tokens per second, where each request costs 1 token. At time 0 the bucket is full and 4 requests arrive: all 4 are admitted, and the balance is 0. A fifth request at time 0 is rejected. At 1 s the bucket has earned 2×1=22 \times 1 = 2 tokens; 2 requests arrive and are admitted, and the balance is 0 again. At 1.5 s the bucket has earned 2×0.5=12 \times 0.5 = 1 token, and one more request is admitted. In the 1.5 s from time 0, 4+2+1=74 + 2 + 1 = 7 requests were admitted, which equals the bound b+rt=4+2×1.5=7b + r t = 4 + 2 \times 1.5 = 7 stated below.

Many identities: if each of 100 identities has its own bucket with b=4b = 4 and r=2r = 2, their combined initial burst is 100×4=400100 \times 4 = 400 requests, not 4, and their combined long-run rate is 100×2=200100 \times 2 = 200 requests per second. A global bucket with b=4b = 4 and r=2r = 2, debited by every admission in addition to the per-identity bucket, holds the total in the first 1.5 s to 7 requests again.

Fixed-window boundary burst: with a limit of 100 requests per minute, a source sends 100 requests at 0:59 and 100 more at 1:00. Each window holds 100, so all 200 are admitted within about 1 s: twice the intended per-minute amount, in one second.

Guessing: a 6-digit PIN has 10610^6 equally likely values. A per-device bucket with b=5b = 5 and r=5r = 5 per hour admits at most 5+5×720=3,6055 + 5 \times 720 = 3{,}605 guesses in 30 days, which is 30×24=72030 \times 24 = 720 hours. An attacker who never repeats a guess succeeds with probability at most 3,605/106≈0.00363{,}605 / 10^6 \approx 0.0036, that is 0.36%.

Stated precisely

Ideal token bucket. Let bb be the capacity in tokens, rr the refill rate in tokens per second, and A(s,s+t)A(s, s + t) the total cost admitted in the interval of length tt seconds that starts at time ss. The balance at time ss is at most bb, the bucket earns at most rtr t tokens during the interval, and every admission spends its cost, so A(s,s+t)≤b+rtfor every s and every t≥0.A(s, s + t) \le b + r t \quad \text{for every } s \text{ and every } t \ge 0. The average admitted rate over the interval, A(s,s+t)/tA(s, s + t) / t, is therefore at most b/t+rb / t + r, which approaches rr as tt grows. Refill that arrives while the bucket is full is discarded, so a source that leaves its credit unused gets less than the bound.

Guessing. If a secret is chosen uniformly from NN values and every attempt against it is charged to one bucket, an attacker who never repeats a guess succeeds within time tt with probability at most (b+rt)/N(b + r t) / N. The bound holds only if every attempt against that secret is charged to that bucket, whatever identity the attacker presents.

Several identities. With kk identities that each have their own bucket with capacity bb and rate rr, the total admitted in time tt is at most k(b+rt)k(b + r t). A shared bucket with capacity bgb_g and rate rgr_g that every admission also debits lowers this to at most min(k(b+rt),bg+rgt)\min(k(b + r t),\ b_g + r_g t).

All three bounds assume that the check and the debit are atomic, that elapsed time comes from a monotonic clock, that the arithmetic neither rounds in the sender’s favor nor overflows, and that every server admitting requests uses the same bucket state.

Limits and common mistakes

Rate limiting is not authentication §1.6. It slows senders but does not tell legitimate ones from attackers, and a per-identity limit is only as strong as the cost of creating a new identity.

Rate limiting is not a service guarantee. An admitted request can still wait in a queue; limiting admissions bounds the load but does not promise any request a particular response time §9.32.

Replicas multiply budgets. Three servers that each enforce 10 requests per second admit up to 3×10=303 \times 10 = 30 per second, unless they share one bucket state or each enforces a third of the budget.

Locking an account after a few failed attempts stops guessing, but it hands attackers a way to lock legitimate users out. A limit that slows attempts, with a delay that grows after each failure, keeps the account usable.

Rejected clients often retry at once, which adds load exactly when the system is overloaded. Tell the client when to retry. HTTP (Hypertext Transfer Protocol), the protocol of the web, has the status code 429, Too Many Requests, which may carry a Retry-After header stating how long to wait.

Implementations deviate from the ideal bucket. Refilling in whole tokens while resetting the update time discards the fractional remainder and so loses refill; multiplying a long idle time by the rate can overflow an integer; and a wall clock §9.36 that jumps forward refills the bucket at once. Prove the bound for the implementation, not only for the ideal bucket.

A rate limit bounds arrivals, not work in progress. When requests are slow, many admitted requests can be running at once. A cap on the number of requests in progress, or a bounded queue with backpressure §9.30, a signal that tells senders to slow down, covers that.

Questions answered

Is the limit per peer, credential, device, or global?

A rate limit can be keyed to any of the four, and the right scope is the one that matches the resource being protected, usually a combination of several: a rate limit caps how much work a source can have accepted per unit of time, and its scope decides what counts as one source. A per-credential limit (per account or key) slows guessing against one account, but not one guess spread over many accounts. A per-peer limit (per network address or connection) slows one sender, but fails when addresses are cheap to change or forge. A per-device limit caps the total load on, or guesses against, one target whoever sends them, but lets one abusive sender use up everyone’s budget. A global limit protects the capacity of the whole service, with the same risk at a larger scale.

A sound design states the scope explicitly and usually layers limits: a per-identity budget sized for one legitimate user, plus a per-device or global budget sized for what the resource and its secrets can tolerate. It also states how identities are verified or how costly they are to create, which unit is charged (requests, bytes or processing cost), and how the check and the debit are made atomic, so that concurrent requests cannot spend the same budget twice.

Related entries: Backpressure (§9.30), Bounded queue (§9.31), Denial of service (DoS) (§1.28), Token bucket: rate, burst, and the arrival envelope (§19.4.1), Post-charge debt and a one-operation overshoot bound (§19.4.2), Authentication (§1.6). Further reading: [90].