Token Bucket Explained: How Rate Limiting Allows Bursts

Token bucket explained in plain terms means understanding a simple way to control how fast requests arrive, by giving each request a token and only letting it through when a token is available. The central question is how a service can allow short bursts of activity without being overwhelmed by a flood, and the answer is a bucket that fills steadily, empties when requests arrive, and sets a firm limit on the total rate.

A token bucket is an algorithm used in rate limiting and traffic shaping. Tokens are added to a bucket at a fixed rate, each request must take a token before it is processed, and a request that finds the bucket empty is delayed or rejected. The bucket has a maximum size, so tokens cannot pile up without limit, and that size decides how large a burst the system will allow.

The idea is easy to picture: a bucket with a slow tap filling it and a hole at the bottom where tokens are spent. A steady stream of requests spends tokens as fast as they arrive, while a sudden burst spends the tokens saved up in the bucket, then must wait for the tap to refill it. This article explains what a token bucket is, how it works, and where it is used.

Key takeaways

  • A token bucket adds tokens at a fixed rate up to a maximum size.
  • Each request must take a token before it is processed.
  • A request that finds the bucket empty is delayed or rejected.
  • The bucket size decides how large a burst is allowed.
  • Token buckets are used for rate limiting and traffic shaping.

What a token bucket is

A token bucket is a counter with two rules: tokens arrive at a steady rate, and the bucket can hold only a set number of them. If the bucket is full, new tokens are discarded rather than stored, so a service that has been quiet cannot save up an unlimited allowance and then release a huge flood. The refill rate sets the long-term average speed, while the bucket size sets the short-term burst.

The algorithm is deliberately simple, which is why it is so widely used. It needs only a count of tokens, a timestamp for the last refill, and the two settings of rate and capacity. From those, a system can decide in a fraction of a millisecond whether a request may pass, making it cheap enough to run on every request to a busy service.

A token bucket is not a queue, and it does not sort or prioritise requests. It treats each request the same, spending one token per request, or more tokens for larger requests, and it says nothing about which requests matter most. What it does is enforce a fair, predictable limit on the total rate. Our guide to Nginx explains a server that often enforces such limits, and our guide to Redis explains a store often used to hold the bucket counters.

How a token bucket works

When a request arrives, the system first works out how many tokens should have been added since the last check, based on the time passed and the refill rate. It adds that number to the bucket, up to its maximum size, and records the current time. This lazy refill means the bucket does not need a background process adding tokens, since the count is updated only when a request arrives.

Next, the system checks whether the bucket holds enough tokens for the request. Most requests cost one token, though a large request, such as a big file upload, may cost several. If enough tokens are available, they are removed, the request is allowed through, and the remaining count is stored for the next arrival.

If the bucket is empty, or holds fewer tokens than the request needs, the request cannot proceed at once. The system then has two choices: reject the request immediately, usually with a message telling the caller to try again later, or delay it until enough tokens have arrived. Which choice fits depends on the service and on how patient its callers can be.

Bursts are the reason the bucket has a size at all. After a quiet period, the bucket is full, so a sudden group of requests can all take tokens and pass through at once, up to the bucket capacity. Once the saved tokens are spent, the rate falls back to the refill rate, so a burst is allowed but a sustained flood is not.

The two settings matter more than any other detail. A high refill rate with a small bucket allows a fast steady flow but almost no burst, while a low refill rate with a large bucket allows long quiet periods to be followed by a big burst. Teams choose the pair by looking at how their users actually behave and how much load the service can safely carry.

A dark desk with a glass jar of glowing green tokens beside a small tap dripping steadily
A dark desk with a glass jar of glowing tokens beside a steadily dripping tap shows how a token bucket works: tokens fill slowly, then are spent when requests arrive.

Comparing a token bucket with other rate limiting methods

A token bucket is one of several ways to limit the rate of requests, and each suits a different need. A fixed window counts requests in set periods, a sliding window smooths the edges between periods, and a leaky bucket releases work at a steady pace, but each has its own trade-offs.

The table below sets out the main contrasts between the leading approaches.

Aspect Token bucket Fixed window Leaky bucket
How it limits Tokens spent per request Count per set period Work released steadily
Bursts allowed Yes, up to bucket size Yes, at window edges No, output is smooth
Memory needed One counter per bucket One counter per window One queue per bucket
Fairness Fair over the long term Uneven at boundaries Very even output
Best for APIs with bursty traffic Simple per-minute limits Smooth downstream flow

