# Rate limiting

> Capping how many requests someone can make in a given time window.

- Level: Intermediate
- Topics: Security, Software
- URL: https://tidbit.legios.com.ar/rate-limiting/
- In Spanish: [Rate limiting](https://tidbit.legios.com.ar/es/rate-limiting/)

**Rate limiting** is a rule like "at most 100 requests per minute per user" enforced by a server or an API. Requests under the limit go through normally. Requests over it are rejected, usually with the HTTP status `429 Too Many Requests` and often a `Retry-After` header that says when to try again.

It protects in several directions at once. It slows down attacks that rely on volume, like a brute-force attack on a login form or a scraper copying a whole catalog. It keeps one buggy script or greedy customer from eating the capacity everyone shares. And it is how paid APIs enforce their plans: the free tier gets fewer calls per hour than the paid one, counted per API key.

Under the hood there are a few classic algorithms. A fixed window counts requests per calendar minute. A sliding window smooths out the burst at the edge of each minute. A token bucket refills a stash of tokens at a steady rate and lets each request spend one, which allows short bursts but caps the average. Limits can be keyed by user, by API key or by IP address. On its own it does not stop a large DDoS, whose traffic comes from thousands of addresses, but it is a cheap first line of defense.

## An analogy

A bakery that gives out numbered tickets and serves five customers per minute. If you keep pushing to the counter, the clerk tells you to come back later; the bread lasts for everyone.

## Keep in mind

- Over the limit, servers usually answer 429 Too Many Requests.
- Limits can be counted per user, per API key or per IP address.
- Token bucket and sliding window are the most common algorithms.
- Good clients wait and retry instead of hammering harder.

## Go deeper

- [Rate limiting on Wikipedia](https://en.wikipedia.org/wiki/Rate_limiting)
- [429 Too Many Requests (MDN)](https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Status/429)
- [Denial of Service Cheat Sheet (OWASP)](https://cheatsheetseries.owasp.org/cheatsheets/Denial_of_Service_Cheat_Sheet.html)

## Related terms

- [Server](https://tidbit.legios.com.ar/server/): A computer whose job is to answer requests from other computers, all day, every day.
- [API](https://tidbit.legios.com.ar/api/): The menu of things one program lets another program ask for.
- [HTTP](https://tidbit.legios.com.ar/http/): The request-and-response language browsers and websites speak.
- [Brute-force attack](https://tidbit.legios.com.ar/brute-force-attack/): Breaking into an account by trying passwords, one after another, until one works.
- [Bug](https://tidbit.legios.com.ar/bug/): A mistake in software that makes it behave differently from what was intended.
- [AWS account and billing](https://tidbit.legios.com.ar/aws-account-and-billing/): What an AWS account is, how you get charged, and how to avoid a surprise bill.
- [API key](https://tidbit.legios.com.ar/api-key/): A secret string that identifies your app when it calls someone else's API.
- [Algorithm](https://tidbit.legios.com.ar/algorithm/): A precise, step-by-step method for solving a problem or doing a task.
