Rate Limiter

API Rate Limiting System

Design the classes of a rate limiter: interchangeable algorithms behind one interface, per-tier and global rules checked in order, a retry-after on every refusal, and correct counting under concurrency.

Overview

A rate limiter decides, for each incoming request, whether to let it through or refuse it because the caller has sent too many recently. It protects a service from one noisy client, a runaway script or a traffic spike. A refused HTTP request usually gets status 429 Too Many Requests, which RFC 6585 defines and which may carry a Retry-After header saying how long to wait.

In a low-level design interview the question is about the objects: one interface that every algorithm implements, a small rule list that says which key each limit applies to (user, tier, whole service), time passed in so tests are exact, and a lock that makes 'count and decide' one step. The interviewer also expects you to compare the algorithms and to say which one you would pick and why.

Scoping the problem

Six questions settle the design before you write a class. For each, here is why it matters and the answer most interviewers accept.

  • What is the key: user, API key, IP or endpoint? It decides the key function of each rule. Usually the key is the user, plus a global cap.
  • Are there tiers, such as free and paid? Tiers mean different limits for the same rule. Usually yes.
  • Should short bursts be allowed? A token bucket allows them; a window may not. Usually yes, up to a set size.
  • How exact must the count be? Exact logs cost memory, while counters approximate. Approximate is usually fine.
  • One process or many servers? This decides between local state and a shared store. Design for one process here; many is a follow-up.
  • What does a refused caller get? This sets the status and headers. The usual answer is 429 with Retry-After.

Functional requirements

  • try_acquire(key, now) returns a Decision: allowed, remaining, and retry_after in seconds when refused
  • Algorithms: token bucket, fixed window, sliding log and sliding counter, all interchangeable
  • Rules pair a key function (user, tier, global) with an algorithm; RateLimiter.check runs them in order and reports which rule refused
  • Tiers: ByTier routes each (tier, id) key to that tier's algorithm
  • Idle token buckets are dropped once they have refilled to full

Non-functional requirements

  • O(1) time and memory per key for token bucket, fixed window and sliding counter; O(limit) for the sliding log
  • Concurrent requests for the same key are counted exactly: check and update happen under one lock
  • A new algorithm is a new Algorithm subclass; RateLimiter and the rules do not change
  • Time is passed in, so every decision in the tests is exact

Interview questions

  • Which algorithm would you pick for a public API, and why?
  • The per-user rule allowed a request and then the global rule refused it. Was the user charged?
  • How does this change with ten API servers behind a load balancer?
  • What should the client see when it is refused?

Sign up free to read the full topic

More Low-Level Design topics

Start free on Cariara · Coding problems · System design studio