Lesson 7 of 9 · 22m
Client Contracts: 429, Retry-After, and Honest Headers
Design response semantics that tell clients what happened, when to retry, which budget failed, and how to avoid turning throttling into a retry storm.
A rate limiter can be technically correct and still create an outage because clients respond badly. The client contract is part of the control. If your API says only "slow down," automated systems will invent their own meaning: immediate retry, ten parallel retries, process restart, or infinite backoff. Good responses replace improvisation with explicit behavior.
Use status codes precisely
429 Too Many Requests means the client has exceeded an allowance. 503 Service Unavailable means the service cannot currently serve the request. They are not interchangeable:
429 policy-based rejection; retry may be appropriate later
503 capacity or dependency failure; retry only with backoff
403 policy prohibition; retrying usually will not help
402 payment or plan state, when your product chooses that semanticIf a customer is over quota, do not report 503 merely because it is operationally convenient. Clients and monitors will classify the wrong incident.
Standard headers are useful but incomplete
The emerging standard fields are clear and machine-readable:
HTTP/2 429
Retry-After: 23
RateLimit-Limit: 100
RateLimit-Remaining: 0
RateLimit-Reset: 23
content-type: application/problem+jsonThe exact header spellings and draft versions have varied across ecosystems, so document what you emit. Retry-After is the strongest interoperable signal. RateLimit-* fields help clients shape traffic before rejection, but only if they describe the correct bucket.
The body should identify the budget
A useful problem response names the policy dimension without leaking internals:
{
"type": "https://api.sudosecurity.org/problems/rate-limited",
"title": "Export quota exceeded",
"status": 429,
"detail": "This workspace has used its hourly export allowance.",
"budget": "workspace_exports_hourly",
"retry_after_seconds": 173,
"upgrade_url": "https://billing.sudosecurity.org/workspace/ws_123/plan"
}Do not expose raw Redis keys, internal machine addresses, or exact attack-detection thresholds. You want legitimate integrators to adapt without giving attackers a precise bypass oracle.
Write the client-side contract too
Documentation should specify behavior for transient rejection:
1. Honor Retry-After when present.
2. Otherwise use exponential backoff with full jitter.
3. Retry only idempotent operations or operations with idempotency keys.
4. Cap total retry time and surface failure to the operator.
5. Do not increase concurrency after a 429.The last rule matters because many clients interpret rejection as a reason to add workers. That response converts self-protection into self-destruction.
Avoid leaking security decisions
For login and sensitive endpoints, verbose limiter messages can become enumeration oracles. "Too many attempts for existing account alice@example.com" confirms the account. Use generic language externally while retaining exact budget attribution in internal logs. The public contract can still be operationally useful without exposing target validity.
Test clients under rejection
Most integration tests exercise the happy path. Add a harness that forces 429 and observes whether the SDK honors delay, preserves idempotency keys, and avoids unbounded concurrency. A client library that ignores your contract is part of your production risk.
Field notes
Retry-Aftermay be a number of seconds or an HTTP date; pick one consistently in generated clients.- Remaining counts are estimates under concurrency. Do not promise transactional exactness unless enforcement truly provides it.
- A reset timestamp is often clearer than a duration for user interfaces; machine clients usually prefer duration.
- If every response includes limit headers, decide whether the overhead and information exposure are justified.
Was this lesson helpful?