The first rate limit anyone writes is one line: five requests a minute, then a 429. It feels finished. Then I built a firewall that a group of products ask before they send a one-time password, and found out how many decisions were hiding in that one line.
One limit is four windows
"Five a minute" says nothing about the next hour. Someone patient can send five a minute all day, and on an SMS that costs money every time, patience adds up.
So the limit on sending a one-time password is four windows stacked on top of each other: one every two seconds, three a minute, ten an hour, thirty a day. The short windows stop a script. The long ones stop a person who has worked out the short ones. A real user who mistyped their number never gets near any of them.
The windows also get different voices. Hitting the two-second window is a Tuesday. Hitting the daily one means something unusual is going on, so that's the only one that emails a human.
The subject is the real decision
Every limit counts something, and choosing what it counts is choosing who shares a budget.
Count per IP, and everyone behind one office router or one mobile network shares a limit with the worst person in the building. Count per phone number, and an attacker just moves to the next number. So the firewall counts both, differently. Per number, it limits how often one number can be sent a code. Per IP, it limits how many different numbers one address tries, which is the shape of someone spraying codes at strangers. A person retrying their own number barely registers. A script walking a list hits the wall fast.
It can also count repeated payloads, the same request sent again and again, separately from new ones. Asking twice is normal. Asking forty times is a pattern.
Rejected attempts are free
The firewall checks every window first and only counts a request once it's allowed through. That sounds like a detail. It decides what happens to someone who keeps hammering a blocked form: nothing. They aren't pushed deeper into the block, and the windows run out on schedule. Punishing every rejected attempt feels tougher, but it mostly punishes real users who don't understand why the button stopped working.
Tell people the longest wait
When a request is over more than one window, the honest answer is the longest wait. Report the first window you find and you'll tell someone to try again in two seconds, when the daily window means they can't until tomorrow. "Try again later" with the wrong number in it is worse than no number at all.
Bans are the bluntest tool
I built a ladder of IP bans too: an hour, then two, then four, then eight, then a day, and permanent after enough offences, with old offences forgiven after a quiet week. It's switched off for now. An IP is often a whole office or a whole carrier's worth of phones, and a ban lands on all of them, which makes it the last tool to reach for, not the first.
Decide how it fails
A security check that fails closed takes the product down with it. If a captcha provider has an outage, should nobody be able to log in? The firewall fails open when a captcha isn't configured, and the rate limits keep working either way, because they live in its own Redis and don't depend on anyone else. Whatever you choose, it's a choice, and it should be written down as one, not discovered during an incident.
What I'd tell someone writing their first one
- Stack windows. A short one for scripts, a long one for patience.
- Choose what you count on purpose: per person, per IP, per repeat, per target.
- Don't charge people for attempts you already refused.
- When you say "try again later", mean the latest window, not the first.
- Keep bans for last, and decide in advance what happens when a dependency is down.
A rate limit is the product deciding who gets to be annoying, and for how long. Better to make that decision on purpose than let a number make it for you.