Rate Limiting and Throttling
Rate limiting caps how many requests a client can make in a time window. It protects login endpoints from brute force, keeps one customer from hurting others, and controls costs. Interviewers often ask you to design a distributed rate limiter.
What you will learn
- Compare fixed window, sliding window, token bucket and leaky bucket
- Implement a Redis-backed limiter shared by all instances
- Choose keys: IP, user, API key, route
- Return 429 with helpful headers
Key concepts
Fixed window
Count requests per minute bucket. Simple, but a client can send double the limit across the boundary (end of one minute + start of the next).
Sliding window
Weights the previous window by how much of it overlaps, or keeps timestamps in a sorted set. Smooth and accurate.
Token bucket
Tokens refill at a steady rate up to a capacity; each request takes one. Allows short bursts but limits the average. Used by most API gateways.
Distributed limit
With many app instances the counter must live in shared storage (Redis) and be updated atomically (INCR with EXPIRE, or a Lua script).
Step by step
Choosing the key and limit
- Login: per account and per IP (5 attempts per 15 minutes)
- Public API: per API key, by plan
- Expensive routes (search, AI): a stricter separate limit
- Behind a proxy, read the real IP correctly (trust proxy) or you limit the load balancer
Examples
Fixed-window limiter with Redis
export const rateLimit = ({ limit, windowSec, key }) => async (req, res, next) => {
const bucket = Math.floor(Date.now() / 1000 / windowSec)
const k = 'rl:' + key(req) + ':' + bucket
const [[, count]] = await redis.multi().incr(k).expire(k, windowSec).exec()
res.set('RateLimit-Limit', String(limit))
res.set('RateLimit-Remaining', String(Math.max(0, limit - count)))
if (count > limit) {
res.set('Retry-After', String(windowSec))
return res.status(429).json({ error: 'Too many requests' })
}
next()
}
app.post('/login', rateLimit({ limit: 5, windowSec: 900, key: (req) => req.ip + ':' + req.body.email }), login)Practise it
Try these without looking back. If you can explain each one out loud, you are ready for this question.
- Implement a token bucket in a Redis Lua script.
- Design rate limits for a free plan (100 per hour) and a paid plan (10,000 per hour).
Interview questions
What interviewers ask next on this topic. Answer out loud first, then open the model answer.
Where should rate limiting happen?
In layers: the CDN or WAF for coarse IP floods, the API gateway for per-key quotas, and the app for business rules such as login attempts. Each layer stops a different kind of abuse.
What if Redis is down?
Decide per route: fail open (allow) for normal traffic so an outage does not take the API down, fail closed (deny) for sensitive routes like login or OTP sending.
Summary
Pick an algorithm (token bucket or sliding window for smoothness), store counters atomically in Redis, choose keys per route, and return 429 with Retry-After and RateLimit headers.
Stuck on this one? Talk to Sumit.
Keys: F focus mode · ← → previous / next topic · Esc leave focus