Definition
Policing, shaping and admission control are three ways to keep work within a rate or capacity. Policing checks each request on arrival and immediately rejects, or marks for lower priority, a request that exceeds the rate. Shaping holds an excess request in a queue and releases it when it fits the rate, delaying excess work instead of refusing it. Admission control decides once, when a new stream of work asks to start, whether its declared demand fits the remaining capacity, and refuses the whole stream if not.
The idea
A rate limiter §9.38 must do something with a request that arrives when the allowed rate is used up, and that choice decides what the requester sees. Suppose a device accepts at most 2 commands per second, and a controller sends 5 permitted commands at once. The device can refuse the 3 extra commands now (policing), or hold them and run them over the next 1.5 seconds (shaping). Had the controller announced a steady demand of 5 commands per second when it connected, the device could have refused that demand before any command ran (admission control). One token bucket §19.4.1, a counter of credit that refills at a fixed rate up to a maximum and is spent by requests, can drive all three: it says only whether a request fits now, not what happens to one that does not.
Policing suits work that loses its value when late, or a source that must be stopped quickly; the requester learns at once and can retry or give up. Shaping suits work that must not be lost and can tolerate delay. Admission control suits recurring work with a promised level of service: refusing a newcomer up front protects the promises already made, which would otherwise all degrade together.
None of the three is an authorization §1.7 decision. Authorization decides whether a requester may perform an action at all, and a security policy reports it as a verdict, typically the word ALLOW or the word DENY. A rate limit answers a different question: whether there is capacity for a permitted action now. Reporting a capacity shortage with the verdict DENY tells the requester that a permitted action is forbidden.
How it works
A policer, for each arriving request:
Refill the token bucket from the elapsed time.
If the bucket holds the request’s cost, subtract the cost and pass the request on.
Otherwise act on the request at once: reject it with a result that says the rate was exceeded, preferably with the time after which a retry can succeed, or mark it so that a later stage drops it first. Nothing is stored.
A shaper:
Append each arriving request to a bounded queue §9.31, a queue with a fixed maximum length. If the queue is full, the shaper must reject the request, which is policing at the edge of the queue.
When the request at the head of the queue fits the bucket, subtract its cost and release it.
Otherwise compute how long until it fits, rounding up so as not to wake too early, and sleep.
Each queued request needs a deadline, or must support cancellation §9.29, so that a request nobody wants any more leaves the queue.
A new stream of work declares its demand, for example a rate and a burst size, before it starts.
The controller adds the declared demand to the demand already admitted and compares the sum with the capacity.
If the sum fits, the stream is admitted and its share is reserved. The stream is then policed or shaped to its declaration, because every promise rests on every stream keeping to what it declared.
If the sum does not fit, the stream is refused before any of its work runs.
The Linux deadline scheduler, SCHED_DEADLINE, works this way for processor reservations: a request for a reservation that would push the total reserved share of the processor past a configured limit fails.
Distinct result codes keep the outcomes apart. The web’s protocol, HTTP (Hypertext Transfer Protocol), is a familiar example: status 403 Forbidden is an authorization refusal, while status 429 Too Many Requests, defined in RFC 6585 [441], is a rate refusal, and the server may add a Retry-After header that says when to try again.
Worked example
A device grants 2 tokens per second, one token every 0.5 seconds, holds at most 2 tokens, and charges 1 token per command. The bucket is full at time 0, when a controller sends 5 commands that the security policy allows.
Policing. Commands 1 and 2 use the 2 tokens and run at 0 s. Commands 3, 4 and 5 are rejected at 0 s with the result “rate exceeded, retry after 0.5 s”. If the controller resends command 3 at 0.5 s, one token has been refilled and command 3 runs.
Shaping with a queue of 3. Commands 1 and 2 run at 0 s. Commands 3, 4 and 5 wait and run when tokens arrive, at 0.5 s, 1.0 s and 1.5 s, delayed by 0.5 s, 1.0 s and 1.5 s. All five commands run and none is refused. With a queue of only 2, command 5 finds the queue full and is rejected.
Admission control. The device can serve 2 commands per second in total and has admitted two streams declaring 0.5 and 1.0 commands per second, together . A new stream declaring 1.0 per second would bring the total to and is refused before it sends anything. A stream declaring 0.5 per second brings it to and is admitted.
The verdict DENY instead. The limiter answers commands 3, 4 and 5 with DENY. The controller treats DENY as a prohibition: it does not retry, and it records three security violations. Half a second later the device had capacity for command 3, but the command is lost, and the audit log reports an attack that never happened.
Stated precisely
Let be the amount of work, in tokens, that arrives in the time interval from to , in seconds. A stream conforms to a token bucket with rate tokens per second and burst size tokens when for every interval. The three mechanisms then promise the following.
Policer. It answers every request without delay. The requests it passes conform. It rejects a request only if passing it would break conformance, given the requests it passed before. If the bucket is full at the start, a stream that already conforms passes unchanged.
Shaper. The requests it releases conform, and each is delayed no longer than needed for that. With a FIFO §14.14 (first in, first out) queue of length , requests leave in arrival order, and none is lost while at most requests wait. If work arrives faster than for long enough, the queue fills and further requests are rejected.
Admission control with a capacity of tokens per second. A new stream with declared rate is admitted only if , where to are the declared rates of the streams already admitted. The promises to admitted streams assume that each stream conforms to its declaration.
An authorization verdict depends on who asks, what is asked and the conditions the policy names. A rate outcome depends on the recent history of requests. The verdict DENY may report a rate outcome only when the policy itself states the rate rule, as a limit on password guesses does, and the interface tells every caller that DENY can mean too many requests.
Limits and common mistakes
Shaping causes head-of-line blocking §9.19: in a FIFO shaper, an expensive request at the head of the queue holds back cheap requests behind it, even when their cost is already available.
One shared bucket is not fair. A single source can use the whole rate and starve the others. Fairness needs a bucket or a queue for each source.
Delay is not neutral when a decision has a deadline. If an action must be allowed or refused by a certain moment, a decision that arrives later is no decision, and the system does whatever its default is: fail open §1.14 (the action proceeds unchecked) or fail closed §1.13 (the action is refused). Shaping such a decision must state which of the two happens when its deadline passes.
A queue without a bound turns overload into memory exhaustion and unbounded delay. A full queue must push back on the producer, which is backpressure §9.30, or reject.
Rejected requests often come back: clients that all retry at once multiply the load. A retry-after time, or randomly spread retry times, reduces that.
The name of a component does not say which behavior it has: a component called a token bucket may police or shape. Read how each outcome is returned to the caller, and what the caller does with it.
Questions answered
Why not throttle by returning DENY?
DENY should not be used for throttling, because DENY is an authorization verdict: it says that the requester is not permitted to perform the action. Throttling means something else: the action is permitted, but there is no capacity for it now, so it must wait or be retried. The one exception is an interface whose policy itself states the rate rule and which tells every caller that DENY can mean too many requests. Callers react to DENY as to a prohibition. They do not retry, they report a security violation, and they may disable a feature or count the event toward locking the requester out. A temporary overload thus becomes a lost request and a false alarm §1.22, and the audit log can no longer tell attacks from load.
Keep three outcomes apart: the authorization verdict, ALLOW or DENY; the rate refusal of policing, rejected now with a hint of when to retry, like the web’s status 429 Too Many Requests beside 403 Forbidden; and the delay of shaping, queued in a bounded queue with a deadline or cancellation. Refuse a recurring workload that cannot fit at admission, before it starts. If a decision must arrive before an action happens, a delayed decision is no decision, so state explicitly what happens when the deadline passes.
Related entries: Backpressure (§9.30), Bounded queue (§9.31), Policy versus mechanism (§1.19), Observation versus enforcement (§1.20), Rate limiting (§9.38), Token bucket: rate, burst, and the arrival envelope (§19.4.1), Authorization (§1.7), Resource cap versus resource reservation (§13.22). Further reading: [441].