Bookmarks

Rate limiting

A mechanism that limits the number of requests a service will accept over a chosen interval: for example, no more than 100 requests per minute from a single client.

Also: throttling, request throttling

checked 8 Nov

All terms

Rate limiting protects a service from overload and abuse: requests that come in too often are dropped or queued, while the rest are handled as usual. Limits are set per client, IP address, API key or endpoint.

How token bucket and leaky bucket work

The classic schemes are token bucket and leaky bucket. In token bucket, tokens are added to the bucket at a constant rate; each request consumes one token, and if the bucket is empty the request is rejected. A full bucket allows a short burst: at a rate of 10 tokens per second and a capacity of 20, a client can make 20 requests in a row, and after that no more than 10 per second.

AlgorithmHow it worksWhat it provides
Token buckettokens are added at a constant rate, each request consumes one tokenallows bursts up to the bucket capacity
Leaky bucketrequests are queued and processed at a constant rate, the queue is boundedeven flow: processing strictly at the set pace

What the client sees when the limit is exceeded

When the limit is exhausted, the server responds with the status 429 Too Many Requests — added by RFC 6585 in 2012 — and may send a Retry-After header: a number of seconds or a date when the request can be repeated. Some tools respond differently by default: nginx returns 503 Service Unavailable, and the status is changed with the limit_req_status directive. The client is expected to retry with exponential backoff and respect Retry-After.

httpResponse when the limit is exceeded
HTTP/1.1 429 Too Many RequestsRetry-After: 30 {"error": "rate limit exceeded", "retry_after": 30}

Where it is used

  • API gateways and reverse proxies: nginx with the limit_req directive, per-API-key quotas in gateways
  • Protection against brute force and abuse: login attempts, form submissions, registration
  • Limiting expensive operations: search, text generation, calls to third-party paid APIs

Sources

the term is written from these pages

Verified 8 Nov

Not to be confused with

what it is mixed up with and how it differs

  • circuit breaker

    Rate limiting restricts clients on the service side, while a circuit breaker cuts off calls to a failing neighbour on the client side.

  • back-pressure

    Back-pressure asks the source to send data more slowly, while rate limiting at the entrance to a service drops excess requests or queues them.

In depth in the references

where the topic is covered in detail