Try
↑ ↓ move↵ openesc close

Interview prep / Rate limiting algorithms

Mid · Rate limiting algorithms · System design interview question

A nightly job must make 5,000 calls to a partner API that allows 10 requests a second. How do you build the client side so it neither gets blocked nor takes longer than it needs to?

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

A strong answer

Put a limiter on your own side that paces calls below the partner's limit. A leaky bucket fits well, because the job can tolerate delay: calls go into a queue and leave at a fixed rate. Set the rate a little under the partner's, say 9 a second, since your clock and theirs will not agree and a window edge on their side can bunch your calls. At 9 a second, 5,000 calls take about 556 seconds, under ten minutes. At 10 a second it would be 500 seconds. The margin costs about a minute.

If the partner uses a token bucket with a burst allowance, you could send the first calls faster, but the saving is a few seconds on a job this long. It is not worth the risk of being rejected.

Still expect 429 responses, because other jobs of yours may share the key. When one arrives, honour its Retry-After, pause the whole queue and not just that call, and resume at the normal rate. Retrying only the failed call while the rest keep flowing keeps you at the limit and earns more rejections.

Run several workers in parallel only if they share one limiter. Ten workers each pacing at 9 a second send 90.

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