Transport Layer · Computer Networks

Token Bucket Algorithm

Watch lectures
SVGVector Architecture Diagram (SVG)
COMPUTER NETWORKS · Module 4★ High PriorityToken Bucket AlgorithmIT Sem 6 · Code 100602 · Traffic shaping with burstsMNEMONICToken bucket = tokens arrive at rate r, a packet needs a token to leave.Idle time saves tokens, so bursts up to b are allowed.Core BreakdownIdeaA shaper that allows bursts but keeps the long-term average rate at r.Credit is stored as tokens; the bucket holds at most b tokens.TokensA token generator adds r tokens per second (one token every 1/r s).If the bucket is full, extra tokens are discarded.SendingA packet (or one byte per token) needs token(s) to be sent at once.No token: the packet waits in a queue, or is dropped / marked.BurstAfter an idle period the bucket is full, so up to b tokens can go out together.Burst length S = b / (M - r), where M is the maximum output rate.vs LeakyLeaky bucket: fixed output, no burst. Token bucket: bursts of up to b.Both enforce the average rate; token bucket is more flexible.UsesRouter policing, DiffServ traffic conditioning, API rate limiting.Often combined: token bucket first, then a leaky bucket to smooth the output.Diagram: Token BucketToken genr tokens/sBucket bPacketsarriveNetwork1 packet needs 1 token to leaveTokens added at rate r, max bNo token: wait / drop / markIdle → full bucket → burst of bAverage rate stays r⚠️ Exam Traps & Viva Favorites① What is a token?Permission to send one packet (or one byte).Tokens are generated at rate r and stored ina bucket of capacity b.② Maximum burst formula?Burst time S = b / (M - r), M = max rate.Data sent in the burst = b + r x S= M x S.③ Difference from leaky?Leaky bucket drains at a fixed rate. Tokenbucket lets idle flows save credit and thensend a burst at full speed.Tip: idle = save tokens = burst laterMaster Revision Table(learn it row by row!)ParameterSymbolMeaningEffectToken raterTokens added per secondLong-term average rateBucket capacitybMaximum stored tokensMaximum burst sizeMax output rateMLine / link speedLimits burst speedBurst timeSS = b / (M - r)Longer if r is close to MPacket cost1 tokenToken consumed per packetNo token: wait or dropIdle flowfullBucket fills up to bNext burst is allowedQUICK FACTSBurst: S = b / (M - r) · data sent in the burst = b + r x S = M x SExample: b = 250 KB, r = 2 MB/s, M = 10 MB/s → S = 0.25 / 8 s = 31.25 msLeaky bucket: no burst · Token bucket: burst up to b, average rate rVERIFIED★ Golden rule: tokens = credit; save it whileidle and spend it as a burst.

The Token Bucket Algorithm


1. Motivation: Overcoming Leaky Bucket Rigidity

The Leaky Bucket algorithm enforces a rigid constant output rate, dropping packets if a burst arrives even if the network was completely idle for the preceding hour.

The Token Bucket Algorithm resolves this by allowing controlled traffic bursts:

  • If a host is idle, it saves up "credits" in the form of Tokens.
  • When a burst of data arrives, the host can consume its accumulated tokens to transmit at the maximum physical link rate (MM) for a brief period before settling back to the sustained average rate (RR).

2. Operating Mechanism of Token Bucket

MermaidDiagram
Rendering diagram…

Operational Rules:

  1. Token Generation: Tokens are added to the bucket at a constant rate of RR tokens per second.
  2. Bucket Capacity (CC): The bucket has a finite capacity of CC tokens. If the bucket is full, newly generated tokens overflow and are discarded.
  3. Transmission Condition:
    • To transmit a packet of length bb bytes, the sender must remove bb tokens from the bucket.
    • If Tokens ≥b\geq b: The tokens are removed, and the packet is transmitted immediately at full link speed MM.
    • If Tokens <b< b: The packet must wait in a queue until sufficient tokens accumulate, or be dropped.

3. Mathematical Formula for Maximum Burst Duration

When a full bucket of capacity CC encounters a massive burst of incoming traffic, the transmitter empties the bucket at maximum rate MM while new tokens continue to flow into the bucket at sustained rate RR (M>RM > R).

Let TT be the maximum duration of the burst at rate MM:

