Your server returns 429 Too Many Requests and the client keeps retrying relentlessly. Or the opposite: you want to set a limit and don't know what to base the number on — IP or token, route or the whole service. This text opens up exactly those decisions: defining the limit, the window, the headers, and what should happen after the limit is exceeded.
What unit should the rate limit be based on
There are three common units, and the wrong choice always leads to two bad outcomes: either you block a legitimate customer, or you fail to stop an attacker.
- IP: The simplest. But behind NAT and proxies, hundreds of real users share one IP. If you base the limit on IP and don't account for Iranian mobile users, you'll return 429 to a large portion of healthy traffic.
- Token or API key: The most precise unit for authenticated services. You tie the limit to the customer's identity, not to the network address.
- Route: Necessary but not sufficient.
/login,/search, and/exporthave three completely different behaviors, and one number for all three is wrong.
The correct combination in most services is: primary key on the token, secondary key on IP for unauthenticated routes, and a separate limit for heavy routes. If you're behind a CDN, the real IP is in X-Forwarded-For and must only be read from a trusted proxy, otherwise any client can change its own limit with a forged header.
Time window: fixed, sliding, or token bucket
A fixed window is simple: the counter resets to zero at the start of each minute. Its problem is the edge burst. Someone who sends 99 requests in second 59 and 99 requests in the first second of the next minute has actually made 198 requests in two seconds and seen no limit at all.
A sliding window solves this, at the cost of more memory and computation. A token bucket offers more flexibility because it allows a short burst up to the bucket size and then limits the rate to the bucket's refill rate. For a public API, I'd choose the token bucket. For sensitive routes like login and SMS sending, a sliding window with a stricter limit.
| Algorithm | Edge burst | Implementation cost | Best for |
|---|---|---|---|
| Fixed window | Yes | Low | Low-importance routes |
| Sliding window | No | Medium | Login, signup, OTP |
| Token bucket | Controlled | Medium | Public API |
In Nginx, the simplest form is with limit_req_zone and limit_req. One point that's often misconfigured: burst is a queue, not a discount. With nodelay, the queue isn't discarded — it's just served immediately; without it, excess requests get delayed responses and the client thinks the service is slow.
limit_req_zone $binary_remote_addr zone=api:10m rate=10r/s;
limit_req zone=api burst=20 nodelay;
The 10m value holds roughly 160,000 IP keys; if it exceeds that, Nginx evicts old keys and counting starts from zero. You see this in real traffic, not in testing.
Headers the client should see
A limit without headers is useless to an API consumer. The client needs to know how much is left and when it's allowed again. The minimum set:
RateLimit-Limit: 100
RateLimit-Remaining: 0
RateLimit-Reset: 42
Retry-After: 42
Retry-After is in seconds and is meaningful in 429 and 503 responses. If you calculate its value from a fixed window, you'll give the wrong number in a sliding window, and the client will either come back too early or wait for no reason. Get the number from the same structure that does the counting, not from a separate estimate.
The X-RateLimit-* headers are older and are still read by many SDKs; if you send both, the values must match. Two headers with two different numbers are worse than not having them at all.
What should be returned after exceeding the limit
A 429 response should have three things: the correct status code, Retry-After, and a body that says which limit was broken. An empty body leaves support guessing.
{"error":"rate_limited","scope":"token","limit":100,"window":60,"retry_after":42}
An important point: don't substitute 429 with 503. 503 means the service is temporarily unavailable, and good clients read it with a longer backoff and sometimes failover to another source. 429 means your requests were too many, not that the service is broken.
And a decision that's rarely made: does a rejected request count toward the counter? If yes, a client stuck in a retry loop will never get out. Don't count 429 requests and don't refresh the counter.
Where they get it wrong
A common mistake I've actually seen: they put the limit on the application layer and leave the web layer without a limit. The result is that a simple scanner sending 500 requests per second fills up the workers before your code even reaches the counter, and the site returns 502 to everyone. The sign is that the application log shows no 429s, but monitoring reports rising latency and connection queue. The limit must also exist at the layer closest to the edge, even if it's cruder.
The second mistake: the same limit for all customers. A paying customer pulling a heavy report and a free customer just checking status don't share one number. Tie the limit to the subscription tier or to the token, not to a fixed number in code.
Where to get the number
Pull the limit from real logs, not from feeling. Calculate the 99th percentile of requests per minute per token and set the limit slightly above it. If you set the number too tight from the start, you'll block your first real customer and then have to change the limit several times, which itself builds distrust.
To see how your service responds from the outside, use the DNS and network tools and check the response headers from several points. If the service is running on Linux hosting, set the web-layer limit right there in Nginx or in the reverse proxy in front of it so the counter stays in shared memory and is the same across workers.
If you have more than one instance, an in-memory counter is wrong. With two instances, the real limit doubles and no one notices until the day traffic goes up. Keep the counter in Redis with INCR and EXPIRE, or if an exact limit isn't required, at the edge with a shared layer.
Frequently asked questions
What's the difference between 429 and 503 in rate limiting?
429 means your requests have exceeded the allowed limit and you should wait. 503 means the service is temporarily not responding due to load or maintenance. The client should respect Retry-After for 429 and retry with exponential backoff for 503.
If you mix these up, your monitoring tools give false alerts and the support team goes looking for a service failure that doesn't exist.
Should I base the rate limit on IP or on token?
For authenticated routes, token. For public routes like login and signup, IP. In practice, have both with different limits.
If you only see IP and many users are behind one NAT, set the limit more generously and be stricter on the token instead.
Why is it still being scanned even though I've set a limit?
Probably the limit is at a layer the scanner doesn't reach, or the counter isn't shared across instances. Compare the edge log with the application log.
If requests are spread across multiple IPs, a single-IP limit is ineffective and you need to work on the route or the request pattern. To examine the service's real behavior, website speed testing and interpreting the result helps you understand whether the slowness is from the limit or from somewhere else.
Should rejected requests count toward the counter?
No. If they count, a client that has fallen into a retry loop can never get out, and the limit becomes a permanent outage.
Count 429 requests for monitoring, but don't advance the limit counter with them. Implement this separation in the same layer that does the counting.
The next step is simple: pull the 99th percentile of requests per minute per token from the logs, set the limit slightly above it, and at that same moment add the RateLimit-Remaining and Retry-After headers to the response. Without these two headers, whatever limit you set looks like a random failure from the consumer's perspective. For implementation details on the web server side, see the documentation and knowledge base and the ServerNet blog.