Skip to main content

Web and APIs

What is Rate Limiting?

Definition

Rate limiting caps how many requests a client may make in a given period, protecting a service from overload, abuse and runaway cost.

Also known as: Throttling, API quota

Rate Limiting in more detail

Every service has a capacity, and without a limit a single misbehaving client — a retry loop, a scraper, a credential-stuffing script — can consume all of it. A rate limiter counts requests per key (an API token, a user id, an IP address) over a window and rejects the ones above the threshold, normally with HTTP 429 Too Many Requests plus a Retry-After header telling the caller when to come back.

The counting algorithm matters more than it sounds. A fixed window is trivial to implement but allows a double burst across the boundary, since a client can spend its whole quota at the end of one window and again at the start of the next. A sliding window smooths that. A token bucket, the most common choice, refills tokens at a steady rate up to a maximum, which permits short bursts while holding the long-run average — usually what you actually want.

Choosing the key is a product decision. Limiting per IP address punishes everyone behind one corporate NAT or mobile carrier and does nothing against a distributed attacker; limiting per API key is fairer but useless for unauthenticated endpoints such as login and password reset, which is exactly where abuse concentrates. Most real systems layer several limits, and run the counters in a shared store such as Redis so every instance sees the same tally.

Rate Limiting example

HTTP/1.1 429 Too Many Requests
Retry-After: 30
RateLimit-Limit: 1000
RateLimit-Remaining: 0
RateLimit-Reset: 30

{ "error": "rate_limited",
  "message": "1000 requests per hour exceeded" }

# Token bucket, in words:
#   capacity 100 tokens, refill 10 per second
#   each request costs 1 token
#   no token available -> 429
# Allows a 100-request burst, then settles to 10/sec.

Rate Limiting — common questions

What status code should a rate-limited request return?
429 Too Many Requests, with a Retry-After header giving either a delay in seconds or an HTTP date. Some services also return RateLimit-Limit, RateLimit-Remaining and RateLimit-Reset so clients can slow down before being blocked.
What is the difference between rate limiting and throttling?
They are often used interchangeably. Where people distinguish them, rate limiting rejects requests over the cap outright while throttling slows them down — queueing or delaying rather than refusing.
How should a client handle being rate limited?
Respect Retry-After, then back off exponentially with jitter so retrying clients do not all return in lockstep. Retrying immediately in a tight loop is the single fastest way to get an API key suspended.

Keep reading

Every definition in the set is indexed A-Z in the developer glossary, and the cheat sheets cover the syntax once you know what you are looking for.

Browse the A-Z glossary