whitehatstoic
Rate Limiting for APIs: What It Is and Why APIs Need It
Rate limiting counts each caller's requests and refuses the extras with HTTP 429. What it counts, for whom, and what a request counter cannot stop.
whitehatstoic
A software, security and AI safety practice: it builds web and mobile apps, finds and fixes weak spots in web apps and AI systems, and does AI safety research.
Rate limiting is a rule a server applies to each caller: it counts that caller's requests over a period of time, and once the count passes a set number it refuses the rest until the period ends. APIs need it because every request spends something the owner has to supply (processor time, memory, bandwidth, or a charge from another company), and without a limit one script can spend all of it.
whitehatstoic is a software, security and AI safety practice, and the question of how many requests an API should accept belongs to the security part of that subject. The definition is the easy part. The hard parts are what gets counted, for whom, and what the server says when the count runs out.
What rate limiting does to a single request
Every request to a limited API passes through the same five steps.
- The request arrives, and the server works out who sent it: an account, an API key, a cookie or a network address.
- The server looks up that caller's counter for the current period.
- If the counter is under the limit, the server adds to it and handles the request.
- If it is not, the server refuses the request before doing any of the work, usually with the HTTP status 429.
- When the period ends, or the counter drains, the caller is served again.
The order of step 4 is the whole point. A refusal sent after the database query has already run saves nothing, so the count has to be checked first.
Where the 429 answer comes from
The status code has a date and two authors. RFC 6585, "Additional HTTP Status Codes", was published by the Internet Engineering Task Force in April 2012 under the names M. Nottingham and R. Fielding. Its section 4 defines 429 Too Many Requests: the code "indicates that the user has sent too many requests in a given amount of time", and the document itself adds the words "rate limiting" in brackets.
The same section says the response should explain the condition and may carry a Retry-After header. Its sample response sets Retry-After: 3600 and tells the reader the site allows "50 requests per hour to this Web site per logged in user". It also says a 429 response must not be stored by a cache.
Retry-After itself is defined in RFC 9110, dated June 2022. The value is either a date or a number of seconds, so Retry-After: 120 asks for a wait of two minutes. RFC 9110 keeps a different code, 503 Service Unavailable, for a server that "is currently unable to handle the request due to a temporary overload or scheduled maintenance". This is the distinction people most often get wrong. A 503 says the server is in trouble. A 429 says one caller has gone past its share, while the server may be perfectly healthy.
Two ways to count: the fixed window and the token bucket
The "50 requests per hour" of RFC 6585 is the simplest counter, a fixed window. Count from the top of the hour, refuse request 51, start again at the next hour. Simple arithmetic shows its flaw. A caller who sends 50 requests at 10:59 and 50 more at 11:00 has obeyed the rule and still sent 100 requests in two minutes, twice the hourly figure.
The token bucket removes that edge. It comes from packet networks, where it checks that traffic keeps to limits on bandwidth and burstiness. Two numbers define it: a rate r and a size b. One token is added every 1/r seconds, the bucket never holds more than b tokens, and each request either removes a token or is refused because none is left. Over a long run the caller cannot beat the rate r, and the largest burst is set by b.
Take one token every 72 seconds and a size of 10. The hourly average is still 50, because 3,600 divided by 72 is 50. But the burst at 10:59 is capped at 10 requests, and the two minutes around 11:00 admit 12 at most: the 10 saved tokens plus the one or two that arrive in 120 seconds. The fixed window allowed 100.
A password reset that sends 40,000 text messages
The clearest case for a limit is in the OWASP API Security Top 10, under the entry API4:2023, Unrestricted Resource Consumption. A social network lets a user reset a password with a code sent by SMS. Each reset makes the back end call an outside provider, and the provider charges $0.05 per call. An attacker writes a script that sends the reset request "tens of thousands of times", and the company loses "thousands of dollars in a matter of minutes".
Put a number on it. Suppose the script makes 40,000 calls, a figure chosen here to fit the scenario's wording. At $0.05 each that is $2,000. Now add a limit of 3 reset messages per phone number per hour. The same script aimed at one number gets 3 messages sent and 39,997 refusals, and the cost falls to $0.15.
Follow it one step further. The attacker changes the script to name a different phone number on every call. A counter kept per number never reaches 3, and the $2,000 is back. So the limit needs a second key, the caller. With 10 reset requests per caller per hour as well, one caller can cause at most $0.50 an hour, whatever numbers it names. A limit protects only the thing it is keyed on.
The OWASP page asks for exactly this kind of narrow rule: a limit on how often "a single API client/user can execute a single operation", with password recovery as its example, and spending limits set with every outside provider.
Requests are not the same as work
The strongest objection to rate limiting is on the same OWASP page, in its second scenario. An API built on GraphQL lets a user upload a profile picture, and the server then makes several thumbnails, which takes a lot of memory. The API has "a traditional rate limiting protection". The attacker sends one request that contains 999 upload operations. The counter sees one request. The server runs out of memory and stops answering everyone.
A request counter records how many requests arrived, not how much work each one carries. The name of the entry has moved the same way. The 2019 edition called it "Lack of Resources & Rate Limiting". The 2023 edition calls it "Unrestricted Resource Consumption", and rate limiting is now one of six preventions on its list, next to maximum sizes for incoming data, a cap on records per page, limits on memory and processor time, the per-operation limit and the spending limits.
Whose requests are being counted
RFC 6585 is frank about what it does not settle: "this specification does not define how the origin server identifies the user, nor how it counts requests". The server may count per resource, across the whole server or across a set of servers, and may identify the user by credentials or a cookie.
The MDN page on 429 describes common practice: restrictions are typically "based on a client's IP" but can be specific to users or applications when requests are authenticated or contain a cookie. Each key has a gap. OWASP notes that the requests can come "from a single local computer or by using cloud computing resources". A caller who rents many machines arrives from many addresses, and a counter per address gives each one a full allowance. A counter per account holds only where a request carries an account, and a login or a password reset arrives before anyone is identified. The endpoints most worth limiting are the ones where the caller is hardest to name.
What the RFCs and OWASP leave open
Three things are still unsettled in the sources themselves.
- Whether to answer at all. RFC 6585 warns in section 7.2 that "responding to each with a 429 status code will consume resources", that servers are not required to use the code, and that "it may be more appropriate to just drop connections".
- How a caller learns its allowance. RFC 6585 and RFC 9110 define one signal, Retry-After, and it arrives only after a refusal. Neither defines a field that says how many requests are left.
- What the number should be. OWASP says rate limiting "should be fine tuned based on the business needs" and that some endpoints need stricter policies. It gives no figure, and neither RFC does.
Frequently asked questions
What does HTTP 429 mean?
It means the caller sent too many requests in a given amount of time. The code was defined in RFC 6585 in April 2012.
How long should a client wait after a 429?
As long as the Retry-After header says, when the response has one. The value is a date or a number of seconds, and the header is optional.
What is the difference between 429 and 503?
A 429 is about one caller, who has passed its limit. A 503 is about the server, which cannot handle requests for the moment because of overload or maintenance.
Does rate limiting stop a denial of service attack?
Only in part. Sending each refusal uses resources, and a single request that asks for a great deal of work is counted as one.
Rate limiting as one limit among several
Rate limiting answers one question: how often may this caller ask? An API also has to settle how much one request may ask for, how long it may run, and how much money it may cause to be spent somewhere else. OWASP counts an API as exposed when even one of those limits is missing or set wrongly, and its wording covers both directions: "too low/high". A limit set too low turns away real users, and one set too high stops nobody.
The counter is the cheap part. What decides whether rate limiting holds is the key it counts by, the number it carries, and whether the limits on size and spending stand beside it.
whitehatstoic
Build it right. Test it thoroughly. Keep it safe. Visit whitehatstoic.com