Total Data Sent in Burst=M×T\text{Total Data Sent in Burst} = M \times T
Tokens Available=C+(R×T)\text{Tokens Available} = C + (R \times T)

Equating the two:

M⋅T=C+R⋅TM \cdot T = C + R \cdot T
M⋅T−R⋅T=CM \cdot T - R \cdot T = C
(M−R)⋅T=C(M - R) \cdot T = C
T=CM−R\mathbf{T = \frac{C}{M - R}}
  • Maximum Burst Duration (TT): The longest time the system can continuously transmit at peak link rate MM.
  • Total Burst Volume (Vmax⁡V_{\max}):
Vmax⁡=M×T=C⋅MM−RV_{\max} = M \times T = \frac{C \cdot M}{M - R}

4. Solved Numerical Example

Problem:

A token bucket regulator has a bucket capacity of C=2 MegabytesC = 2\text{ Megabytes} (16 Megabits16\text{ Megabits}) and a token generation rate of R=5 MbpsR = 5\text{ Mbps}. The maximum physical link transmission speed is M=25 MbpsM = 25\text{ Mbps}.

  1. Calculate the maximum burst duration (TT) at peak rate 25 Mbps25\text{ Mbps}.
  2. Calculate the total data volume transmitted during this peak burst.

Solution:

  • C=16 MegabitsC = 16\text{ Megabits}
  • R=5 MbpsR = 5\text{ Mbps}
  • M=25 MbpsM = 25\text{ Mbps}
  1. Maximum Burst Duration (TT):
T=CM−R=16 Megabits25 Mbps−5 Mbps=1620=0.8 secondsT = \frac{C}{M - R} = \frac{16\text{ Megabits}}{25\text{ Mbps} - 5\text{ Mbps}} = \frac{16}{20} = \mathbf{0.8\text{ seconds}}
  1. Total Burst Volume:
V=M×T=25 Mbps×0.8 s=20 Megabits=2.5 MegabytesV = M \times T = 25\text{ Mbps} \times 0.8\text{ s} = \mathbf{20\text{ Megabits} = 2.5\text{ Megabytes}}

Analysis: In 0.80.8 seconds, the bucket delivered 2.5 MB2.5\text{ MB} of data at peak speed: 2 MB2\text{ MB} came from accumulated stored tokens, and 0.5 MB0.5\text{ MB} (5 Mbps×0.8 s5\text{ Mbps} \times 0.8\text{ s}) came from new tokens generated during the burst itself!


5. Comprehensive Comparison: Leaky Bucket vs. Token Bucket

Parameter Leaky Bucket Algorithm Token Bucket Algorithm
Bucket Contents Stores actual Packets / Data Stores abstract Tokens (Credits)
Output Rate Strictly Constant (Fixed leak rate RR) Variable / Bursty (Up to maximum link rate MM)
Burst Handling Smooths bursts into a constant trickle Allows controlled bursts up to capacity CC
Token Generation None (Direct packet queue) Generated at fixed periodic rate RR
Idle Behavior Discards empty ticks Accumulates tokens up to capacity CC
Packet Dropping Drops packets when bucket buffer is full Drops tokens when full; delays or drops packets
Implementation FIFO Queue + Rate limiter Counter + Token Generator

Frequently Asked Questions

QWhat is the fundamental difference between Leaky Bucket and Token Bucket?

The Leaky Bucket stores packets and enforces a strict, constant output rate regardless of bursts or link capacity. The Token Bucket stores tokens and allows idle nodes to save tokens, enabling them to transmit legitimate bursts at peak link speed when traffic arrives.

QState the formula for maximum burst time in Token Bucket.

T=CM−RT = \frac{C}{M - R}, where CC is bucket token capacity, MM is maximum physical link rate, and RR is continuous token arrival rate.

QCan Token Bucket and Leaky Bucket be combined?

Yes. A common architecture places a Token Bucket before a Leaky Bucket: the Token Bucket allows controlled bursts into the node, and the Leaky Bucket caps the absolute peak rate sent into the core network.

QWhat happens to tokens when the Token Bucket is full?

Newly generated tokens overflow and are discarded, so an idle host can save credits only up to the bucket capacity CC.

Free AppAndroid App

Coursify Android App

Generate AI courses, take quick quizzes between classes, and master complex engineering concepts on the go.

← All Computer Networks modules