Lesson 8 of 14 · 18m
Rate Limiting Is a Boundary Defense
Token buckets at the edge absorb volumetric abuse; app-level limits keyed correctly stop credential stuffing at the login route.
Rate limiting is a budget, not a wall. The canonical mechanism is a token bucket: capacity B, refilled at r tokens per second — a burst up to B is fine, sustained traffic above r earns a 429 with Retry-After. Fixed windows and sliding logs are approximations of the same idea with different edge cases, and the fixed window's edge case is literal: with a 100-per-minute window, a client can fire 100 requests at :59 and 100 more at :00 — double your intended limit inside the boundary instant. A sliding-window log (Redis sorted set or INCR on bucketed keys) costs more to count what it refuses to approximate.
Where the limit lives
Two layers, two jobs. The edge — Cloudflare, your WAF, nginx limit_req — absorbs volumetric abuse. nginx spells the bucket limit_req zone=api burst=10 nodelay: a declared burst and one config line. GitHub's February 2018 DDoS peaked at 1.35 Tbps of memcached amplification traffic; no application-layer counter survives that, and volumetric floods are an edge problem by definition. The app layer enforces business rules: five login attempts per minute per account, three signups per IP per day, per-user quotas on expensive endpoints. express-rate-limit, Hono's limiter middleware, or a Redis INCR+EXPIRE counter — the mechanism is trivial. The decisions are not: weight by cost, not count — a search endpoint and a health check should not share a quota.
Keying is the hard part
A limit keyed on IP alone punishes the innocent: carrier-grade NAT puts a city's phones behind one address, and one shared office blocks itself out. Key on what the abuse actually targets — per-account for login (the attack is against the password), per-IP for unauthenticated endpoints, composite keys for the gray cases. And get the key right mechanically: req.ip is your proxy's address until trust proxy is set, and the client-writable left end of X-Forwarded-For is attacker data, as the proxy lesson covered.
Distributed limiting adds its own trap: GET then SET on a counter is a race — two limiters both read 99 and both allow. INCR is atomic for a reason; if you must do multi-step logic, a Lua script in Redis keeps it inside one round-trip.
The endpoint that needs it most
Your login route is also your most expensive route. bcrypt's entire purpose is to cost CPU — 80–300 ms per verify at sane cost factors. An unthrottled login endpoint is therefore two attacks in one: credential stuffing at scale (Collection #1 put 773 million email/password pairs into circulation in 2019) and free CPU exhaustion, where every guess makes you pay the hashing bill. Rate-limit it per-account and per-IP, return identical timing and messages for wrong-password and no-such-user (the different response is a user-enumeration oracle), and log the failures — they are your early warning. When limits alone don't stop it, the escalation is a challenge — a Turnstile-style proof on signup and login — raising the attacker's cost instead of yours.
Failure posture
Decide fail-open versus fail-closed by asset. If the limiter store dies, does it block everyone or no one? For most API routes fail-open with an alarm is defensible — a down limiter should not amplify an outage — but on auth endpoints fail-closed is the correct call, because the open failure is the attack succeeding.
The response contract
429 is not complete without Retry-After — seconds until the client may retry — and the RateLimit-*/X-RateLimit-Remaining family tells well-behaved clients their budget before they spend it. A limiter that gives no signal teaches clients to hammer harder.
// Runnable: a token bucket in ten lines
let tokens = 5;
function allow() { if (tokens >= 1) { tokens--; return true; } return false; }
console.log(allow(), allow(), allow(), allow(), allow(), allow(), "| left:", tokens);
// true true true true true false — the burst drains the bucket.The practice
Edge for volume; app for business rules; 429 plus Retry-After; composite keys; auth fails closed; never build security decisions on a header the client can forge.
Was this lesson helpful?
Keep reading
Related articles from the SudoSecurity blog.
Node Runs TypeScript Now. Here's Exactly What It Strips, What It Refuses, and What It Costs
Node 24 and 26 run .ts files with no build step and no flags, by replacing types with whitespace. I ran eleven test cases, the TypeScript 7 compiler and tsx against both versions: enums, parameter properties and type imports without the type keyword all break, type errors run anyway, and...
Node.js vs Deno vs Bun: Picking the Right JavaScript Runtime in 2026
Three JavaScript runtimes. Three different philosophies on what the developer experience should feel like. Here's how they compare on performance, ecosystem, tooling, and the only decision that actually matters for production.
What is Bun? The JavaScript Runtime That Made npm Install Fast
Bun is a JavaScript runtime, package manager, bundler, and test runner in a single binary written in Zig. It's also the fastest thing to happen to JavaScript tooling in years — and it runs your existing Node.js code.
What is Deno? Ryan Dahl's Second Attempt at a JavaScript Runtime
In 2018, Ryan Dahl stood at JSConf EU and apologized for Node.js. Then he built Deno to fix the things he got wrong. Here's what changed, what the security model actually looks like, and whether switching is worth it.