ArticleslgStudy

computer science

Token bucket

Token bucket is a computer science topic covered in the lgStudy science library. This page brings together a partial reference excerpt, illustrations, worked examples, real-world applications and a short study plan, so you can understand Token bucket rather than just read about it. In short: The token bucket is an algorithm used in packet-switched and telecommunications networks. It can be used to check that data transmissions, in the form of packets, conform to defined limits on bandwidth and burstiness (a measure of the unevenness or variations in the traffic flow).

Token bucket — main illustration
Token bucket — illustration

Key takeaways

  • Token bucket belongs to computer science; place it in that map before memorising details.
  • Learn the definition first, then one example that makes the definition concrete.
  • Connect Token bucket to a quantity you can measure, compute or draw — that is where exam questions come from.
  • Reproduce the core statement of Token bucket from memory before moving on to harder problems.

Reference excerpt

The token bucket is an algorithm used in packet-switched and telecommunications networks. It can be used to check that data transmissions, in the form of packets, conform to defined limits on bandwidth and burstiness (a measure of the unevenness or variations in the traffic flow). It can also be used as a scheduling algorithm to determine the timing of transmissions that will comply with the limits set for the bandwidth and burstiness: see network scheduler.

Overview The token bucket algorithm is based on an analogy of a fixed capacity bucket into which tokens, normally representing a unit of bytes or a single packet of predetermined size, are added at a fixed rate. When a packet is to be checked for conformance to the defined limits, the bucket is inspected to see if it contains sufficient tokens at that time. If so, the appropriate number of tokens, e.g. equivalent to the length of the packet in bytes, are removed ("cashed in"), and the packet is passed, e.g., for transmission. The packet does not conform if there are insufficient tokens in the bucket, and the contents of the bucket are not changed. Non-conformant packets can be treated in various ways:

They may be dropped. They may be enqueued for subsequent transmission when sufficient tokens have accumulated in the bucket. They may be transmitted, but marked as being non-conformant, possibly to be dropped subsequently if the network is overloaded. A conforming flow can thus contain traffic with an average rate up to the rate at which tokens are added to the bucket, and have a burstiness determined by the depth of the bucket. This burstiness may be expressed in terms of either a jitter tolerance, i.e. how much sooner a packet might conform (e.g. arrive or be transmitted) than would be expected from the limit on the average rate, or a burst tolerance or maximum burst size, i.e. how much more than the average level of traffic might conform in some finite period.

Algorithm The token bucket algorithm can be conceptually understood as follows:

A token is added to the bucket every 1 / r {\displaystyle 1/r} seconds. The bucket can hold at the most b {\displaystyle b} tokens. If a token arrives when the bucket is full, it is discarded. When a packet (network layer PDU) of n bytes arrives, if at least n tokens are in the bucket, n tokens are removed from the bucket, and the packet is sent to the network. if fewer than n tokens are available, no tokens are removed from the bucket, and the packet is considered to be non-conformant.

Variations Implementers of this algorithm on platforms lacking the clock resolution necessary to add a single token to the bucket every 1 / r {\displaystyle 1/r} seconds may want to consider an alternative formulation. Given the ability to update the token bucket every S milliseconds, the number of tokens to add every S milliseconds = ( r ∗ S ) / 1000 {\displaystyle (r*S)/1000} .

Properties

Average rate Over the long run the output of conformant packets is limited by the token rate, r {\displaystyle r} .

Burst size Let M {\displaystyle M} be the maximum possible transmission rate in bytes/second. Then T max = { b / ( M − r ) if r < M ∞ otherwise {\displaystyle T_{\text{max}}={\begin{cases}b/(M-r)&{\text{ if }}r<M\\\infty &{\text{ otherwise }}\end{cases}}} is the maximum burst time, that is the time for which the rate M {\displaystyle M} is fully utilized. The maximum burst size is thus B max = T max ∗ M {\displaystyle B_{\text{max}}=T_{\text{max}}*M}

Uses The token bucket can be used in either traffic shaping or traffic policing. In traffic policing, nonconforming packets may be discarded (dropped) or may be reduced in priority (for downstream traffic management functions to drop if there is congestion). In traffic shaping, packets are delayed until they conform. Traffic policing and traffic shaping are commonly used to protect the network against excess or excessively bursty traffic, see bandwidth management and congestion avoidance. Traffic shaping is commonly used in the network interfaces in hosts to prevent transmissions being discarded by traffic management functions in the network. The token bucket algorithm is also used in controlling database IO flow. In it, limitation applies to neither IOPS nor the bandwidth but rather to a linear combination of both. By defining tokens to be the normalized sum of IO request weight and its length, the algorithm makes sure that the time derivative of the aforementioned function stays below the needed threshold.

Comparison to leaky bucket

… excerpt ends here. Continue reading the full article.

Worked examples

Example 1 — a first encounter with Token bucket

Start with the simplest possible case. Write down what Token bucket claims or describes in one sentence, then invent the smallest concrete situation in which that sentence is true. In computer science, the smallest case is usually a single object, a single equation or a single measurement. Check that every symbol or term in your sentence has a meaning in that case.

Example 2 — changing one variable

Take the situation from Example 1 and change exactly one quantity: double it, halve it, or set it to zero. Predict what should happen to Token bucket before you calculate. Comparing your prediction with the result is the fastest way to find out whether you understand the idea or only the words.

Example 3 — an exam-style question

Typical questions about Token bucket ask you to (a) state it precisely, (b) apply it to given data, and (c) explain a limitation. Practise writing all three answers in under five minutes; the third part is what separates a full-mark answer from an average one.

Applications of Token bucket

In research
Token bucket appears in computer science research whenever the underlying quantities have to be modelled precisely. Papers usually cite it as a starting assumption and then explore where it breaks down.
In technology and industry
Engineering practice reuses Token bucket in design rules, simulations and safety margins. Knowing the idea lets you read a specification sheet and understand why the numbers look the way they do.
In the classroom
Token bucket is common in secondary-school and first-year university syllabi. It links to neighbouring topics Network performance, Network scheduling algorithms, so understanding it makes those chapters shorter.
In everyday life
Look for Token bucket outside the textbook — in sport, cooking, traffic, electronics or the sky above you. An example you found yourself is remembered far longer than one you were given.
Ask Teacher Smith questions about this articleOpens your AI tutor with a question about “Token bucket” →

Affiliate

Preply — study more efficiently by working with a personal tutor. 50% off.

How to study Token bucket in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what Token bucket means in your own words.
  3. Compare your version with the excerpt and mark what you missed.
  4. Work through the three examples above with pen and paper.
  5. Explain Token bucket out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is Token bucket in simple terms?

The token bucket is an algorithm used in packet-switched and telecommunications networks. It can be used to check that data transmissions, in the form of packets, conform to defined limits on bandwidth and burstiness (a measure of the unevenness or variations in the traffic flow).

Why does Token bucket matter?

Because it connects several computer science ideas at once: it gives you a definition you can apply, a quantity you can calculate, and a way to check whether a result is plausible.

How should I study Token bucket?

Read the excerpt, restate it from memory, then work through the examples and applications listed on this page. The five-step study plan above takes about twenty minutes.

What does this page cover?

It gives you a compact reference excerpt plus original lgStudy explanations, examples, applications and study material on Token bucket.

Tags

  • Network performance
  • Network scheduling algorithms

Keep exploring