SkillSprout
Chapter 36 · Lesson 4
Advanced
4 min read

Capstone: Design a Rate Limiter Service

Overview

A rate limiter decides, for every request, whether a client is still within its allowed quota. It sits on the hot path of every API call, so it must be extremely fast, correct under concurrency and never become the thing that takes the system down.

Detailed Notes

What is the task

Design a shared service that many APIs (usually through the API gateway) call to check whether a request from a given client, user or IP address is allowed. Over-limit requests are rejected with HTTP 429 (Too Many Requests).

The problem it solves

Without limits, one noisy or abusive client can exhaust capacity for everyone. A limit written inside each server only sees that server's traffic, so with 50 servers a client could get 50 times its quota. A central service gives one consistent answer.

How it works: follow these steps

  1. Requirements. Functional: configurable rules (for example 100 requests a minute per API key, stricter for login), allow or reject each request, and tell the client how long to wait. Non-functional: adds only a few milliseconds, highly available, accurate enough, and works across many servers.
  2. Estimate. Say the platform handles 1 million requests a second and has 10 million active clients. Each check is one small read-modify-write. Storing a counter or token bucket of about 100 bytes per client means 10 million x 100 bytes = 1 GB, which fits comfortably in memory. The challenge is throughput and latency, not storage.
  3. High-level design. Requests hit the API gateway, which calls the rate limiter (or runs it as a library). The limiter keeps state in an in-memory store such as Redis, and reads rules from a rules database cached locally. Responses include headers like remaining quota and retry-after.
  4. Deep dive: the algorithm. Token bucket is a good default: each client has a bucket that refills at a steady rate up to a maximum; a request spends one token. It allows short bursts yet enforces the average rate, and stores only two numbers (tokens, last refill time). A sliding window counter is smoother for strict limits; a fixed window is simplest but allows double bursts at window edges.
  5. Deep dive: concurrency. Two requests can read the same count at the same moment and both pass. Make the check-and-update a single atomic operation, such as an atomic increment or a small script that runs inside the store, instead of read, then write.
  6. Scaling the state. Partition clients across store nodes by hashing the client key, so each client's counter lives on one node. Replicate each node so a failure does not wipe quotas.
  7. Failure behaviour. If the limiter or its store is unreachable, choose a policy. Fail open (allow traffic) protects availability; fail closed (reject) protects the backend. Most public APIs fail open, and sensitive ones such as login may fail closed. Also use a short timeout so a slow limiter never stalls requests.

A simple example

A rule says 100 requests a minute per API key. Key k42 sends a request. The gateway asks the limiter, which runs one atomic step on the node that owns k42: refill tokens based on elapsed time, and if at least one token remains, spend it. The gateway forwards the request. On the 101st request in that minute no tokens remain, so the gateway returns 429 with a retry-after header.

System design example

text
Client -> API gateway -> Rate limiter -> Redis cluster (partitioned by client key, replicated)
Rate limiter -> local cache <- Rules database
Allowed -> Backend services ; Rejected -> 429 + Retry-After
Limiter metrics -> monitoring and alerts

Bottlenecks and fixes:

  • Network hop per request: keep the limiter close to the gateway and the store in the same region; optionally let each gateway take small batches of tokens and sync periodically.
  • Hot keys: one very busy client hammers one node; allow local pre-checks, or split that key's quota across several counters.
  • Store failure: replicas plus the fail-open or fail-closed policy.
  • Clock differences: compute time on the store side so servers do not need synchronised clocks.

Trade-offs to mention: exact global limits cost a network call every request, while local approximate limits are faster but can slightly exceed quotas; token bucket allows bursts while a fixed window is simpler but less fair.

Summary

  • A central limiter gives one consistent answer across all servers.
  • Token bucket is a strong default; keep state tiny and in memory.
  • Make check-and-update atomic to avoid races.
  • Partition and replicate the store, and decide fail open versus fail closed.

Architecture Diagram

Rate limiter service
Architecture
Client
API gateway
Rate limiter
In-memory storetoken buckets
Backend

The gateway asks the limiter, which checks atomic counters in memory.

  1. 1The gateway asks the limiter about the client's key.
  2. 2The limiter atomically refills and spends a token in the store.
  3. 3Allowed requests go to the backend.
  4. 4Over-limit requests get 429 with a retry-after hint.

Common Interview Questions

Finished this lesson?

0 of 91 lessons done · 0% of the course