Try
↑ ↓ move↵ openesc close

Interview prep / Rate limiting algorithms

Senior · Rate limiting algorithms · System design interview question

What goes wrong with a rate limiter in production, and how do you design for it?

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

A strong answer

The limiter is a dependency on the hot path of every request, so it fails in all the ways a dependency does. The counter store can be slow or down. Clocks on different servers can disagree and shift window edges. A few very hot keys can overload one shard of the store. A key space that includes addresses behind a shared gateway can limit thousands of users as if they were one.

Design for each. Put a timeout on the limiter call and decide the fallback: open for protective limits, closed for quotas. Use a local approximate limiter as a backstop when the shared one is unreachable. Take time from the store where it matters, not from the app servers. Shard or add local caching for hot keys. Choose keys to match the thing you want to protect, and be careful with address-based keys.

A limit is also a product surface. Clients will build against it. Changing rates silently breaks them. Publish the limits, return them in headers, and roll changes out gradually, observing the rejected share per limit.

Finally, a limiter does not protect against the sum of many well behaved callers. For that the server needs load shedding on its own state.

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