Rate limits protect service capacity and make access more predictable, but a single request count per IP rarely matches real cost. A cheap metadata read and a CPU-intensive code execution should not consume the same budget.
Choose keys and budgets
Use authenticated account or API-key identity where available, with an IP-based fallback for anonymous traffic. Apply separate limits to expensive operations and concurrent work. In distributed deployments, store counters in shared infrastructure or use a gateway that enforces a consistent policy. Local in-memory limits can still protect one process but should not be presented as global quotas.
Pick windows and burst behavior based on the client experience and backend capacity. Return a stable error code and
Retry-AfterPlan for limiter failures
Decide per route whether to fail open or closed when the limiter is unavailable. Public reads may tolerate a degraded policy; expensive or sensitive operations may need to stop. Monitor limiter health and fallback mode. Ensure reverse-proxy trust settings are correct before using forwarded IP headers, or clients may spoof the key.
Test boundary timing, concurrent requests, shared networks, multiple app instances, and recovery after Redis restarts. Track allowed, denied, and false-positive reports. A useful limit protects the service while making denial understandable and recoverable for legitimate clients.
Decide what the quota response communicates
A server returning 429 is telling the client its request rate exceeded a policy. RFC 6585 permits Retry-After, and the HTTP semantics specification allows either an HTTP date or a delay in seconds for that header. Convert and clamp the value at the delivery boundary. The client can retain input and schedule another attempt only where retrying makes sense. RFC 6585 · RFC 9110 Retry-After
Determine quota in shared storage when requests for the same identity can land on several instances. Verify atomic updates with two workers and cover a burst that crosses the window boundary. Separate identity, route class, and concurrency so a burst on one low-cost endpoint doesn't reserve every slot for a costly tool. Track rejections and accepted requests together to show whom the limit affects.
Worked example
The quota calculation must be atomic in shared storage when several instances serve requests. This function only translates a trusted limiter result into an HTTP response.
tsinterface QuotaResult { allowed: boolean; retryAfterSeconds: number; } function quotaResponse(result: QuotaResult): Response | null { return result.allowed ? null : new Response("Try again later", { status: 429, headers: { "Retry-After": String(result.retryAfterSeconds) } }); }




Community
Discussion
Loading comments…