Rate limiting caps how many requests a client can send in a period of time. The client might be an IP address, a user, an API key, or a token. Past the cap, the server rejects or delays the extra requests, usually with HTTP 429 and a Retry-After value. This protects capacity, keeps usage fair, and controls cost. Building one comes down to four choices: the key, the algorithm, where it runs, and what the rejected response says.
What rate limiting is, and what it is not
A quota allows a fixed number of requests per window and rejects the rest. Throttling delays extra requests instead, so they still run, only later. Load shedding drops work because the server is overloaded, no matter who sent it.
Being rate limited means the server counted more requests from your key than its policy allows. For the operator, a jump in rejections can mean a buggy client or a limit set too low, so alert on the rejection rate as you would on errors.
Choose the key and where the limiter runs
The key decides who shares a counter. Use the IP address for anonymous traffic, but many users can sit behind one office or mobile network address. Once a request is authenticated, a user ID or API key is fairer. Expensive routes such as search or login can get their own, stricter limit.
Placement decides what the limiter can see. An edge or CDN limiter stops floods early but sees only addresses and headers. A gateway or reverse proxy sees routes and keys. The application sees users, plans, and the cost of each request.
The NGINX limit_req module uses the leaky bucket method and, unless told otherwise, rejects with 503:
limit_req_zone $binary_remote_addr zone=api:10m rate=10r/s;
server {
location /api/ {
limit_req zone=api burst=20 nodelay;
limit_req_status 429;
}
}
In a test on NGINX 1.24.0, 40 requests sent at once gave 21 served (one plus the burst of 20) and 19 rejected. They got 503 without the last line and 429 with it.

Each checkpoint knows more about the visitor than the last.
Rate limiting algorithms compared
| Algorithm | Burst behavior | State per key | Precision | Typical use |
|---|---|---|---|---|
| Fixed window | Up to twice the limit across a window edge | One counter | Coarse | Daily or hourly quotas |
| Sliding window log | Never above the limit | One timestamp per request | Exact | Low-volume routes such as login |
| Sliding window counter | Small overshoot | Two counters | Approximate | General API limits at scale |
| Token bucket | Bursts up to bucket size, then a steady rate | Tokens and last refill time | Exact | Public APIs that allow short bursts |
| Leaky bucket | Smooths bursts into a steady outflow | Queue level and timestamp | Exact | A backend with fixed throughput |
The right choice depends on your traffic. If clients burst for good reasons, such as a page load that fires ten calls, use a token bucket. If the system behind the limiter takes a fixed rate, use a leaky bucket. A fixed window is fine for daily quotas. A sliding window counter avoids the edge burst at almost the same cost.
The burst at the edge of a fixed window
Here is a simplified example: a limit of 5 requests per 60 seconds, with requests at seconds 55 to 59 and 60 to 64. Both window types ran against the same arrivals with a fixed test clock:
FixedWindow: allowed 10 of 10 in 9 s -> [55, 56, 57, 58, 59, 60, 61, 62, 63, 64]
SlidingWindowCounter: allowed 6 of 10 in 9 s -> [55, 56, 57, 58, 59, 61]
The fixed window resets at second 60, so twice the limit gets through in nine seconds. The sliding window counter weights the previous window by how much of it still overlaps. At second 61 its estimate is 5 × 59/60 = 4.92, just under 5, so one extra request gets in.
Token bucket vs leaky bucket
In the token bucket algorithm, each key has a bucket of up to N tokens that refills at a steady rate. A request takes a token, and an empty bucket means rejection. After a quiet spell, a client can spend all N at once.
A leaky bucket queues requests and drains them at a fixed rate, rejecting arrivals when the queue is full. In NGINX, burst=20 is that queue. Without nodelay, extra requests wait their turn. With it, they are served at once but still fill the queue.
What a rejected request should return
RFC 6585 defines 429 Too Many Requests: the user has sent too many requests in a given amount of time. The response should explain the condition, may include Retry-After, and must not be stored by a cache. How you identify users and count requests is up to you.
RFC 9110 allows Retry-After as an HTTP date or a number of seconds. Seconds are safer because they do not depend on the client’s clock. A problem details body, defined in RFC 9457, gives a readable reason:
HTTP/1.1 429 Too Many Requests
Retry-After: 30
Content-Type: application/problem+json
{
"type": "https://api.example.com/problems/rate-limited",
"title": "Too many requests",
"detail": "This API key may send 100 requests per minute."
}

