Interview's MateSystem Design
Interview's Mate

System Design

Architecture & trade-offs

Visualizer

DSA Visualizer
All 32 lessons
Custom Playground
Try your own examples

Learning

System Design
31 architecture topics
SQL Learning
160 foundations → production topics
AI Engineering
160 LLM → backend topics
Data Science
160 Python → MLOps topics
Data Engineer
160 SQL → streaming topics
Docker
160 Docker learning topics
Kubernetes
160 K8s fundamentals → cloud
Java LearnSoon
Coming soon
Python Learn
Interview Q&A & internals
JavaScript Learn
Closures, async & DOM Q&A
Tech Learn
Search, practise & track Q&A
AI DSA Practice
Solve coding problems
Theme
Home
System Design topics
Lesson 26/ 31
Reliability, Security & Case Studies

Rate Limiting & Throttling

Protect APIs from abuse, DDoS bursts, and noisy neighbors using token buckets and Redis.

IntermediateToken Bucket · Sliding Window · 4293 quiz questions0/2 subtopics done
Practice in AI HLD
In plain wordsDiagramTheorySubtopics (2)Step by stepWhen to use itInterview cluesMistakes to avoidQuiz (3)Practice
Deep-dive subtopics (2) - 0/2 completed
Token Bucket vs. Sliding Window Algorithms

Compare burst-friendly Token Bucket against strict Leaky Bucket and accurate Sliding Window counters.

Intermediate
Distributed Rate Limiting & Leaky Bucket

Coordinate limits across dozens of API gateway pods and smooth outbound bursts.

Intermediate

In plain words

Rate limiting caps how many requests someone can make in a period of time, like a turnstile that only lets so many people through per minute.

Think of it like…

A nightclub bouncer letting people in at a steady pace (tokens) and asking the overflow crowd to wait outside instead of packing the dance floor past fire code.

Where you’ve seen it

GitHub’s API allows 5,000 requests per hour per user. Go over that and you get a 429 "Too Many Requests" error until the limit resets.

Words you’ll hear

Token bucket
Tokens refill at a steady rate; each request spends one. Allows short bursts.
Sliding window
Counts requests over a rolling time window for smoother limits.
HTTP 429
The standard "Too Many Requests" response.
Throttling
Slowing requests down rather than rejecting them outright.

Visual diagram

Loading interactive diagram
Loading interactive exercise

Theory & deep dive

Why Rate Limiting belongs at the Edge/Gateway

Without rate limits, a single buggy customer script in a while(true) loop, a scraper bot, or a DDoS attack can exhaust all database connections and take down every customer.

Rate limiters reject excess requests early at the API Gateway with HTTP `429 Too Many Requests` and helpful headers (X-RateLimit-Remaining, Retry-After), keeping backends healthy for everyone else.

Key takeaway: Use Token Bucket (for bursts) or Sliding Window with an atomic Redis Lua script at the API Gateway.

How it works, step by step

Step 1 of 4
API Gateway / Edge

1. Choose rate-limiting dimensions & placement

Enforce limits at the edge/API Gateway keyed by user_id, api_key, ip, or route before requests touch compute or databases.

Return 429 + Retry-After & X-RateLimit-* headers
Key numbers at this step
Status Code
429 Too Many Requests
Keys
IP / User / Tenant

When to use it & trade-offs

What it is

Rate limiting caps how many requests a user, IP, or tenant can send in a time window, rejecting excess traffic cleanly with HTTP 429 Too Many Requests.

Why it matters: A single buggy client loop, scraper, or credential-stuffing bot can saturate databases and spike cloud bills in seconds.

When to reach for it

  • API monetization tiers (Free = 60 req/min, Pro = 1,000 req/min)
  • Brute-force protection on login, OTP, and password-reset endpoints
  • Tenant isolation in multi-tenant SaaS so one customer cannot starve others

When to avoid or be careful

  • Using naive GET count then INCR count in two separate Redis calls (race condition under concurrency)
  • Using Fixed Window counters when exact burst limits matter at minute boundaries

Rate-Limiting Algorithms Compared

Token Bucket
Best for: Public APIs (Stripe, GitHub) allowing brief bursts at a steady average rate

Strengths: Tiny state (2 numbers: tokens + last_refill); naturally smooths bursts.

Watch out: Requires tuning both burst capacity and refill rate.

Leaky Bucket (Queue)
Best for: Smooth, constant-rate egress to fragile downstream APIs

Strengths: Outputs traffic at an exact steady drip with zero downstream bursts.

Watch out: Queue fills under heavy spikes, adding latency or dropping newest requests.

Sliding Window Counter
Best for: Strict per-minute/hour quotas without 2× boundary spikes

Strengths: Approximates exact sliding window using just two adjacent counters (~O(1) RAM).

Watch out: Slightly approximate across boundaries vs. storing every timestamp.

Interview signals & numbers to know

When this topic comes up

  • prevent abuse
  • brute force
  • API quota
  • noisy neighbor
  • 429 Too Many Requests

Benchmarks & rules of thumb

Redis Lua check RTT -
~0.5 ms
Token bucket memory/key -
~16–64 bytes
10M active keys RAM -
<1 GB in Redis
Standard response -
HTTP 429 + Retry-After

Common mistakes & follow-ups

Common interview mistakes

  • Storing a full list of every request timestamp per user at 100k QPS (Sliding Window Log eats massive RAM)
  • Rate limiting only by IP behind shared corporate/mobile NATs (blocks thousands of innocent users)
  • Forgetting distributed race conditions across multiple gateway pods

Likely interviewer follow-ups

  • Explain the boundary-burst flaw of Fixed Window Counters and how Sliding Window Counter fixes it.
  • How do you make distributed Redis rate limiting atomic without a full lock?
  • What should happen if the Redis rate-limiter cluster goes down - fail-open or fail-closed?
Interview tip: Explicitly mention a Redis Lua script for atomic token updates and fail-open local fallback if Redis blips.

Knowledge check

Loading quiz

Put it into practice

Design full architectures with AI feedback

Apply Rate Limiting & Throttling inside real High-Level Design prompts or review Q&A flashcards.

Open AI HLD Practice →SD Interview Q&A
Previous topic
Resilience & Fault Tolerance
Next topic
Observability, Logging & Tracing