Try
↑ ↓ move↵ openesc close

Interview prep / Rate limiting algorithms

Junior · Rate limiting algorithms · System design interview question

Your API limits each source address to 10 requests a second. A customer with 300 staff behind one office gateway says the API keeps rejecting them. What is going on, and what would you change?

Sketch your own answer first, then compare it with the one below.

A strong answer

All 300 people reach you through the gateway's single public address, so they share one allowance. If even a third of them are active at the same moment, 100 users split 10 requests a second, which is 0.1 request a second each. Any page that makes a few calls hits the limit at once. The limiter is working as written. The key is wrong for this customer.

The address is a poor stand-in for a person. Behind one address you can find an office, a university, a mobile carrier or a proxy, and many unrelated people. The reverse also happens: one attacker can use many addresses and never meet the limit.

The change is to key on what you actually want to protect. For authenticated calls that is the API key or account, which maps to a customer and to a contract. Keep a coarse address limit, set high, as a guard for anonymous traffic. If the customer is a paying one, you can also raise their limit on request, or give a known gateway address its own larger allowance.

Then tell them what happened. A 429 that names the limit hit, plus a Retry-After header, would have saved the support ticket.

Sources

Learn it by running it

The rate limiting algorithms lesson explains the mechanism behind this question, and its lab lets you change the parameters and watch the effect.

Your progress is saved in this browser. Sign in to keep it on every device. Privacy · Terms