The practical result is that a token bucket is the usual choice when bursts are normal and should be allowed, a fixed window suits the simplest limits, and a leaky bucket wins when the service downstream needs a perfectly smooth flow.

Where a token bucket is used

Application programming interfaces use token buckets more than any other setting. A public interface may allow a hundred requests per minute per key, with a bucket that lets a user send a quick burst of twenty and then settle to the steady rate. That keeps the service usable for real people while stopping a single user from taking more than a fair share.

Networks use token buckets for traffic shaping. An internet provider or a network device can allow a connection to send a burst of data, then hold it to an agreed average rate, which keeps one heavy user from crowding out everyone else on the same link. The same idea shapes traffic between data centres and inside cloud networks.

Web servers use token buckets to protect themselves from overload. When traffic spikes, the bucket slows the flow of requests to a rate the server can handle, rejecting or delaying the rest, rather than letting the server accept more work than it can finish and fail for everyone.

Messaging and notification systems use token buckets to pace outgoing messages. A service that sends emails or text messages may be limited by its provider to a set rate, and a bucket keeps it inside that limit, allowing a burst for an urgent alert while keeping the average flow steady.

Even everyday software uses the idea in simpler forms, such as a download manager that allows a short burst of speed before settling to a cap, or a game that lets a player act quickly a few times and then forces a pause. Wherever a resource is shared, a bucket is a fair way to share it.

Strengths and limits

The greatest strength of a token bucket is its flexibility. One simple mechanism gives both a long-term average rate and a short-term burst allowance, and two numbers, the refill rate and the bucket size, control both. That makes it easy to tune for different users, different plans, and different kinds of traffic.

It is also a strength that the algorithm is cheap and easy to share across machines. A single counter in a fast store can serve many servers, so a limit can be enforced across a whole system rather than per server, which stops a user from multiplying their allowance by spreading requests across servers.

The limits are real, though. A token bucket does not prioritise important requests over unimportant ones, it cannot tell a genuine burst from an attack on its own, and a shared counter can become a bottleneck if every request must check it. For those cases it needs to be combined with other controls, such as authentication, quotas, and monitoring.

Token bucket checklist

  • Set the refill rate to the average speed the service can sustain.
  • Set the bucket size to the largest burst you are willing to allow.
  • Use one bucket per user, key, or client, not one for everyone.
  • Return a clear message when a request is rejected for lack of tokens.
  • Include a retry-after hint so callers know when to try again.
  • Store shared buckets in a fast store all servers can reach.
  • Log rejected requests so abuse and misconfiguration can be spotted.
  • Test the limits with bursts, not only with steady traffic.
  • Review the rate and size as usage patterns change over time.

Frequently asked questions

What is a token bucket in simple terms?

It is a bucket that fills with tokens at a fixed rate, up to a maximum size. Each request must take a token to proceed, so the refill rate sets the average speed and the bucket size sets how large a burst is allowed.

How is a token bucket different from a fixed window?

A fixed window counts requests in set periods, such as a minute, and resets the count at each boundary, which can allow a burst at the end of one window and the start of the next. A token bucket refills continuously, so it has no sharp boundary and allows bursts only up to its size.

What is a token bucket used for?

It is used for rate limiting and traffic shaping, mainly for application programming interfaces, networks, and web servers. It keeps a shared service fair and available by stopping any one user from taking more than an agreed share.

What happens when the bucket is empty?

The request cannot proceed at once. The system either rejects it, usually with a message telling the caller to try again later, or delays it until enough tokens have arrived, depending on how the service is designed.

Can a token bucket allow bursts?

Yes, that is one of its main strengths. After a quiet period the bucket is full, so a sudden group of requests can pass through at once, up to the bucket capacity, before the rate falls back to the steady refill rate.

How do I choose the refill rate and bucket size?

Choose the refill rate from the average load the service can sustain, and the bucket size from the largest burst you are willing to allow. Look at real usage patterns, start with modest values, and adjust them as you learn how users actually behave.

The bottom line

Token bucket explained in one line is this: tokens arrive steadily, requests spend them, and the bucket size decides how big a burst may be.

It is a small algorithm with a wide reach, keeping shared services fair, steady, and available even when demand arrives in sudden waves.

This article is general information about how the token bucket algorithm works, not advice about setting rate limits for a particular service.

Latest articles

Scroll to Top