Token Bucket Rate Limiting Algorithm

Machine Learning
Introduced by
Jon Turner
Year
1986
Type
rate limiting algorithm
Family
Traffic shaping

What does it do?

Token bucket is an algorithm for controlling the rate of traffic in networks.

A virtual bucket accumulates tokens at a fixed rate R up to a fixed capacity C, and a packet of N bytes is sent only when enough tokens are available to cover it, with those tokens then removed. The result bounds the long-run average rate and the size of any burst, while allowing short bursts up to C to pass unchanged.

The intuition behind the method

Picture a bucket that fills with tokens at a steady rate and can hold at most C tokens; tokens that arrive when the bucket is full are discarded. To send a packet, remove one token per byte of the packet. If the bucket does not hold enough tokens, the packet must wait (in a shaper) or is treated as non-conforming (in a policer). A source that has been idle can therefore send a burst of up to C bytes at once, but over a long period it cannot send faster than R on average.

History

The first published description of the token bucket is not documented here. RFC 2697 (1999) defines a single rate three colour marker, whose metering uses token-bucket-style counters. The RFC does not standardise the token bucket as a general algorithm.

When to use it

Choose a token bucket when the long-run average rate must be limited but short bursts up to a chosen size are acceptable, for example when policing traffic against a per-class contract at a network edge, as in DiffServ traffic conditioning. Metering schemes such as the single rate three colour marker in RFC 2697 use token-bucket-style counters. Do not use a token bucket when the output must follow a constant rate with no bursts, since a leaky bucket shaper is designed for that. It is also not sufficient on its own to control congestion inside the network, because it does not observe queues at downstream links.

Limitations and considerations

  • Bursts up to C are allowed, so the instantaneous output rate can exceed R; the algorithm does not cap the peak rate
  • Basic token bucket gives no feedback to the sender, so non-conforming packets are only delayed, dropped or marked
  • A single bucket treats all traffic that passes through it as one stream; per-flow rate limits need one bucket per flow, so state grows with the number of flows
  • A packet larger than C can never accumulate enough tokens to conform

Source conditions do not replace independent scientific review or validation by an analysis engine.

Strengths

  • Two parameters, the token rate R and the capacity C, set both the long-run average rate and the largest burst, which makes the behaviour easy to reason about
  • Each packet needs one comparison and one subtraction, so the test is cheap to implement in hardware or software
  • Sources that have been idle can send bursts up to C without delay, so occasional bursty traffic is not penalised while the average rate stays bounded

Applications

  • Traffic policing and metering in DiffServ, for example the single rate three colour marker of RFC 2697, which uses token-bucket-style counters to mark packets
  • Ingress rate limiting and enforcement of per-customer traffic contracts at network edges
  • Request throttling in software systems, where a bucket of request tokens refills over time; implementations differ in detail
  • Traffic shaping in front of a link, where non-conforming packets are queued so that output is smoothed

How it works

How it works

1

A packet of N bytes arrives at the shaper or meter.

2

Compare the current token count with N. The packet conforms only if the bucket holds at least N tokens.

3

Tokens are added at rate R up to the capacity C. Tokens that would exceed C are discarded, so idle time cannot build up credit beyond C.

4

If the packet conforms, N tokens are removed and the packet is sent. A non-conforming packet is queued (shaping) or dropped or marked (policing) and removes no tokens.

5

Over a long interval the conforming traffic cannot exceed R on average, plus a burst of at most C. Bursts up to C pass without delay, while sustained excess is delayed or discarded.

Common pitfalls

  • Setting C smaller than the largest packet size means those packets never conform, so they are always delayed or dropped
  • Mixing up rate units: R counts tokens per second, and with one token per byte this is bytes per second, not bits per second, so a link rate given in bits needs a conversion
  • Treating policing and shaping as the same: a policer drops or marks non-conforming packets, while a shaper queues them and adds delay
  • Expecting the bucket to prevent congestion downstream: it limits only the traffic admitted at its own point, so queues at later links are unaffected
  • Assuming unused capacity accumulates without limit: tokens are capped at C, so long idle periods do not allow bursts larger than C

Frequently asked questions

What is the difference between token bucket and leaky bucket?

A token bucket allows bursts up to its capacity, so a packet can be sent immediately when enough tokens have accumulated. A leaky bucket is usually described as smoothing output to a constant rate, with excess traffic queued or dropped, so it does not pass bursts through unchanged. The token bucket is therefore more tolerant of bursty traffic.

How is the token rate tuned?

R sets the long-run average rate, and C sets the largest burst that can be sent at once. If one token represents one byte, R is in bytes per second and C is in bytes. Convert to bits per second separately if the link is specified in bits.

What if a packet exceeds the bucket capacity?

A packet larger than C can never accumulate enough tokens, so it will always be dropped or queued indefinitely. Packets should be smaller than C; if necessary, increase C.

How does token bucket interact with congestion?

A token bucket limits the rate at which traffic is admitted, but it does not observe queues at downstream links. Congestion needs to be handled by active queue management (AQM) or similar mechanisms at the congested point.

Related methods

Sources

  1. Turner, J. S. (1986). New directions in communications (or which way to the information age?). IEEE Communications Magazine, 24(10), 8-15.

    Open publication reference
  2. Heinanen, J., Guerin, R., & May, A. (1999). A Single Rate Three Color Marker. RFC 2697.

    Open publication reference