Retry-After tells the client when to try again.
Rate limit headers: conventions vs the IETF draft
No standard defines headers such as X-RateLimit-Limit; they are vendor conventions. GitHub sends x-ratelimit-limit, x-ratelimit-remaining, x-ratelimit-used, and x-ratelimit-reset, with the reset in UTC epoch seconds. Other APIs reuse the names with other meanings: resets appear as seconds, milliseconds, timestamps, or dates.
The IETF HTTPAPI working group’s draft 12, dated November 20, 2026, defines two fields instead, shown here in the draft’s own examples:
RateLimit-Policy: burst;q=100;w=60,"daily";q=1000;w=86400
RateLimit: default;a=50;w=30
q is the quota, w the window in seconds (in RateLimit, the seconds left), and a the quota still available. If a response also has Retry-After, that takes precedence. This is still an active Internet-Draft, not an RFC, so details can change.
Keep the counter correct under concurrency
When several app instances share a limit, the counter usually lives in Redis. A common first version reads the count, compares it with the limit, increments it, and sets an expiry when the value is 1, in three separate round trips.
Many requests can read the same count before any of them increments it, the classic check-then-act race. And if a client crashes between INCR and EXPIRE, the key never expires and that client stays blocked. The Redis INCR documentation warns about this and suggests a Lua script, which Redis runs as one atomic step:
local current = redis.call('INCR', KEYS[1])
if current == 1 then
redis.call('EXPIRE', KEYS[1], ARGV[1])
end
return current
The caller admits the request when the result is at or below the limit. Both versions ran on Redis 7.0.15 with redis-py 5.2.1, with a limit of 10 and 200 requests released at once from 200 threads:
trial 1: naive allowed 20 (ttl 60) | atomic allowed 10 (ttl 60)
trial 2: naive allowed 52 (ttl 60) | atomic allowed 10 (ttl 60)
trial 3: naive allowed 59 (ttl 60) | atomic allowed 10 (ttl 60)
trial 4: naive allowed 65 (ttl 60) | atomic allowed 10 (ttl 60)
trial 5: naive allowed 83 (ttl 60) | atomic allowed 10 (ttl 60)
INCR without EXPIRE -> TTL -1
The naive version let in up to 83 requests, while the script admitted exactly 10 every time. The TTL of -1 on the last line is a key with no expiry. This script is still a fixed window, so the edge burst applies. A token bucket needs the same rule: read, compute, and write inside one script.

A key with no expiry keeps the rope up after the window ends.
When you are the client being rate limited
On the client side of API rate limiting, GitHub’s documentation gives this order:
- If
Retry-Afteris present, wait that many seconds. - If the remaining count is 0, wait until the reset time.
- Otherwise, wait at least one minute.
- If requests keep failing, wait exponentially longer and give up after a fixed number of tries.
Add random jitter to each wait. If thousands of clients retry at the same reset second, they create a thundering herd problem, which the IETF draft warns about. Only retry requests that are safe to repeat, and replace polling with webhooks where you can.
Test and monitor a limit
Test window edges with a clock you control, then release a burst against the shared store: exactly the limit should pass, and no counter key should have a TTL of -1.
Before enforcing a new limit in NGINX, limit_req_dry_run on (since 1.17.1) counts excess requests without rejecting them. The $limit_req_status variable (since 1.17.6) can log PASSED, REJECTED, REJECTED_DRY_RUN, and other results per request.
Rate limiting system design checklist
- Pick a key per limit: IP when anonymous, API key or user when authenticated.
- Choose the algorithm by how your clients burst.
- Return 429 with
Retry-Afterin seconds and a problem details body. - Document what your
X-RateLimit-*values mean. - Keep read, increment, and expiry in one atomic step.
- Test window edges, concurrent bursts, and keys without expiry.
- Alert on rejections per key and route.



