Design an API Rate Limiter

Quick Overview

Design a distributed rate limiting system. Compare token bucket, sliding window, and fixed window algorithms. Cover distributed coordination, race conditions, and how to handle rate limiting across multiple data centers.

Meta
System Design
Software Engineer
Meta
Software Engineer
System Design Round
System Design
Medium

0


Design a distributed rate limiting system. Compare token bucket, sliding window, and fixed window algorithms. Cover distributed coordination, race conditions, and how to handle rate limiting across multiple data centers.

Meta uses rate limiter design to see whether a candidate can protect shared APIs such as Graph API endpoints from abusive clients without adding real latency. Interviewers look for a reasoned choice among token bucket, fixed window and sliding window, plus a plan for atomic counters and multi-region enforcement.

What the Interviewer Expects
  • Compare token bucket, fixed window and sliding window on burst handling and memory
  • Place the limiter (gateway or sidecar) and state a latency budget for the check
  • Keep counters in a shared store with atomic updates (Lua script or INCR with TTL)
  • Explain race conditions and how atomic operations remove them
  • Discuss multi-data-center limits: local budgets, async sync, accepted overshoot
  • Define fail-open vs fail-closed behavior when the counter store is down
Key Topics to Cover
Token bucket
Sliding window log and counter
Redis atomic operations
Distributed counters
Multi-region consistency
API gateway
How to Approach This
  1. Start by clarifying functional and non-functional requirements with the interviewer.
  2. Estimate the scale: QPS, storage, bandwidth. This drives your design decisions.
  3. Draw a high-level architecture first, then deep dive into 1-2 critical components.
  4. Discuss trade-offs explicitly (e.g., consistency vs availability, SQL vs NoSQL).
  5. Address failure scenarios, monitoring, and how the system handles 10x traffic spikes.
Possible Follow-up Questions
  • How do you enforce one global limit for a client whose traffic hits three regions?
  • What would you change to give premium API keys higher burst allowances?
  • How do you tell clients when to retry (headers, status codes)?
  • How would you limit by cost per request rather than by request count?
Sharpen Your Skills on Codemia

Practice similar problems with our interactive workspace, get AI feedback, and track your progress.

Practice System Design Problems
Sample Answer
Requirements

Functional Requirements

  1. Rate Limiting: The API should limit the number of requests a user can make within a specified time period (e.g., 100 requests per minute).
  2. User Identification...
Capacity Estimation

Assuming Meta has approximately 2 billion active users and an average of 5 API calls per user per day:

  1. Total API Calls:
    • Daily: 2 billion users * 5 calls/user = 10 billion calls/day
    • ...

Submit Your Answer
Markdown supported