A Rate Limiter controls and restricts the rate at which requests can access an object or resource. This limit is typically measured in units such as Requests Per Second (RPS) or Requests Per Minute (RPM). The primary purpose of using a Rate Limiter is to ensure resources remain available and to prevent Denial of Service (DoS) attacks. One popular algorithm for implementing Rate Limiting is the Leaky Bucket algorithm. In this repository, the Leaky Bucket algorithm is implemented using the Redis cache engine.
-
Learn more about leaky bucket here.
-
For more details about this implementation read my blog post here.
As an example, a simple Circuit Breaker has been implemented using the Leaky Bucket Rate Limiter. To understand how a Circuit Breaker works, read Martin Fowler's article here.
There are two examples: one demonstrates how to use a rate limiter to restrict the number of password recovery attempts for an email,
and the other shows how to implement a circuit breaker in an alarming system to prevent notification flooding.
Check examples.py.
uv sync
ruff format --check
ruff check
- Implementation of async version
- Implementation of other methods of rate limiting