1. Home
  2. Computer Networks
  3. Token Bucket (Traffic Shaping)

Token Bucket (Traffic Shaping)

Limit how fast a sender may transmit while still allowing short bursts. Tokens drip into a bucket and each packet spends one.

Interactive 3DIntermediate11 min readCNUpdated

Drag to rotate · Right-drag to pan · Click, then scroll to zoom · Space play · Left arrowRight arrow step

What's happening

A token bucket limits how fast a sender may transmit. Tokens arrive at a steady rate, a packet spends one token, and a full bucket lets short bursts through. Press Run.

Pseudocode

  1. tokens ← capacity // bucket starts full
  2. every tick:
  3. tokens ← min(capacity, tokens + rate) // refill
  4. for each packet that arrives:
  5. if tokens ≥ 1: tokens ← tokens − 1; send the packet
  6. else: drop (or queue) the packet
Read this model as text · 18 steps

One run of the model above, written out step by step. Useful with a screen reader, on a slow connection, or for quick revision. The values may differ from the run on your screen.

  1. A token bucket limits how fast a sender may transmit. Tokens arrive at a steady rate, a packet spends one token, and a full bucket lets short bursts through. Press Run.
  2. The bucket starts full with 5 tokens. Every tick it gains 1 token (up to 5). Each packet needs one token to be sent.
  3. Tick 1: refill 5 → 5 tokens (the bucket is full, extra tokens are lost). 4 packets arrive.
  4. 4 packets are sent (one token each). Tokens left: 1.
  5. Tick 2: refill 1 → 2 tokens. 4 packets arrive.
  6. 2 packets are sent (one token each), and 2 are dropped because the bucket ran empty. Tokens left: 0.
  7. Tick 3: refill 0 → 1 token. 0 packets arrive.
  8. Tick 4: refill 1 → 2 tokens. 0 packets arrive.
  9. Tick 5: refill 2 → 3 tokens. 1 packet arrives.
  10. 1 packet is sent (one token each). Tokens left: 2.
  11. Tick 6: refill 2 → 3 tokens. 0 packets arrive.
  12. Tick 7: refill 3 → 4 tokens. 5 packets arrive.
  13. 4 packets are sent (one token each), and 1 is dropped because the bucket ran empty. Tokens left: 0.
  14. Tick 8: refill 0 → 1 token. 0 packets arrive.
  15. Tick 9: refill 1 → 2 tokens. 0 packets arrive.
  16. Tick 10: refill 2 → 3 tokens. 1 packet arrives.
  17. 1 packet is sent (one token each). Tokens left: 2.
  18. After 10 ticks: 12 packets sent and 3 dropped. A full bucket allows a burst of up to 5 packets, but the long-run average can never exceed 1 per tick.

Try this in the 3D model

  • Run Bursty traffic with the default bucket (size 5, rate 1). At which tick are packets dropped, and why?
  • Increase the bucket size to 8 and run again. How many fewer packets are dropped?
  • Switch to Steady traffic. With rate 1, what is the average rate the bucket can sustain?
  • Run Overload with rate 3. Is anything dropped now?

Controlling the flow into the network

If every sender transmitted as fast as it liked, routers would overflow. Networks therefore shape or police traffic. The token bucket is the most widely used method, found in routers, API rate limiters and cloud gateways.

The algorithm

  • A bucket holds up to B tokens (the bucket size) and starts full.
  • Every tick, r new tokens are added. If the bucket is full the extra tokens are lost.
  • To send a packet, take one token out. If there is none, the packet is dropped (or queued).
tokens ← B
every tick:
    tokens ← min(B, tokens + r)
    for each arriving packet:
        if tokens ≥ 1:  tokens ← tokens − 1;  send
        else:            drop

Worked example

Bucket size 5, rate 1, arrivals per tick: 4, 4, 0, 0, 1, 0, 5, 0, 0, 1.

Tick Arrivals Sent Dropped Tokens left
1 4 4 0 1
2 4 2 2 0
3 0 0 0 1
4 0 0 0 2
5 1 1 0 2
6 0 0 0 3
7 5 4 1 0
8–10 0, 0, 1 1 0 2

Total: 12 sent, 3 dropped. The idle ticks 3–6 let the bucket refill, which paid for the burst at tick 7.

Token bucket vs leaky bucket

Token bucket Leaky bucket
Output Bursts allowed, up to the bucket size Constant rate
Idle time Saved as tokens Wasted
Typical use Rate limiting APIs and routers Smoothing traffic into a fixed pipe

Tuning

  • Rate r sets the long-run average the sender may use.
  • Size B sets how large a burst is tolerated. A big bucket is friendly to bursty applications but can hit the network with a large spike.

Compare this with TCP congestion control, where the sender slows itself down after losses instead of being limited by a router.

Code

class TokenBucket:
    def __init__(self, size, rate):
        self.size, self.rate, self.tokens = size, rate, size

    def tick(self):
        self.tokens = min(self.size, self.tokens + self.rate)

    def allow(self):
        if self.tokens >= 1:
            self.tokens -= 1
            return True
        return False

Common mistakes

  • Thinking the bucket holds packets. It holds permission tokens.
  • Forgetting the cap: tokens beyond the bucket size are discarded, so idle time cannot be saved forever.
  • Confusing the two buckets: only the leaky bucket forces a constant output rate.

Complexity at a glance

Case / operationTimeWhy
Per packetO(1)Check and decrement a counter.
Maximum burstbucket sizeA full bucket lets that many packets go at once.
Long-run average ratetoken rateNever more than the tokens added per tick.
Extra spaceO(1) — one counter and a timestamp

Quick check

Test yourself — pick an answer to see if you got it.

1. What happens to a packet when the bucket has no tokens (in the policing version)?

2. What does the bucket size control?

3. How is a token bucket different from a leaky bucket?

4. A bucket of size 5 is full and 7 packets arrive in one tick (refill rate 1). How many are sent?

Saved in this browser.
Spotted a mistake or a bug in the 3D model?

Report a mistake

in Token Bucket (Traffic Shaping). Thank you — every report makes the lesson better for the next reader.

We'll also include a link to the step of the 3D model you're on and your browser type, so we can reproduce it.