Skip to main content

Welcome to a Grasp lesson

Create a free account to keep studying and create your own study path.

Lesson 10

Understanding the Token Bucket Algorithm

Recommended resources

Hello! Welcome back to our course on building a production-ready rate limiter.

In our last two lessons, we explored counter-based algorithms. We started with the simple Fixed Window Counter and saw its major flaw—the boundary burst problem. Then, we moved to the more robust Sliding Window Counter, which solves that problem by using a rolling time window, giving us a more accurate view of the recent request rate.

Today, we're going to explore a completely different and widely-used approach. Instead of counting requests after they happen, we'll look at an algorithm that manages a pre-allocated budget of requests.

Your learning outcome for this lesson:

By the end of this lesson, you will be able to describe the token bucket algorithm and its two key parameters: capacity and refill rate.



1. A New Paradigm: From Counting to Budgeting

The core idea of the Token Bucket algorithm is intuitive and powerful. Imagine that for each client you want to rate limit, you maintain a bucket.

  • This bucket has a fixed capacity.
  • Tokens are added to the bucket at a steady refill rate.
  • Each incoming request from the client must take one token from the bucket to be processed.
  • If the bucket is empty, there are no tokens to take, and the request is denied.

This simple analogy elegantly controls the rate of requests. The long-term rate is governed by how quickly tokens are replenished, while the bucket's capacity allows for short, intense bursts of traffic.

Token Bucket Algorithm Illustration
This diagram illustrates the core concept of the Token Bucket algorithm. The system adds tokens to a bucket at a constant rate. Each request from a client consumes one token. If the bucket is empty, the request is rejected.

To see this concept animated, let's watch a short, clear explanation.

Rate Limiter System Design: Token Bucket, Leaky Bucket, Scaling

The video 'Rate Limiter System Design' by ByteByteGo provides a great introduction to the Token Bucket algorithm and how it contrasts with the fixed window approach.

Watch from 02:59 to 04:03. This segment will introduce the token bucket analogy and explain how accumulating tokens during quiet periods helps manage traffic bursts.



2. The Two Critical Parameters: Capacity and Refill Rate

The behavior of the token bucket algorithm is controlled entirely by two parameters. Understanding them is key to configuring your rate limiter correctly.

  1. Bucket Capacity (Burst Capacity): This is the maximum number of tokens the bucket can hold. It directly determines the maximum burst size you will permit. If a client has a full bucket of 100 tokens, they can make 100 requests as fast as the network allows. After that, they are limited by the refill rate. This parameter is crucial for allowing legitimate, bursty traffic patterns without throttling them unnecessarily.

  2. Refill Rate (Sustained Throughput): This is the rate at which tokens are added to the bucket (e.g., 10 tokens per second). This parameter determines the long-term, average throughput the client is allowed. Even if a client has a large capacity for bursts, over a longer period, their average request rate cannot exceed the refill rate.

Let's watch the next segment of the video, which explicitly defines these parameters.

Rate Limiter System Design: Token Bucket, Leaky Bucket, Scaling

The same video from ByteByteGo clearly breaks down these two parameters.

Watch from 04:03 to 04:30. Pay attention to how the example (capacity 100, refill rate 100/min) defines both the allowed burst and the long-term rate.

For a solid textual definition, this article provides a concise summary.

Design a Distributed Rate Limiter

The article 'Design a Distributed Rate Limiter' from Hello Interview offers a clear, written explanation of the Token Bucket algorithm and its parameters.

Read the section titled 'Token Bucket'. It provides a crisp definition of burst capacity and refill rate and explains how they work together.



3. How It Works: The "Lazy Refill" Implementation

You might be picturing a background process constantly adding tokens to every user's bucket. This would be highly inefficient. In practice, the Token Bucket algorithm is implemented using a "lazy refill" strategy.

Instead of actively refilling, the system stores just two pieces of information for each client:

  • tokens: The number of tokens in the bucket.
  • last_refill_timestamp: The time when the bucket was last accessed.

When a new request arrives:

  1. The system retrieves the current tokens and last_refill_timestamp.
  2. It calculates the time elapsed since the last refill.
  3. It computes how many new tokens should have been added during this elapsed time based on the refill_rate.
  4. It adds these new tokens to the current tokens count, ensuring the total does not exceed the bucket_capacity.
  5. It updates the last_refill_timestamp to the current time.
  6. Finally, it checks if there is at least one token available. If yes, it decrements the token count and allows the request. If no, it rejects the request.

This approach is highly efficient as it only performs a calculation when a request actually occurs.

Let's look at another video that animates this process and an article that breaks down the steps.

Design Rate Limiter (LLD) - Token Bucket, Fixed & Sliding Window with Thread Safety

This video from Shubh Patel provides another excellent animation of the token bucket in action, including the refill process.

Watch the segment from 15:54 to 18:56. Focus on how tokens are consumed by requests and then periodically refilled over time.

Token Bucket Rate Limiter (Redis & Java)

For a clear, step-by-step breakdown of the logic, the article 'Token Bucket Rate Limiter' by Raphael De Lio is a great resource. It outlines exactly how the check-and-refill process works.

Read the section 'How It Works'. It clearly lists the four steps: defining the refill rate, tracking consumption, refilling, and checking the limit. This directly maps to the lazy refill logic we just discussed.


Key Takeaways

  • The Token Bucket algorithm uses a "budgeting" approach rather than a "counting" one.
  • It is defined by two parameters:
    • Capacity: Controls the maximum allowed burst of requests.
    • Refill Rate: Controls the sustained, long-term average request rate.
  • It naturally handles bursty traffic by allowing idle clients to accumulate tokens up to the bucket's capacity.
  • It is memory-efficient, requiring only two values to be stored per client: the token count and the last refill timestamp.
  • The time-based refill mechanism means it is not susceptible to the boundary-edge problems seen in fixed-window algorithms.

Next Steps

We have now explored the three core rate-limiting algorithms you set out to build: Fixed Window Counter, Sliding Window Counter, and Token Bucket. Each has a distinct approach with its own strengths and weaknesses.

In the next lesson, we will directly compare these three algorithms. We'll analyze their trade-offs in terms of performance, memory usage, accuracy, and flexibility, so you can make an informed decision about which algorithm to use in different scenarios for your rate limiter project.