Rate limiting caps how many requests a client can make in a period, such as 100 requests per minute per API key. It protects a service from overload, stops one heavy customer slowing everyone else, slows brute-force attacks on login and OTP endpoints, and controls spend on expensive calls such as LLM requests. "Throttling" is often a synonym, though some teams use it for delaying requests rather than rejecting them.
The status code and headers
When a client goes over the limit, the server should answer HTTP 429 Too Many Requests, defined in RFC 6585, section 4. The response may include a Retry-After header, defined in RFC 9110, section 10.2.3, as either a number of seconds or an HTTP date:
HTTP/1.1 429 Too Many Requests
Retry-After: 30
Content-Type: application/json
{"error": "rate_limited", "message": "Limit is 100 requests per minute"}
RFC 6585 also says caches must not store 429 responses. The IETF draft "RateLimit header fields for HTTP" defines standard RateLimit-Policy and RateLimit headers, but it is still an Internet-Draft, not an RFC, so header names in the wild vary.
The main algorithms
- Fixed window. Count each client's requests per calendar minute and reset at the boundary. One counter per client, easy in Redis with
INCRandEXPIRE. - Sliding window log. Store every request's timestamp and count the last 60 seconds. Exact, but memory grows with traffic.
- Sliding window counter. Keep the counts for the current and previous fixed windows, and weight the previous count by how much of that window still falls inside the last 60 seconds. Nearly exact with two counters.
- Token bucket. A bucket holds up to b tokens and refills at r tokens per second. Each request takes a token, and an empty bucket means 429. It allows short bursts of up to b while holding the long-run average to r. Amazon API Gateway throttles this way: its "rate" setting is the refill rate and "burst" is the bucket size.
- Leaky bucket. Requests join a queue that drains at a fixed rate, which smooths traffic but adds waiting.
Worked example: 100 requests per minute
A client sends 100 requests at 12:00:59 and another 100 at 12:01:00.
- Fixed window accepts all 200, because the second batch lands in a fresh 12:01 window. That is twice the intended rate within about a second.
- Sliding window counter at 12:01:00 gives the previous window almost 100% weight, because nearly all of it is still within the last 60 seconds. The estimate is 100, so the second batch is rejected.
- Token bucket with b = 100 and r = 100/60 (about 1.67 tokens a second): the first batch empties the bucket. One second later about one token has refilled, so one request succeeds and 99 get 429 with
Retry-After. An idle client can still burst 100 at once, which suits real users.
Where to enforce it
Put coarse per-IP and per-key limits at the edge: the API gateway, CDN or firewall. Put limits tied to business identity, such as five OTP attempts per phone number per hour, in the application. With several app servers, keep counters in a shared store such as Redis or Valkey and update them atomically (Redis vs Valkey vs Memcached).
Common mistakes
- Limiting only by IP address. Many mobile users share one public IP through carrier NAT, so limit by user or API key too.
- Retrying immediately on 429. Clients should honour
Retry-Afterand back off with jitter, or retries deepen the overload. - Returning 500 or 503 for a limit, which hides the cause.
We design API limits and retries as part of our product engineering work.