Limit how many requests one user can make, so nobody can overload the system.
Rate limiting controls how many requests one user (or app) can make in a period of time. Extra requests are refused.
A rate limiter sits in front of your API. For every request it asks one question: βHas this caller already used too much?β If yes, it says no before the request reaches your servers.
There are four classic algorithms. Each one counts in a different way.
| Algorithm | How it works | Good | Not so good |
|---|---|---|---|
| Fixed window | Count per time block (for example per minute). Reset at the start of each block. | Very simple, tiny memory | Edge burst: 100 at 12:00:59 plus 100 at 12:01:00 = 200 in 2 seconds |
| Sliding window | Count requests in the last N seconds from now (a log of timestamps, or two weighted counters). | No edge burst, smooth and fair | Uses more memory or more math |
| Token bucket | A bucket holds tokens. A request takes one. Tokens refill at a fixed rate up to a maximum. | Allows short bursts, simple, very popular | Two numbers to tune (size and refill rate) |
| Leaky bucket | Requests join a queue and leave at a constant rate. A full queue drops new requests. | Smooth, steady output to the backend | Bursts wait or get dropped; old requests can be stuck behind the queue |
| Header | Meaning |
|---|---|
| Retry-After: 30 | Try again after 30 seconds |
| X-RateLimit-Limit: 100 | Your limit in this period (a common header name, not a strict standard) |
| X-RateLimit-Remaining: 0 | Requests you have left |
| X-RateLimit-Reset: 1700000060 | When your count resets (often a Unix time) |
| Term | Meaning in easy words |
|---|---|
| Rate limit | The maximum number of requests allowed in a time period |
| Throttling | Slowing a caller down (or delaying their requests) instead of refusing them |
| 429 Too Many Requests | The HTTP status code for βyou went over the limitβ |
| Retry-After | A header that tells the client how long to wait |
| Burst | A short spike of many requests at once |
| Window | The time period that is being counted |
| Token bucket | A bucket that refills with tokens; each request spends one |
| Leaky bucket | A queue that lets requests out at a steady speed |
| Quota | A longer-term cap, for example 10,000 calls per month |
| Fail open / fail closed | If the limiter breaks: let everything in (open) or block everything (closed) |
| Choice | We gain | We lose |
|---|---|---|
| Limit per IP | Works for anonymous users | Many people can share one IP (office, school, mobile network) |
| Limit per user or key | Fair and exact | Needs login or a key first |
| Strict limit | Strong protection | More blocked real users |
| Fail open | Users are never blocked by a broken limiter | No protection while it is broken |