(Note · · 5 min read)
Rate limiting that protects you instead of annoying everyone
Most rate limiting is added after an incident, keyed on IP address, and set to a number somebody guessed. It inconveniences legitimate users behind shared networks, barely slows a determined attacker, and leaves the expensive endpoints just as exposed as the cheap ones.
By Md. Habibur Rahman Shohel, full-stack & AI product engineer in Dhaka
Key on identity where you have it
An IP address is a poor identifier: an office or a mobile network shares one among many people, while anyone who cares can use many. Where a request is authenticated, limit per account, which is the thing you actually want to bound.
Keep an IP limit as the outer layer for unauthenticated traffic, but treat it as crowd control rather than protection, and set it loose enough that a shared network is not punished.
Limit the expensive things specifically
One global limit treats a cached page and a report that runs for thirty seconds as equivalent. They are not. Give the costly endpoints their own, much tighter limits: anything that sends mail, triggers a payment, starts a model call or generates a document.
For work with real unit cost, such as calls to a model provider, consider limiting the cost rather than the count. Ten cheap requests and one enormous one are not the same load, and a request-count limit cannot tell them apart.
Protect the endpoints that leak
Login, password reset, signup, promotional codes and anything that confirms whether a value exists need their own limits, because the attack is not volume for its own sake — it is guessing. Limit per account as well as per source, so distributing an attack across addresses does not evade it.
Keep the refusal uninformative. A rate-limit response that distinguishes between an account that exists and one that does not has given away the thing the attacker was asking.
Refuse in a way clients can act on
Return the status code for too many requests, say when to try again, and tell callers what their limit and remaining allowance are. A client that can read that will back off correctly; one that gets an opaque error will retry immediately and make things worse.
Allow short bursts. Real usage is uneven — a page that fires several requests on load is normal — so a limit that permits a burst while bounding the sustained rate rejects far less legitimate traffic than a flat per-second cap.
Count somewhere shared, and watch it
Counters held in process memory are per instance, so the real limit is multiplied by however many are running, and resets whenever one restarts. Keep them in a shared store if the number is meant to mean anything.
Then log rejections with enough detail to tell an attack from a misconfigured client of your own, which is what most sudden spikes turn out to be. Start by recording what would have been blocked without blocking it, so you can choose a limit from data rather than from a guess.
Common questions
Should rate limits be per IP address?
Only as an outer layer for unauthenticated traffic. Offices and mobile networks share one address among many users while an attacker can use many, so limit per account wherever the request is authenticated.
Should all endpoints share one rate limit?
No. A cached page and a thirty-second report are not equivalent load. Give tight, specific limits to anything expensive — sending mail, triggering payments, calling a model, generating documents — and consider limiting cost rather than request count where unit costs vary.
How should an API respond when a limit is hit?
With the too-many-requests status, a retry-after time, and headers describing the limit and remaining allowance, so clients can back off correctly. Keep the response uninformative about whether an account exists, or the refusal itself leaks what an attacker asked.
Why do my rate limits not match the configured number?
Usually because counters are held in process memory, so the effective limit is multiplied by the number of running instances and resets when any restarts. Keep counters in a shared store if the number is meant to be meaningful.