Rate Limiting — Why and Where
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.
| Where | What it protects |
|---|---|
| Client | Avoids accidental repeats; cannot stop a hostile caller |
| API gateway | Shared entry capacity before services do work |
| Service | That service when callers bypass the gateway |
| Expensive operation | Database searches or paid calls with limited capacity |
| Limit key | Benefit | Cost |
|---|---|---|
| User ID | Fair allowance per account | Requires login; multiple accounts can evade it |
| API key | Allowance per integration | Keys need management |
| IP address | Works before login | Offices and mobile networks share addresses |
| Global | Caps total load | One busy caller may consume the allowance |
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.
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.
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.
Contain a runaway bot
Each trading account may submit 50 orders per second.
A bot and another account pass through separate account limits before reaching the matching engine.
Reject excess requests before matching work begins. Also cap total traffic so many accounts cannot overwhelm the engine together.
The insight
Per-account limits isolate excessive clients.
Choose the right identity
An anonymous scraper hammers listing pages from one IP address.
Use a per-IP limit for anonymous browsing and a higher per-account allowance for logged-in shelters.
Check shared-network complaints before tightening IP limits. Many shelter staff may appear under the same address.
The insight
Choose a limit key that matches who you want to restrain.
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
Takeaways
- Rate limits protect capacity and fairness.
- Choose where to enforce and what to count by.
- Tell clients with 429 and Retry-After.