Backend and APIs · Question 19 of 50
How would you rate-limit an API?
An algorithm, plus what changes with many servers.
The answer
Count requests per client (API key, user or IP) and refuse the extra ones with 429 Too Many Requests and a Retry-After header. The token bucket is the usual algorithm: each client has a bucket of N tokens that refills at a steady rate, each request takes one, and an empty bucket means 429. It allows short bursts up to N while holding the average rate.
A fixed window (100 a minute) is the simplest but allows double bursts at the edge of two windows; a sliding window smooths that. With many servers, keep the counters in a shared store such as Redis with atomic operations (INCR with an expiry, or a Lua script).
The picture
bucket: holds 10, refills 1 token a second [● ● ● ● ● ● ● ● ● ●] request ─▶ take one ─▶ OK [ ] request ─▶ empty ─▶ 429, Retry-After: 1
Is limiting per IP enough?
Show the answer
Many people can share one IP (offices, mobile networks), and attackers rotate IPs. Limit per user or API key, with per-IP as a backstop.