Leaky Bucket and Token Bucket Algorithms for Traffic Shaping and Rate Limiting
The Leaky Bucket and Token Bucket algorithms are widely used to control how data flows through networks. Both limit the average rate of sending/processing, but they differ in how they handle bursts.
At a high level:
- Leaky Bucket enforces a steady “drip” (constant release) with optional buffering.
- Token Bucket allows sending in bursts as long as tokens are available, while still limiting the long-term average rate.
A helpful mental model is:
- With leaky bucket, packets “wait in a bucket” that drains at a fixed rate.
- With token bucket, packets “spend tokens”; tokens regenerate at a fixed rate, enabling short-term bursts.
Key parameters you’ll see in both algorithms:
- : allowed average rate (e.g., tokens added per second)
- : bucket capacity (max queue size or max tokens)
- Time unit: discretized intervals or continuous time (implementation choice)
Token Bucket vs Leaky Bucket (Traffic Shaping Overview)
Core idea and state representation
Leaky bucket (queue + constant drain)
In leaky bucket, you conceptually maintain a buffer of capacity :
- When a packet/request arrives:
- If buffer is not full, it is queued.
- If buffer is full, the packet/request is dropped (or equivalently, not admitted).
- A “leak” process continuously removes data from the buffer at a fixed rate .
This makes the output rate approximately constant, which is why it’s popular for traffic shaping.
Token bucket (tokens + spending)
In token bucket, you maintain:
- A token counter with maximum capacity
- Tokens are added at rate up to
- When a packet/request arrives:
- If (or packet cost), it is allowed and tokens are consumed.
- Otherwise, it is delayed (wait for tokens) or rejected/dropped depending on policy.
This is why token bucket can allow bursts up to the bucket size.
Terminology keywords (for this section)
- Rate limiting
- Traffic shaping
- Burst tolerance
Leaky Bucket Algorithm (Admission + Constant Output)
- 1Step 1
Set average output rate and buffer capacity . Decide whether overflow causes drops or blocking.
- 2Step 2
Start with an empty buffer (queue length ).
- 3Step 3
If , enqueue the packet and set (or add packet size if variable). If , drop the packet.
- 4Step 4
Every time interval (or continuously), remove from the queue at rate : if , dequeue and set according to the drain schedule.
- 5Step 5
The departure process is smoothed: long-term average outflow approaches , and instantaneous output is limited by the leak rate and available buffered data.
Token Bucket Algorithm (Token Generation + Spend to Transmit)
- 1Step 1
Set token generation rate and maximum tokens . Decide whether you ‘delay’ or ‘drop’ when tokens are insufficient.
- 2Step 2
Start with (common for immediate burst) or (strict warm-up), depending on policy.
- 3Step 3
Between arrivals, increase by , but clamp to : .
- 4Step 4
Let each packet require tokens (often ). If , admit and consume tokens . Otherwise, either delay until tokens accumulate or drop/reject.
- 5Step 5
If arrivals come in bursts, the bucket allows them as long as cumulative tokens permit. The long-term average send rate cannot exceed .
Side-by-side behavior (burst handling and smoothing)
| Aspect | Leaky Bucket | Token Bucket |
|---|---|---|
| Long-term average rate | Enforced by constant drain at | Enforced by token generation at |
| Burst handling | Limited by buffer then constant leak; burst is “smoothed” | Naturally allows bursts up to token capacity |
| Queueing vs admission | Typically queues up to , drops on overflow | Can admit immediately when tokens exist; else delay or drop |
| Output shape | More constant/regular departure rate | More bursty (but bounded by tokens) |
| Parameters meaning | is max queued backlog; is leak rate | is max token storage; is refill rate |
Practical interpretation
- If your goal is strictly smooth output, leaky bucket is a strong fit.
- If you want to permit occasional bursts while still capping the long-term rate, token bucket is typically preferred.
Keywords for this section
- Bucket capacity
- Token refill rate
- Drain rate
Implementation detail: discrete vs continuous time
In real systems you’ll implement token generation/draining with a timer or on each request. Either way, ensure you cap at and compute available tokens/queue correctly from elapsed time.
Overflow policy changes the semantics
For leaky bucket, overflow usually drops or blocks. For token bucket, insufficient tokens can cause delay or rejection. Pick the policy to match your SLA and client behavior.
When to Choose Each Algorithm
Use Leaky Bucket
Constant-rate shaping goalPrefer stable departure spacing to reduce jitter and smooth downstream load."
Use Token Bucket
Burst-tolerant admission goalPrefer immediate allowance for short bursts while controlling long-term average rate."
Combine with per-flow buckets
Need bounded fairness under loadApply buckets per user/flow to avoid one source consuming all capacity."
Select drop vs delay
Operational constraintsDelay increases latency; drop increases loss. Choose based on requirements."
Variable packet sizes (important extension)
Both algorithms can be generalized from “1 unit per packet” to packet sizes/costs:
- In token bucket, each packet costs tokens proportional to its size.
- In leaky bucket, each packet adds units to backlog, and draining removes tokens/units at rate .
Then:
- Token check becomes: admit if
- Leaky bucket overflow becomes: queue if , else drop
This is common in routers and traffic engineering where “packet weight” matters.
Mathematical framing (conceptual)
Let token count evolve as:
Admission with cost occurs only if .
(Implementation usually discretizes the integral using elapsed time.)
Common FAQs and edge cases
Behavior Comparison Summary
Qualitative scoring: higher means more of that property in typical implementations.
Leaky vs Token Bucket—Self Check
Knowledge Check
In a leaky bucket algorithm, what primarily determines the departure (output) rate when the bucket is not empty?
Explore Related Topics
Cache Memory: Why FIFO and LRU Are Replacement Algorithms
Pure ALOHA vs Slotted ALOHA: Differentiation, Throughput, and Worked Examples
The Banker's Algorithm: Deadlock Avoidance in Operating Systems
The Banker's Algorithm is a deadlock‑avoidance method that keeps a system in a safe state by checking each resource request against the maximum declared needs of processes.
- Maintains Available, Max, Allocation, and Need matrices, where .
- The Safety Algorithm uses vectors Work and Finish to find an execution order; if all processes finish, the state is safe.
- The Resource‑Request Algorithm simulates allocation, runs the safety check, and commits only if the resulting state remains safe.
- Time complexity of the safety check is .
- In practice the algorithm is rarely used because processes must predeclare maximum needs and the algorithm’s overhead is high.