Skip to the guide

Design a rate limiter

IntermediateCase study12 pages

A rate limiter decides, for every call, whether this caller has sent too many lately, and refuses the extra ones fast. It's a set of turnstiles at a stadium that all share one count of how many times each season ticket has been through today.

It's also asked as "design a distributed rate limiter" or "rate-limit our API". It builds on the developer platform in Public APIs for developers, with limits like those on GitHub's and Stripe's APIs. Interviewers like it because the API fits on a napkin, so the round is about counting correctly across many servers, what happens when the counter store fails, and regions. The algorithms are taught in Rate limiting algorithms; this page builds the service around them. Its closest case study is the Distributed cache, which runs the kind of store the counters live in.

Below is a full mock interview following the seven steps of the framework. You're the candidate: at each Guess before you read on, stop and answer first.

Step 1 · Clarify the features and cut the scope

Minutes 0–5 · How this step works

Interviewer: Design a rate limiter for our public API.

You: A few questions first. Who calls the API, and what do we limit by: an API key, a user or an IP address?

Interviewer: Developers' apps, each with an API key, and most calls act for one of the app's users with that user's token. Sign-up and login have no key.

You: So a call can count against its user, its app or its IP address. What are the limits?

Interviewer: 5,000 calls an hour per user token, and 5,000 a second per app by default. Big partners get more.

You: Over a limit, do we refuse or queue? And do some endpoints cost more than others?

Interviewer: Refuse, and tell them when to retry. A search costs ten.

You: One region or several?

Interviewer: Three in the end. Start with one.

You: Then I'll cover per-user, per-app and per-IP limits with a cost per endpoint. Over a limit we refuse with Retry-After, every answer says what's left, and rules change without a deploy. Out: monthly billing quotas, network floods, which the CDN absorbs, and limits between our own services. OK?

Interviewer: Good.

Each answer shapes the design: two keys per call mean two counters, refusing means no queue, costs mean a call can take more than one token, and three regions mean the counters can't all live in one place.