Why Limit Requests? Rate Limiting Basics
Overview
Rate limiting controls how many requests a caller may send in a given amount of time, and rejects or delays the extra ones. It keeps one greedy or buggy client from hurting everyone else.
Detailed Notes
What is rate limiting
Rate limiting sets a maximum rate (requests per second, minute, etc.) for a caller. Requests beyond it are rejected or delayed. It usually lives in the API gateway (previous chapter), but can also run at the edge or inside services.
The problem it solves
A free weather API is popular. One developer writes a loop that calls it 5,000 times per second. The servers spend most of their time on this one client and everyone else sees timeouts. Without limits:
- Capacity is lost: one client can overload the system.
- Fairness suffers: a "noisy neighbour" starves the others.
- Security is weaker: brute-force password guessing and scraping are easy.
With a limit of, say, 50 requests per second per key, the runaway client gets HTTP 429 Too Many Requests and is slowed while everyone else is unaffected. Limits also enable pricing tiers (Free: 60/min, Pro: 600/min).
How it works
For each request, the limiter:
- Identifies the key: what we count by.
- Looks up current usage for that key.
- If within the limit, allows the request and updates usage.
- If over, rejects (429) or delays it.
What to count by:
- User or account ID: fair for logged-in users.
- API key / client ID: typical for developer APIs, easy to attach plans.
- IP address: works for anonymous traffic, but many users can share one IP (offices, mobile networks), so innocent users get blocked.
Rate vs quota; burst vs sustained. A rate limit works over short windows (per second/minute) and protects the system now. A quota works over long windows (per day/month) and is about billing. Sustained rate is the average speed allowed; a burst is a short spike above it. Good limits allow reasonable bursts (a page firing ten calls at once) while capping the average.
Where to enforce: use layers: coarse limits at the edge, per-client limits at the gateway, protective limits inside critical services.
A simple example
A nightclub has a capacity limit and a door person who admits people at a steady pace, so one big group can't fill the venue. Premium members get a faster line.
System design example
A public API puts a limiter in its gateway. Free keys get 60 requests per minute, Pro keys 600. The login endpoint allows 5 attempts per minute per account to slow brute-force attacks. Every rejection is logged as a metric so the team can tune limits. Tight limits may block real users; loose limits risk overload, so tune with real data.
Summary
- Rate limiting caps requests per caller over time, protecting capacity, fairness, security and cost.
- Choose the key carefully (user, API key, IP, endpoint, global).
- A rate limit protects now; a quota is for long-term usage and billing.
- Allow bursts but cap the sustained rate; enforce in layers.
Architecture Diagram
Limits at the edge, the gateway and the service protect the system in layers.
- 1A request arrives and the limiter identifies the key.
- 2It checks the key's usage against its limit.
- 3Within the limit: the request continues.
- 4Over the limit: the client receives 429 Too Many Requests.
Common Interview Questions
Finished this lesson?
0 of 91 lessons done · 0% of the course

