Access & APIsUpdated 2026-11-20

Rate limiting

How I think about request limits: what they protect, why the counters must be shared, and which endpoints get one first.

A doorman who stops anyone hammering the bell, so real customers get in.

That is rate limiting. It caps how many requests one client can make in a window of time. Real customers keep getting in, and the one script pressing the bell a thousand times a second does not.

How it works

A rule names a limit and a window. When a client goes over, the server refuses the extra requests.

Code
Limit: 100 requests / minute / client

request 1..100   → 200 OK
request 101      → 429 Too Many Requests
next window      → counter resets

The refusal is HTTP 429. A good response also says when to retry, so a well-behaved client backs off instead of hammering harder.

Why I add one

  • Abuse. Stops scraping and brute-force guessing.
  • Protection. A flood at the front should not reach the database behind it.
  • Cost. Some endpoints call a metered third-party service. A loop in someone's code can turn into a bill.
  • Accidents. A buggy client with a retry loop looks exactly like an attack. The limit treats both the same.

Where limits go first

  • Sign-in, password reset and verification endpoints, to stop brute force.
  • Expensive endpoints: search, report generation, anything that calls an AI model.
  • Any public or semi-public API.

The counters must be shared

This is the part that surprises people. If each app instance counts on its own, a client limited to 100 a minute can send 100 to each of three instances and get 300. The counter has to live somewhere every instance can see it, which is why a shared store such as Redis is the usual home. That ties directly to stateless architecture: the instances hold no memory of their own, so the count lives outside them.

Common counting methods:

  • Fixed window. Count per minute, reset on the minute. Simple, but a client can burst at the boundary.
  • Sliding window. Smooths the boundary at the cost of more bookkeeping.
  • Token bucket. Allows short bursts while holding a steady average.

Where the check runs is a choice too. It can sit in an API gateway in front of everything, or in the app for rules that depend on who the user is.

What the client sees

A limit is part of the API contract, so document it. The response should carry the limit, how much is left and when the window resets. Clients that read those values can slow down before they are refused. Without them, a client can only guess, and a guessing client with a retry loop makes the problem worse. A short, fixed retry hint is better than silence, and logging every refusal with the client identity makes a spike readable as a pattern.

Where I used it

I haven't shipped this in a public project yet.

IMS lists Turnstile in its auth stack, which is a bot check, and I will not stretch that into a claim about request limits. No case study of mine documents a rate limiter, so this note is the design I would follow, not a result.

Trade-offs

  • Shared state is a dependency. The counter store is now on the request path. If it is down, you must choose: let everyone through, or refuse everyone. For most endpoints, failing open is reasonable. For sign-in, think harder.
  • Identifying the client is hard. Limit by IP and you punish a whole office behind one address. Limit by account and an attacker can try one password across thousands of accounts. Often you need both.
  • Limits that are too tight look like outages. A legitimate burst, such as a page that fires many calls at load, can trip a low limit. Set limits from measured traffic, not a guess.
  • Not a security boundary alone. A limiter slows an attacker. It does not replace authentication or input checks.
  • Internal calls. For trusted service-to-service traffic a limit mostly adds friction, though a generous one still works as a safety net.

A useful rule: put a limit on every endpoint that is public, expensive or guards a login, and keep the counter somewhere all instances share.