Skip to content
← All articles
Worked examples

Worked example: design an API rate limiter

One strong design for a junior-level question, from the first clarifying questions to what happens when the limiter itself breaks.

A blue bucket dispensing golden tokens one at a time through a small tap

This walkthrough shows one strong design and the trade-offs it makes. Where another choice would work as well, it says so. Scale figures come from the question's brief.

Pin down what is being limited

Before drawing anything, settle what "limit" means here. Ask:

  • Limited by what? Per API key, as the question says. Not per IP address, since many customers share offices and proxies.
  • Over what window? Per minute, with limits that differ by plan, from 100 requests a minute on Free to 10,000 on Enterprise. Ask whether short bursts are acceptable, such as 20 requests in one second from a key whose limit is 100 a minute.
  • How strict? Does a key that sends 103 requests in a minute need exactly three refusals, or is "about 100" good enough?
  • What does a refusal look like? An HTTP 429 Too Many Requests response with a Retry-After header tells a well-behaved client when to come back.

For this example, assume bursts are fine as long as the average stays within the limit, and "about right" is acceptable. Those two answers decide the counting method below.

Get a rough sense of scale

The brief gives about 50,000 requests a second at peak, spread across 30 API servers, and about 1 million keys. Each key needs only a couple of numbers of state, so all the counts fit in memory on one machine. Storage is easy. The difficulty is keeping 30 servers in agreement while adding at most a few milliseconds to a request that takes about 50 ms.

Choose how to count

Common approaches, each defensible:

  • Fixed window: count requests per calendar minute. Simple, but a key can send 100 requests at 12:00:59 and another 100 at 12:01:00.
  • Sliding log: keep a timestamp for every request. Exact, but memory grows with traffic.
  • Sliding window counter: blend this minute's count with last minute's. Close to exact for little memory.
  • Token bucket: each key has a bucket that refills at a steady rate. A request takes a token or is refused.

This design uses a token bucket, because it allows the short bursts the requirements accept while holding the average to the limit. It stores two numbers per key: tokens left and when the bucket was last refilled. A sliding window counter would meet the same requirements. Say which one you chose and why.

Keep the counts in one place

If each API server counted on its own, a key could get its limit 30 times over, once per server. So the counts live in a shared in-memory store, such as Redis, next to the API servers. Routing every request for a key to one limiter instance is another way to keep a single count.

The check and the update must happen together. Two servers reading "1 token left" at the same moment would both let a request through. Running the refill-and-take step as one atomic operation in the store, such as a script, prevents that race.

Keep the check off the slow path

The limiter runs as middleware in each API server, before any real work. An API gateway in front of the servers is an equally valid place for the check. Keep the store in the same data centre and measure the round-trip time under load against the few-millisecond budget. Each server also caches the limits themselves and refreshes them every few seconds, so changing a customer's limit needs no deploy and adds no lookup per request.

Decide what happens when the limiter fails

If the counter store goes down, the API can fail open and let requests through, or fail closed and refuse them. The brief says the API must keep serving, and refusing every customer because the limiter broke is worse than briefly not limiting, so this design fails open. As a backstop, each server keeps a rough local limit per key, set well above normal traffic, and the outage raises an alert.

The design

Client
Client
Load balancer
Gateway
API servers
Service
API handlers
Service
Limits database
Database
Shared counter store
Cache
One token count shared by all API servers

Trade-offs

  • Counts are shared, so every request makes one extra round trip. That is the cost of a limit that holds across 30 servers.
  • Token buckets allow bursts. A customer whose backend cannot absorb them would need a smaller bucket.
  • Failing open means a broken store briefly removes protection. The local backstop limits how far a key can go over.

What the grading looks for

The question's criteria name concerns, not components: where each request is counted and checked, one atomic count shared by every server, a 429 that says when to retry, and what happens to requests when the counter store is unavailable. This design meets each of them in one way. A gateway instead of middleware, routing each key to one limiter instance, or rejecting requests only on sensitive endpoints during an outage could meet the same criteria.

Two requests compete for the last token

Practise the API rate-limiter question in Grind. Talk through two requests on different servers competing for the last token. Explain why you chose your counting method and what happens when the counter store fails.

← Back to the blog