DEV Community

Arun Sai Veerisetty
Arun Sai Veerisetty

Posted on

# How to Throttle Like a Pro: 5 Rate Limiting Patterns in Python You Should Know ๐Ÿšฆ๐Ÿ

In todayโ€™s world of high-scale APIs, bots, and distributed systems, rate limiting is not just a nice-to-haveโ€”itโ€™s essential. Whether you're protecting your server from abuse or controlling how often a user can take action, rate limiting is the key to reliability and fairness.

In this blog, weโ€™ll explore 5 powerful rate limiting patterns with hands-on Python implementations. By the end, youโ€™ll not only understand when and why to use each pattern but also walk away with real code to apply in your own projects.


๐Ÿง  What is Rate Limiting?

Rate limiting is the process of restricting how many requests or actions a system allows over a period of time. For example, โ€œNo more than 5 login attempts per minuteโ€ or โ€œOnly 100 API calls per hourโ€.

This is crucial for:

  • Avoiding abuse or spam.
  • Managing traffic spikes.
  • Fair resource usage.
  • Avoiding overloads and DDoS attacks.

๐Ÿงฉ Overview of Patterns

Hereโ€™s a quick glance at the patterns we'll cover:

Pattern Allows Bursts? Description Best For
Fixed Window โŒ Simple time window Basic rate limiting
Sliding Window โŒ Fairer than fixed window API fairness
Leaky Bucket โœ… (Smooth) Queues excess traffic Traffic shaping
Token Bucket โœ… Token-based burst tolerance Most flexible rate limits
Distributed (Redis) โœ… Multi-server rate limiting Scalable systems

1. ๐ŸชŸ Fixed Window

Concept: Allow N actions per fixed time window (e.g., per minute).

Analogy: Like a parking garage that resets at midnight โ€” doesnโ€™t matter when you arrived, just how many came during the time.

Pros: Simple to implement.

Cons: Susceptible to bursts at window edges.


2. ๐ŸชŸ Sliding Window

Concept: Records timestamps of requests and checks the rolling window.

Analogy: Like keeping a log of visitors for the last 60 seconds โ€” fair and accurate.

Pros: Fairer than fixed window.

Cons: Slightly more complex.


3. ๐Ÿชฃ Leaky Bucket

Concept: Adds requests to a queue, and processes them at a fixed rate.

Analogy: Like a faucet dripping water at a steady rate, even if you pour a bucket into it.

Pros: Smoothens traffic.

Cons: Can introduce latency.


4. ๐ŸŽŸ๏ธ Token Bucket

Concept: Tokens are added at a fixed rate; each request consumes a token.

Analogy: Like a vending machine that refills slowly โ€” if you have tokens, you can burst; otherwise, wait.

Pros: Flexible and burst-tolerant.

Cons: Requires token logic and state.


5. ๐ŸŒ Distributed Rate Limiting (with Redis)

Concept: Use a shared data store like Redis to manage limits across servers.

Analogy: Like a shared notebook in the cloud tracking user activity.

Pros: Scalable, central tracking.

Cons: Needs external Redis setup.

๐Ÿ› ๏ธ Make sure Redis is running locally or remotely before testing.


๐Ÿ’ป GitHub Project

Explore all these patterns in code here:

๐Ÿ‘‰ GitHub Repo

Each rate limiter is implemented in Python with comments and test files to help you understand and experiment.


๐Ÿ”ง How to Run the Code

  1. Clone the repo
git clone https://github.com/arunsaiv/rate-limiter-patterns.git
cd rate-limiter-patterns
Enter fullscreen mode Exit fullscreen mode

2. install dependencies

pip install -r requirements.txt
Enter fullscreen mode Exit fullscreen mode

3. Run any pattern script

python fixed_window.py
python token_bucket.py
Enter fullscreen mode Exit fullscreen mode

๐Ÿ™Œ Like What You Read?

If this helped you:

  • ๐Ÿ’ฌ Leave a comment
  • ๐Ÿ” Share it with your network
  • ๐ŸŒŸ Star the GitHub repo!

Let's connect on LinkedIn!


Top comments (0)