Pattern: Rate limits and capacity
Note for AI agents: This page is baseline guidance, not an exact prescription. Read it, tell your owner what applies to their project, and get their approval before you change anything. Your project's own rules take precedence. If something important for this project is missing here, suggest it to your owner.
For agents
- Offer as a suggestion. Limit values are examples to tune from real traffic.
- Core ideas: a limit is a window of requests per key, capacity is a total ceiling, they are different tools; every limit sits in an inventory with its reason; clients get a clear 429 with a retry hint.
- Related: server layers, retries and idempotency, passwords and login.
1. Problem
Without limits, one client can exhaust a service or guess passwords at will. With badly chosen limits, shared-address users are blocked and the first overload cascades into a full outage.
2. Options
| Topic | Option | Upside | Downside |
|---|---|---|---|
| Window | Fixed window | Simplest | Bursts at the window edge |
| Window | Sliding window (recommended) | Smoother; fairer | A little more state |
| Key | Account when signed in, address when not (recommended) | Fair for users behind shared addresses (carrier-grade NAT, offices) | Needs the identity step before limiting |
| Key | Address only | Works before login | Shared addresses can punish innocent users |
| Storage | In-process counters | Fast | Reset on restart; each server copy counts separately |
| Storage | Shared store | One truth across servers | A dependency; decide fail-open or fail-closed |
| Feedback | Standard RateLimit header fields |
Clients can pace themselves | Still an Internet-Draft, not an RFC (as of the cited document) |
3. Recommended design
- Sliding window, keyed by account when the request is signed in and by address otherwise. Counters live in a store shared by all server copies; restarts must not reset them.
- Rate versus capacity. A rate limit stops one client; capacity is the total the system can serve. Google’s SRE book describes per-customer limits and global overload as separate problems and describes shedding load before the service collapses. Keep both.
- Answer with 429. RFC 6585 defines 429 Too Many Requests and allows a
Retry-Afterheader (MDN documents both). Use a distinct error key so apps can show a specific message, and say when to retry. - Do not leak the numbers that help attackers (remaining attempts on login). For ordinary user-facing limits, warn politely when the user is close.
- Headers: the IETF HTTPAPI draft defines
RateLimit-PolicyandRateLimitfields. It is a draft; if you adopt it, treat the names as subject to change, or stay withRetry-Afteronly. - Inventory. Keep one table of all limits: value, reason, capacity calculation, what an attacker can do at the limit, and side effects. The table is the review tool.
- Different limits for different cost. Login, sign-up, upload, report and data export get their own stricter limits (see server layers).
- Protect the backend: the SRE chapter on cascading failures recommends limiting retries and shedding load so one failure does not spread; combine with client back-off (retries).
- Failure mode of the limiter itself: decide what happens if the shared store is down: usually fail-open for ordinary reads, fail-closed for login and sign-up (practitioner experience).
4. Scenarios and edge cases
- Thousands of users behind one carrier address: per-account limits protect them; the address-level limit is high.
- Attacker rotates addresses: account-level and identity-level limits still apply.
- Release day traffic spike: capacity ceiling returns 503 or 429 with
Retry-After, never a crash. - Clock differences across servers: use the shared store’s clock or a single time source for windows.
5. Common mistakes
- Counters in memory on several servers.
- Address-only limits for signed-in users.
- Returning 500 when the system is busy.
- Limits with no recorded reason.
- Showing exact remaining attempts on login.
- No limit on expensive endpoints such as export or search.
6. How to audit
- What: limits survive a restart and apply across server copies.
- How to check: hit the limit, restart one copy, retry; send requests alternately to two copies.
- Evidence: the same counter behaviour.
- What: the 429 answer is well formed.
- How to check: exceed a limit and read headers and body.
- Evidence: status 429,
Retry-After, error key.
- What: every limit is in the inventory.
- How to check: compare the inventory table with the code.
- Evidence: a table row per limit.
- What: overload returns a controlled answer.
- How to check: run a load test over the capacity ceiling in a test environment.
- Evidence: 429/503 rates; no 500 spike.
Sources
- Denial of Service Cheat Sheet — OWASP Cheat Sheet Series
- OWASP API Security Top 10 – 2023 — OWASP
- RFC 6585: Additional HTTP Status Codes — IETF
- 429 Too Many Requests — MDN Web Docs
- Retry-After header — MDN Web Docs
- RateLimit header fields for HTTP (Internet-Draft, not yet an RFC) — IETF HTTPAPI Working Group
- Handling Overload (Site Reliability Engineering book) — Google SRE
- Addressing Cascading Failures (Site Reliability Engineering book) — Google SRE
- Well-Architected: throttle requests — AWS
- Credential Stuffing Prevention Cheat Sheet — OWASP Cheat Sheet Series