Techzone/Bucket Leaks at Constant Rate

Bucket Leaks at Constant Rate

3 min readArticle

The defining property of the leaky bucket algorithm: no matter how fast data arrives into the bucket (the input queue), it always exits at a fixed, constant rate. This is the "leak" — the output is metered, not driven by input volume.

The Core Property

shell
Input rate:   variable (bursty — 0 to line rate)
Output rate:  constant (fixed drain rate R)
Buffer:       finite queue of depth B bytes/packets

The bucket drains at exactly R packets (or bytes) per second regardless of how full it is. If the bucket is nearly empty, it still only outputs at rate R. If the bucket is overflowing, it still only outputs at rate R — excess is dropped.

Why This Matters

The constant drain rate is what smooths traffic. Consider an application that sends in bursts:

  • 1000 packets in 10ms (burst)
  • Nothing for 90ms
  • Repeat

Without shaping, this pattern causes downstream queues to overflow during the burst and starve during the gap. The leaky bucket absorbs the burst into the queue and releases it at a steady rate — downstream equipment sees smooth, predictable traffic.

Drain Rate Calculation

If the bucket drains at rate R (packets/second) and has depth B (packets):

shell
Max delay for a packet = B / R seconds
Average throughput = R (regardless of input rate, as long as input average <= R)
Burst handling = B packets worth before dropping

Example: R = 1000 pkt/s, B = 500 packets

  • A 500-packet burst is fully absorbed (takes 500ms to drain)
  • A 600-packet burst loses 100 packets (overflow) and takes 500ms to drain the rest
  • Output is always 1000 pkt/s while bucket has content

Constant Rate vs Token Bucket

The key contrast with token bucket: leaky bucket enforces a strict constant output rate. Token bucket allows bursts up to the token reservoir size.

Property Leaky Bucket Token Bucket
Output rate Always constant (R) Variable — up to token accumulation + R
Burst handling Buffers then smooths Allows bursts (tokens consumed)
Packet loss On overflow On token exhaustion
Application Traffic shaping/policing Rate limiting with burst tolerance

Linux Traffic Control Implementation

In Linux tc (traffic control), the tbf (token bucket filter) qdisc approximates leaky bucket behavior:

bash
# Set up a leaky-bucket-like rate limiter on eth0
# Rate: 1mbit, burst: 32kbit, latency max: 50ms
sudo tc qdisc add dev eth0 root tbf rate 1mbit burst 32kbit latency 50ms

# View the qdisc
tc qdisc show dev eth0

# Remove it
sudo tc qdisc del dev eth0 root

For strict constant-rate output (true leaky bucket), use netem with a rate limit:

bash
# Simulate strict constant output rate of 1Mbit/s with 10ms delay
sudo tc qdisc add dev eth0 root netem rate 1mbit delay 10ms

# Show stats
tc -s qdisc show dev eth0

Practical Observation

Watch a leaky bucket in action — induce a burst and observe the constant drain:

bash
# Generate burst traffic with iperf3
iperf3 -c <server> -b 100M -t 10   # burst to 100Mbps target

# Meanwhile watch the tc stats showing constant queue drain
watch -n 0.5 "tc -s qdisc show dev eth0"
# Look for 'sent' bytes increasing at constant rate despite burst input

Network Interface as Leaky Bucket

Every network interface is essentially a leaky bucket. The NIC transmit queue (txqueuelen) is the bucket:

bash
# Check transmit queue length
ip link show eth0
# -> qlen 1000   = 1000 packets

# Increase queue depth (larger bucket = more burst absorption)
sudo ip link set eth0 txqueuelen 2000

# View queue drops (bucket overflow counter)
ip -s link show eth0
# -> TX errors 0 dropped 47   <- dropped = overflow packets

Reference

  • RFC 2697 — A Single Rate Three Color Marker (leaky bucket based)
  • Linux tc/tbf documentation: https://lartc.org/manpages/tc-tbf.8.html
  • "Computer Networks" by Tanenbaum — Chapter 5 (Quality of Service)
techzonesite.comUnlock Your IT Potential