Rate limiting with Redis: An essential guide
- January 13, 2025
- 3337 Unique Views
- 5 min read

Why Redis for Rate Limiting?
Redis has become a go-to tool for implementing rate limiters, and for good reason. It’s fast, reliable, and packed with features like atomic operations, data persistence, and Lua scripting. Just ask GitHub. When they migrated to a Redis-backed solution with client-side sharding, they solved tough challenges like replication, consistency, and scalability while ensuring reliable behavior across their infrastructure. So, why Redis? Its speed, versatility, and built-in capabilities make it perfect for handling distributed traffic patterns. But what’s even more important is how you use it. Let’s break down the most common rate-limiting patterns you can implement with Redis and what each one brings to the table.Popular Rate-Limiting Patterns
Choosing the right rate-limiting algorithm can be challenging. Here’s a breakdown of the most popular options, when to use them, and their trade-offs, with practical examples to help you decide:Leaky Bucket
How It Works: Imagine a bucket with a small hole at the bottom. Requests (water) are added to the bucket and processed at a steady “drip” rate, preventing sudden floods.
Token Bucket
How It Works: Tokens are generated at a fixed rate and stored in a bucket. Each request consumes a token, allowing for short bursts as long as tokens are available.
Fixed Window Counter
How It Works: Tracks the number of requests in fixed intervals (e.g., 1 minute). Once the limit is reached, all subsequent requests in that window are denied.
Sliding Window Log
How It Works: Maintains a log of timestamps for each request and calculates limits based on a rolling time window.
Sliding Window Counter
How It Works: Divides the time window into smaller intervals (e.g., 10-second buckets) and aggregates request counts to approximate a rolling window.
Choosing the Right Tool for the Job
Selecting a rate-limiting strategy isn’t just about matching patterns to scenarios; it’s about understanding the trade-offs and the specific needs of your application. Here’s how to make a more informed choice:Understand Your Traffic Patterns
- Predictable Traffic: If your API serves consistent request rates (e.g., hourly status checks or regular polling), Leaky Bucket is excellent for maintaining a steady flow.
- Burst Traffic: If you expect short bursts of traffic, such as during product launches or login spikes, Token Bucket allows controlled bursts while enforcing limits.
- Mixed Traffic: APIs with unpredictable traffic may benefit from Sliding Window Counter, which balances accuracy and resource usage.
Assess the Level of Precision Needed
- High Precision: If exact limits are critical (e.g., financial transactions or fraud detection), Sliding Window Log provides the most accurate enforcement by logging every request.
- Approximation is Okay: For most APIs, Sliding Window Counter strikes a balance between precision and efficiency, as it uses aggregated data instead of tracking every request.
Consider Resource Constraints
- Memory and CPU Overhead: Algorithms like Sliding Window Log can become resource-intensive at scale, especially with millions of users. For a lightweight alternative, Fixed Window Counter is simple but effective for low-traffic APIs.
- Scalability: Redis makes scaling rate limiting easier with atomic operations, Lua scripting, and replication features, but your choice of algorithm still affects performance. For instance, Token Bucket is computationally cheaper than Sliding Window Log in most distributed systems.
Account User Experience
- User Tolerance for Errors: Fixed-window approaches like Fixed Window Counter may frustrate users due to rigid resets. Sliding-window methods smooth out these boundaries, leading to a better user experience.
- Handling Edge Cases: Algorithms like Token Bucket allow some flexibility for bursts, which can help avoid unnecessary rate-limit errors during legitimate usage spikes.
Implementations for each type of rate limiter with Java & Redis will be released weekly! Stay tuned!
Stay curious!
Related Articles:Don’t Forget to Share This Post!
Comments (4)
Vovkin
2 months agoNice article with summary of the most popular rate limiting algorithms. Looks like you used same image for sliding window counter as for sliding window log.
Raphael De Lio
2 months agoThank you for letting me know and thank you for reading! I've replaced the correct animation :)
Anas
2 months agoNice article. I have a very little note. The article title says "With Redis" although the article is focusing more on the different rate limiting patterns. I have no idea whether you are planning to post a second part that talk about implementation with Redis or not. If you are planning to do that, I suggest to add a statement at the end of the article to inform the reader to wait for the second part for specific implementation with Redis.
Raphael De Lio
2 months agoHey, Anas! Thanks for the suggestion, that's indeed the case. I'll add a note informing that the following parts will be coming out weekly.