Implement a Leaky-Bucket Rate Limiter with Tests and Distributed Design
Context and Assumptions
This task requires implementing a leaky-bucket rate limiter. The implementation should be in a general-purpose language (Python 3 is used as a reference for clarity and unit tests).
The leaky bucket operates with the following semantics:
- A fixed drain rate
runits per second. - A finite capacity
bunits. - Each request consumes 1 unit by default, but the
try_acquiremethod should support consumingnunits. - The bucket "leaks" continuously at rate
r.
The limiter must support:
- A
try_acquire(n=1)method that returns a boolean indicating whether a request of sizenis allowed immediately. If not allowed, it should also return the wait time (in seconds) until it would be allowed. - Unit tests covering steady-state behavior (at or under the configured rate), burst behavior (up to capacity), and boundary/edge conditions (e.g., precision, zero/near-zero time deltas, exact thresholds).
- A discussion of concurrency safety for multi-threaded/multi-process environments.
- A design proposal for extending the limiter to a distributed setting (e.g., using Redis), including considerations for consistency and failure modes.
Task
- Implement the core leaky-bucket rate limiter logic, enforcing a maximum average request rate with a fixed drain rate.
- Develop unit tests to validate its behavior under:
- Steady-state conditions (at or under the configured rate).
- Burst conditions (up to the bucket's capacity).
- Boundary and edge cases (e.g., precision issues, very small time differences, exact threshold checks).
- Detail how to ensure the limiter is safe under concurrent access from multiple threads or processes.
- Propose a comprehensive design for a distributed version of this rate limiter, specifically addressing how to maintain consistency and handle various failure modes.
AI interview prep
Core dilemma
Balancing accurate, real-time rate limiting across a distributed system with high availability and low latency, while managing the state and synchronization overhead.
3 tradeoffs and 3 stress tests locked
Interview prep
Unlock the interviewer X-Ray rubric, tradeoff matrix, stress tests, clarifying questions, and key concepts.
Implement a Leaky-Bucket Rate Limiter with Tests and Distributed Design
Context and Assumptions
This task requires implementing a leaky-bucket rate limiter. The implementation should be in a general-purpose language (Python 3 is used as a reference for clarity and unit tests).
The leaky bucket operates with the following semantics:
- A fixed drain rate
runits per second. - A finite capacity
bunits. - Each request consumes 1 unit by default, but the
try_acquiremethod should support consumingnunits. - The bucket "leaks" continuously at rate
r.
The limiter must support:
- A
try_acquire(n=1)method that returns a boolean indicating whether a request of sizenis allowed immediately. If not allowed, it should also return the wait time (in seconds) until it would be allowed. - Unit tests covering steady-state behavior (at or under the configured rate), burst behavior (up to capacity), and boundary/edge conditions (e.g., precision, zero/near-zero time deltas, exact thresholds).
- A discussion of concurrency safety for multi-threaded/multi-process environments.
- A design proposal for extending the limiter to a distributed setting (e.g., using Redis), including considerations for consistency and failure modes.
Task
- Implement the core leaky-bucket rate limiter logic, enforcing a maximum average request rate with a fixed drain rate.
- Develop unit tests to validate its behavior under:
- Steady-state conditions (at or under the configured rate).
- Burst conditions (up to the bucket's capacity).
- Boundary and edge cases (e.g., precision issues, very small time differences, exact threshold checks).
- Detail how to ensure the limiter is safe under concurrent access from multiple threads or processes.
- Propose a comprehensive design for a distributed version of this rate limiter, specifically addressing how to maintain consistency and handle various failure modes.