Design a Distributed Rate Limiter
OracleBackend Engineer · OnsiteLast updated
This problem tests the ability to design a scalable, low-latency distributed system for enforcing traffic quotas across multiple geographic regions with considerations for consistency and burst handling.
Problem Statement
Design a distributed rate limiter for a multi-tenant SaaS platform. Each tenant has a defined request-per-second (RPS) quota, and the system must ensure these limits are enforced across multiple data centers.
Functional Requirements
- Enforce rate limits per tenant (identified by
tenant_id). - Support 'burst' traffic (allowing temporary spikes above the sustained RPS).
- Support dynamic policy updates (e.g., changing the limit for a tenant without service restart).
Non-Functional Requirements
- Low latency: The decision to allow or block a request must add negligible overhead (sub-10ms).
- High availability: If the rate limiter service is unreachable, the system should fail-open or fail-closed based on configuration.
- Accuracy vs. Performance: Discuss the trade-offs between local-node caching and centralized state storage.
Submission Guidelines
- Describe the high-level architecture (e.g., sidecars vs. centralized service).
- Specify the storage mechanism for tracking request counts.
- Explain how you would handle 'hot keys' (e.g., a single large tenant consuming all global capacity).
- Provide an API contract for the rate limiter service.
Hints
Hint 1
Consider using the Token Bucket or Leaky Bucket algorithm to handle burstiness requirements effectively.
Hint 2
For multi-region consistency, evaluate whether you need strong consistency (using a global coordinator) or if eventual consistency (with atomic counters) is sufficient.
Hint 3
Think about using a distributed cache like Redis to store counters, and consider partitioning the keys to mitigate hot-key issues.
Follow-up questions
- How would you implement a 'sliding window log' algorithm instead of a fixed window, and what are the memory trade-offs?
- If the centralized database experiences a network partition, how would you ensure the system remains functional while maintaining overall quota compliance?
- How would you integrate a feedback loop that automatically throttles specific API endpoints if the backend services report high latency or error rates?
Solution walkthrough is for members
Get the full walkthrough with reference code and complexity analysis, plus Kitty Coach at 300 messages a day. Hints, the code runner and community answers stay free.