Design an API Rate Limiter

0%

Editorial Solution

System requirements

Functional

Capacity estimation

Traffic

API design

Decision API

Database design

Rule Configuration (PostgreSQL)

High-level design

Architecture Overview

Request flows

Allow/Deny Decision Flow

Detailed component design

Atomic Operations With Lua Scripts

Trade offs/Tech choices

Token Bucket vs Sliding Window

Failure scenarios/bottlenecks

Redis Failure

Future improvements

Multi-Scope Limits

Frequently asked questions

Which rate limiting algorithm should I use in an interview?

Why store counters in Redis instead of in each server's memory?

How do you stop two requests from both taking the last token?

What happens when Redis goes down?

How do you handle a single hot key?

Table of Contents

System requirements

Functional

Capacity estimation

Traffic

API design

Decision API

Database design

Rule Configuration (PostgreSQL)

High-level design

Architecture Overview

Request flows

Allow/Deny Decision Flow

Detailed component design

Atomic Operations With Lua Scripts

Trade offs/Tech choices

Token Bucket vs Sliding Window

Failure scenarios/bottlenecks

Redis Failure

Future improvements

Multi-Scope Limits

Frequently asked questions

Which rate limiting algorithm should I use in an interview?

Why store counters in Redis instead of in each server's memory?

How do you stop two requests from both taking the last token?

What happens when Redis goes down?

How do you handle a single hot key?

In short: put a stateless rate limiting service between the API gateway and your backends. For each request it builds a key from the user and endpoint, then runs a Lua script on Redis that checks and decrements a token bucket atomically, so the decision costs about 1 ms. It returns 429 with Retry-After on denial. Rules live in PostgreSQL and are cached on every worker. Use token bucket by default and a sliding window for login and payment endpoints. If Redis is unreachable, fall back to local counters at reduced limits instead of failing fully open or closed.

Premium content

Unlock

Premium content

Unlock

Premium content

Unlock

Premium content

Unlock

Premium content

Unlock

Premium content

Unlock

Premium content

Unlock

Premium content

Unlock

Premium content

Unlock

Premium content

Unlock

Which rate limiting algorithm should I use in an interview?

Token bucket for most endpoints, because it is simple and allows short bursts. Switch to a sliding window counter for endpoints that must not burst, such as login and payment.

Why store counters in Redis instead of in each server's memory?

In-memory counters need sticky routing, which creates hot spots and loses state on every restart. Redis adds about 0.5 ms but lets any worker serve any request.

How do you stop two requests from both taking the last token?

Run the check and the decrement as a single Lua script on Redis, so the operation is atomic.

What happens when Redis goes down?

Failing closed blocks all legitimate traffic, and failing open removes all protection. Instead, a circuit breaker opens after 3 timeouts and workers switch to local in-memory counters at 50% of normal limits until Sentinel promotes a replica, usually within 5 to 10 seconds.

How do you handle a single hot key?

Each worker collects N requests locally, then sends one DECRBY N to Redis. With N = 10, Redis traffic for that key drops 90%, but a few requests can get past the limit. That's fine for a search API, but not for a payment API with a limit of 10.

Try this problem yourself

Reading a solution is not the same as producing one under interview pressure. Design it on the whiteboard and get scored feedback.

Asked in these interview guides

Company guides that list this problem among their sample questions.

Concepts used in this design

The building blocks this design relies on, each explained in its own lesson.


Markdown supported

0%

Editorial Solution

System requirements

Functional

Capacity estimation

Traffic

API design

Decision API

Database design

Rule Configuration (PostgreSQL)

High-level design

Architecture Overview

Request flows

Allow/Deny Decision Flow

Detailed component design

Atomic Operations With Lua Scripts

Trade offs/Tech choices

Token Bucket vs Sliding Window

Failure scenarios/bottlenecks

Redis Failure

Future improvements

Multi-Scope Limits

Frequently asked questions

Which rate limiting algorithm should I use in an interview?

Why store counters in Redis instead of in each server's memory?

How do you stop two requests from both taking the last token?

What happens when Redis goes down?

How do you handle a single hot key?

Table of Contents

System requirements

Functional

Capacity estimation

Traffic

API design

Decision API

Database design

Rule Configuration (PostgreSQL)

High-level design

Architecture Overview

Request flows

Allow/Deny Decision Flow

Detailed component design

Atomic Operations With Lua Scripts

Trade offs/Tech choices

Token Bucket vs Sliding Window

Failure scenarios/bottlenecks

Redis Failure

Future improvements

Multi-Scope Limits

Frequently asked questions

Which rate limiting algorithm should I use in an interview?

Why store counters in Redis instead of in each server's memory?

How do you stop two requests from both taking the last token?

What happens when Redis goes down?

How do you handle a single hot key?