Skip to main content

Token bucket algorithm

Short answer

Token bucket limits average rate r, allows bursts of up to b packets. Tokens added at rate r into bucket of capacity b; a packet consumes one token if available, else waits or is dropped. Idle hosts accumulate tokens for bursts. Unlike leaky bucket, it permits variable output rates and saves idle credit as tokens.

Explain the token bucket algorithm for congestion control, with a diagram.

The token bucket algorithm is a traffic-shaping / congestion-control method that limits the average rate of a flow but still allows short bursts.

Working

  1. A bucket of capacity b tokens holds tokens. Tokens are added at a constant rate r (tokens per second). If the bucket is full, new tokens are discarded.
  2. One token permits one packet (or one byte, in byte-based versions) to be sent.
  3. When a packet arrives, if enough tokens are present, the tokens are removed and the packet is sent at once. If not, the packet waits (shaping) or is dropped/marked (policing).
  4. An idle host saves tokens, so later it can send a burst of up to b packets.
  tokens added at rate r
          |
          v
      +--------+
      | bucket |  capacity b
      | o o o  |
      +---+----+
          | one token per packet
 packets  v
 -------> [ queue ] ---(token available?)---> network

Example

Bucket capacity b = 5 tokens, r = 1 token per tick, 1 token per packet, bucket initially full.

TickPackets arriveTokens availableSentWaiting
05550
10100
20200
33330
43112
50111
60110

The burst of 5 packets passes at once, but in the long run the rate cannot exceed 1 packet per tick.

Token bucket vs leaky bucket

PointLeaky bucketToken bucket
Output rateConstantVariable; bursts allowed
Idle creditNot savedSaved as tokens
BurstsSmoothed outAllowed up to b
When bucket is fullPackets discardedTokens discarded, packets kept

If M is the peak output rate of the link, the maximum time S for which a burst can be sent at rate M is S = b / (M − r).

✓ Answer checked by Claude Opus 5.5 (high effort)

All Computer Networks and Security chapter 5 questions →