Catalog My learning Blog
Application Security for Developers: Secure Coding Course · Phase 4 · Lesson 24 · 3 min read

Rate Limiting, Bot Protection and DDoS Mitigation

On this page
  1. Bots, abuse and automated traffic
  2. Stopping credential stuffing with layered rate limits
  3. Bot detection that doesn't punish real users
  4. DDoS mitigation: network vs application layer
  5. Check yourself

Bots, abuse and automated traffic

A large share of web traffic comes from bots. Some are fine (search engines, monitoring). Others try leaked passwords against your login page, scrape prices, create fake accounts, buy up limited stock, or try to knock you offline. Each needs a different defense.

Stopping credential stuffing with layered rate limits

Attackers take username and password pairs leaked from other sites and try them against yours, spread across thousands of IP addresses so each one sends only a few requests. A single per-IP limit misses this. Layer several limits:

Code
// Login: several independent limits, each catching a different pattern
await limiter.consume(`ip:${req.ip}`, { points: 20, window: '5m' });             // one source hammering
await limiter.consume(`account:${normalize(email)}`, { points: 5, window: '15m' }); // one account targeted from many IPs
await limiter.consume('login:global', { points: 5000, window: '1m' });             // an overall surge
// Beyond the limits: require a CAPTCHA or other challenge, rather than blocking outright
  • Per account catches distributed guessing against one user. Respond with a challenge or a delay rather than locking the account, or attackers can lock out real users on purpose.
  • Per IP (and per network range) catches single sources.
  • Global catches large campaigns: alert a human.
  • Check new and changed passwords against breached-password lists (Day 2), and offer MFA and passkeys (Day 3), which make stuffed credentials useless.

The API design course covers rate-limiting algorithms and the 429 response.

Layered limits per IP, per account and globally, bot detection at the edge, and DDoS absorption by a CDN protect an application from different kinds of automated abuse.
Layered limits per IP, per account and globally, bot detection at the edge, and DDoS absorption by a CDN protect an application from different kinds of automated abuse.

Bot detection that doesn't punish real users

  • Bot management at the edge (Cloudflare, AWS WAF Bot Control, Akamai, Fastly) scores requests using browser fingerprints, behavior and reputation.
  • Challenges for suspicious requests: invisible proof-of-work challenges (Cloudflare Turnstile, Friendly Captcha) are kinder to real users than image puzzles.
  • Business-level limits: one account per phone number, purchase limits per customer, velocity checks on sign-ups.
  • Watch the outcomes: failed-login rates, sign-up spikes from one region, checkout attempts per card.

Avoid relying only on IP reputation or User-Agent; both are easy to fake or rotate.

DDoS mitigation: network vs application layer

LayerAttackWho defends
Network (L3/L4)Volumetric floods (UDP, reflection), SYN floodsYour provider or CDN's scrubbing capacity (AWS Shield, Cloudflare); you can't absorb terabits yourself
Application (L7)Floods of expensive but legitimate-looking requests (search, login, uncached pages)WAF rate rules, caching, challenges, and your application's limits

What you control:

  • Put a CDN or DDoS protection in front and keep origin IP addresses private, so attackers can't bypass it.
  • Cache aggressively so attack traffic hits cheap responses.
  • Protect expensive endpoints with stricter limits and challenges.
  • Autoscale with caps, so an attack doesn't become an unlimited bill.
  • Shed load gracefully: keep critical functions working when overloaded (see the distributed systems course).
  • Have a runbook and know how to reach your provider's response team before you need them.

Check yourself

Attackers spread attempts across thousands of IP addresses, so each stays under the limit; per-account and global limits catch the pattern.

Lockouts let attackers deny service to real users on purpose, just by failing logins for their accounts. A challenge, like a CAPTCHA or email confirmation, slows bots without blocking the owner.

If attackers know them, they can send traffic directly to the origin and bypass the CDN's DDoS protection and WAF. Allow inbound traffic to the origin only from the CDN's address ranges.

Trying username and password pairs leaked from other sites, relying on password reuse. Breached password checks, MFA and rate limits are the main defenses.

It sends valid-looking HTTP requests to expensive endpoints, so it can't be filtered by traffic volume alone; rate limits, caching and bot detection help.