System Design Academy

Saving…

Lesson 2 of 10 in Module 50 of 54 lessons complete

Rate Limiting — Why and Where

Module 5, lesson 2Concept12 min

Definition

Rate limiting

In plain English
Rate limiting caps how many requests a client may make in a period of time — you met it briefly in Module 2's API gateway lesson.
Think of it like
It is like a theme-park ride letting each guest go once per hour.

Why it exists

One voting app sends the same request in a loop. Other voters wait while that client consumes the available capacity.

You need a rule that shares capacity and bounds costs. It should also tell clients when they can try again.

How it works

429 Too Many Requests

In plain English
429 is the HTTP status code that tells a client it has hit a rate limit.
Think of it like
Like a bouncer saying "come back later".

Retry-After header

In plain English
Retry-After is a response header telling the client how long to wait before trying again.
Think of it like
Like a sign saying "next tour in 10 minutes".

Limit key

In plain English
A limit key is what you count requests by, such as user ID, API key or IP address.
Think of it like
Like counting rides per wristband rather than per family.
WhereWhat it protects
ClientAvoids accidental repeats; cannot stop a hostile caller
API gatewayShared entry capacity before services do work
ServiceThat service when callers bypass the gateway
Expensive operationDatabase searches or paid calls with limited capacity
Limit keyBenefitCost
User IDFair allowance per accountRequires login; multiple accounts can evade it
API keyAllowance per integrationKeys need management
IP addressWorks before loginOffices and mobile networks share addresses
GlobalCaps total loadOne busy caller may consume the allowance
Wait 30 seconds; X-RateLimit fields are illustrative application headers, not universal HTTP fields.
HTTP
HTTP/1.1 429 Too Many Requests
Retry-After: 30
X-RateLimit-Limit: 60
X-RateLimit-Remaining: 0
Content-Type: application/json

{"error":"rate_limit","retry_in_seconds":30}

Clients reach a gateway decision that sends allowed requests to the service and excess requests to a 429 response.

Reject excess requests before expensive work begins.

Clients reach a gateway decision that sends allowed requests to the service and excess requests to a 429 response.

Examples

Share voting opportunities

Each phone number may vote once per minute during the final.

Votes are counted per phone number, with the first accepted and excess votes rejected using 429.

The phone number is the fairness rule.

Votes are counted per phone number, with the first accepted and excess votes rejected using 429.

Return Retry-After with the remaining wait. Enforce the rule on the server rather than relying on the voting screen.

The insight

Limits enforce fairness as well as capacity.

Trade-offs

Pros

  • Protects capacity and costs.
  • Shares access between clients.
  • Reduces abuse.

Cons

  • Rejects legitimate bursts.
  • Shared IPs combine innocent users.
  • Global limits need coordinated counting.

Use when

  • Public APIs accept unbounded traffic.
  • Operations consume scarce capacity.

Avoid when

  • Existing queues already bound this internal work.
  • An IP-only rule would block a whole shared network.

Quick check

What status code says "you've hit the rate limit"?
Why can limiting purely by IP address hurt innocent users?

Takeaways

  • Rate limits protect capacity and fairness.
  • Choose where to enforce and what to count by.
  • Tell clients with 429 and Retry-After.

Try the quick check above whenever you feel ready.

Previous: Failure Is Normal: Timeouts, Retries & Backoff