25. Rate Limiting
1
The picture to keep in your head2
25.1 Why limit?3
25.2 What are the algorithms?4
25.3 How do the algorithms compare?5
25.4 How does distributed rate limiting with Redis work?6
25.5 Where should the limiter go?7
25.6 What do 429 and rate-limit headers look like?8
25.7 How do you handle bots and abuse?9
Memory card and where the analogy breaks10
Practice: five levels11
Interview questions12
Solutions13
Sources14
Code lab25. Rate Limiting
Practice: five levels
3 min read+10 XP
Assignments
Five levels. Do them in order. Answers are in "Solutions" at the end, so try first.
Level 1: Think (no code)
A. Which algorithm? One line each.
- A public API where a page load fires 20 calls at once, then goes quiet.
- Calls to an old inventory system that falls over above 50 requests a second, even briefly.
- "5 login attempts per 15 minutes per account", exact.
- A daily quota of 10,000 calls per API key.
- High-volume per-key limits where fixed windows let through too much at boundaries.
B. Which key (IP, account, API key, phone number, or a combination)? One line each.
- Stopping credential stuffing against one account from many IPs.
- Free vs paid plans for partner apps.
- Stopping a script from requesting 1,000 OTPs to one phone number.
- Blunting a flood from a few IP addresses before it reaches your servers.
Level 2: Calculate
- A token bucket holds 100 tokens and refills at 100 per minute. How often does a token arrive? After it's emptied, how long until it's full again?
- With a fixed window of 100 per minute, what's the most requests a client can get through in one second, and how?
- A sliding log keeps one 8-byte timestamp per request. With 1 million API keys at 100 per minute, how much memory for timestamps? And for a sliding window counter with two 8-byte counters per key?
- The API handles 1 million requests a minute, each needing one Redis operation for rate limiting. How many Redis operations a second?
- One client sends 100 requests a minute across 10 servers that each enforce a local limit of 10. The load balancer sends it 30, 25, 20, 10, 5, 4, 3, 2, 1 and 0 requests. How many get through?
- An attacker makes 10,000 login guesses against one account from 1,000 IPs. How many get through a per-IP limit of 10? A per-account limit of 5?
Level 3: Build
In Python:
- A
TokenBucket(capacity, refill_per_s)withallow(now, cost=1), storing only tokens and last time. - A
LeakyBucket(capacity, leak_per_s)withoffer(now)returning the delay orNone. FixedWindow,SlidingLogandSlidingWindowCounterwithallow(now), and aboundary_bursttest that shows fixed window allowing 200 while the others allow 100.- A fake Redis and two shared-counter functions: read-then-write, and atomic
INCR. Show the race. headers(limit, used, reset_in)returningRateLimit-*headers, withRetry-Afterwhen exhausted.
Level 4: Debug
During a partner's traffic spike, monitoring shows one API key making about 160 requests a minute against a limit of 100. The limiter, running on 20 API servers:
python
- Explain exactly how the key gets past its limit.
- Fix it. Then name the other weakness this limiter still has, even after the fix.
Level 5: Design
Design rate limiting for the bookstore: a public partner API (free plan 100 a minute, paid 1,000 a minute, with bursts allowed), a login endpoint, an OTP endpoint, and the search endpoint (expensive). Traffic: about 1 million API requests a minute across 30 servers.
Deliver:
- The algorithm and key for each limit, with numbers.
- Where each limit is enforced (edge, gateway, service).
- How the shared state works, and what happens if Redis is unavailable.
- What clients see when limited (status, headers, body).
- How you'd spot and handle a credential-stuffing attack.