Rate Limiting & Throttling
Protect APIs from abuse, DDoS bursts, and noisy neighbors using token buckets and Redis.
In plain words
Rate limiting caps how many requests someone can make in a period of time, like a turnstile that only lets so many people through per minute.
Think of it like…
A nightclub bouncer letting people in at a steady pace (tokens) and asking the overflow crowd to wait outside instead of packing the dance floor past fire code.
Where you’ve seen it
GitHub’s API allows 5,000 requests per hour per user. Go over that and you get a 429 "Too Many Requests" error until the limit resets.
Words you’ll hear
- Token bucket
- Tokens refill at a steady rate; each request spends one. Allows short bursts.
- Sliding window
- Counts requests over a rolling time window for smoother limits.
- HTTP 429
- The standard "Too Many Requests" response.
- Throttling
- Slowing requests down rather than rejecting them outright.
Visual diagram
Theory & deep dive
Why Rate Limiting belongs at the Edge/Gateway
Without rate limits, a single buggy customer script in a while(true) loop, a scraper bot, or a DDoS attack can exhaust all database connections and take down every customer.
Rate limiters reject excess requests early at the API Gateway with HTTP `429 Too Many Requests` and helpful headers (X-RateLimit-Remaining, Retry-After), keeping backends healthy for everyone else.
How it works, step by step
Step 1 of 41. Choose rate-limiting dimensions & placement
Enforce limits at the edge/API Gateway keyed by user_id, api_key, ip, or route before requests touch compute or databases.
- Status Code
- 429 Too Many Requests
- Keys
- IP / User / Tenant
When to use it & trade-offs
What it is
Rate limiting caps how many requests a user, IP, or tenant can send in a time window, rejecting excess traffic cleanly with HTTP 429 Too Many Requests.
Why it matters: A single buggy client loop, scraper, or credential-stuffing bot can saturate databases and spike cloud bills in seconds.
When to reach for it
- API monetization tiers (Free = 60 req/min, Pro = 1,000 req/min)
- Brute-force protection on login, OTP, and password-reset endpoints
- Tenant isolation in multi-tenant SaaS so one customer cannot starve others
When to avoid or be careful
- Using naive
GET countthenINCR countin two separate Redis calls (race condition under concurrency) - Using Fixed Window counters when exact burst limits matter at minute boundaries
Rate-Limiting Algorithms Compared
Strengths: Tiny state (2 numbers: tokens + last_refill); naturally smooths bursts.
Watch out: Requires tuning both burst capacity and refill rate.
Strengths: Outputs traffic at an exact steady drip with zero downstream bursts.
Watch out: Queue fills under heavy spikes, adding latency or dropping newest requests.
Strengths: Approximates exact sliding window using just two adjacent counters (~O(1) RAM).
Watch out: Slightly approximate across boundaries vs. storing every timestamp.
Interview signals & numbers to know
When this topic comes up
- prevent abuse
- brute force
- API quota
- noisy neighbor
- 429 Too Many Requests
Benchmarks & rules of thumb
- Redis Lua check RTT
- ~0.5 ms
- Token bucket memory/key
- ~16–64 bytes
- 10M active keys RAM
- <1 GB in Redis
- Standard response
- HTTP 429 + Retry-After
Common mistakes & follow-ups
Common interview mistakes
- Storing a full list of every request timestamp per user at 100k QPS (Sliding Window Log eats massive RAM)
- Rate limiting only by IP behind shared corporate/mobile NATs (blocks thousands of innocent users)
- Forgetting distributed race conditions across multiple gateway pods
Likely interviewer follow-ups
- Explain the boundary-burst flaw of Fixed Window Counters and how Sliding Window Counter fixes it.
- How do you make distributed Redis rate limiting atomic without a full lock?
- What should happen if the Redis rate-limiter cluster goes down - fail-open or fail-closed?
Knowledge check
Put it into practice
Design full architectures with AI feedback
Apply Rate Limiting & Throttling inside real High-Level Design prompts or review Q&A flashcards.