Skip to content

Lesson 5 of 9 · 25m

Rate Limiting and Abuse Control That Matches Cost

Throttle by meaningful identity, choose limit algorithms deliberately, and avoid breaking legitimate bursts while still absorbing hostile traffic.

Rate limiting is not one feature; it is a pricing system for requests. You are deciding how much concurrency, bandwidth, CPU, and downstream work one identity can consume. If the only answer is a global requests-per-second number, you have not designed protection. You have installed a random outage generator with better marketing.

Choose the identity before the number

A client is not always an IP address. It may be IP plus account, API token, ASN, user agent, route class, or unauthenticated endpoint. NAT means hundreds of real users can share one IP. IPv6 means one bot can rotate through an effectively unlimited prefix. Use layered controls: coarse edge filtering, route-specific application limits, and downstream quotas.

Nginx token buckets

limit_req_zone $binary_remote_addr zone=login_per_ip:10m rate=5r/m;
limit_req_zone $http_authorization zone=api_per_token:10m rate=30r/s;

server {
    location /api/login {
        limit_req zone=login_per_ip burst=10 nodelay;
        proxy_pass http://app_backend;
    }

    location /api/ {
        limit_req zone=api_per_token burst=20 delay=10;
        proxy_pass http://app_backend;
    }
}

rate is admission over time, burst is queue capacity, delay permits a defined burst before delaying, and nodelay rejects immediately beyond burst. nodelay is often right for login abuse and wrong for bursty browser page loads. There is no universally correct value; there is only whether the shape matches legitimate traffic.

Concurrency and bandwidth are separate controls

limit_conn_zone $binary_remote_addr zone=addr_conn:10m;

location /downloads/ {
    limit_conn addr_conn 20;
    limit_rate_after 10m;
    limit_rate 512k;
}

Connection limits protect worker slots. Rate shaping protects bandwidth. Neither understands application cost. A cheap static download and an expensive export are both one request to the web server until you classify them.

Apache leans on modules and application cooperation

Modern Apache deployments often implement throttles with mod_ratelimit, mod_evasive, external WAF rules, or upstream application middleware:

<Location "/downloads/">
    SetOutputFilter RATE_LIMIT
    SetEnv rate-limit 400
</Location>

mod_evasive can help with primitive HTTP floods, but it is not a complete abuse system. It keys heavily on IP behavior and can punish shared networks. Treat it as a blunt instrument, not architecture.

Fail closed for abuse, fail open for users — consciously

During limiter failure, what happens? A Redis-backed app limiter outage might disable protection. An Nginx zone misconfiguration might block everyone. Decide per route. Login protection can fail closed or degrade to CAPTCHA. Public content often favors availability. Payment authorization must never become more permissive because a defense layer failed.

Measure before tightening

Graph accepted, delayed, and rejected requests per zone. Inspect the top offenders and the legitimate p99 burst shape. A control that rejects 0.1% of users continuously is not quiet; it is a slow-motion support incident. Start in log-only or permissive mode where possible, then enforce.

Field notes

  • $binary_remote_addr memory usage is lower than string keys, but identity correctness matters more than memory efficiency.
  • CDN traffic makes direct client IP meaningless unless you restore it carefully from trusted proxy headers.
  • Throttle expensive endpoints separately; a global API limit rewards attackers who choose the most costly operation.
  • Return consistent retry metadata where appropriate, but do not provide attackers exact counter introspection they can tune against.

Was this lesson helpful?

Sign in to rate