Rate limiting controls how much work a caller can ask an API to perform over time. It helps protect shared capacity from accidental spikes and abuse, but a request-count limit alone is rarely enough. A simple health check, a large report export, and a password reset may have very different costs and consequences. Design limits around operation cost, identity, business risk, and system capacity.
Start with a resource inventory: database queries, CPU-heavy transformations, external API calls, email or SMS sends, file storage, and paid services. Estimate normal and peak demand per operation. Identify unauthenticated routes, expensive workflows, and actions whose repeated use can affect other people. This makes it possible to choose limits that reduce abuse without arbitrarily degrading normal traffic.
For authenticated requests, use a stable user, tenant, API key, or service identity as the primary limiter key. An IP address may be useful as an additional signal but can represent many legitimate users behind a NAT or proxy. Conversely, an attacker can rotate addresses. For unauthenticated traffic, combine carefully trusted client IP information with global capacity limits and abuse signals.
Trust forwarded client-IP headers only when the request came through a known proxy that strips untrusted inbound values and writes its own verified value. Otherwise, a caller may spoof a header and receive a fresh quota. Avoid using a raw account identifier supplied in a request as an identity; derive identity from authenticated server-side context.
Use different policies for different routes and actions. Login and recovery endpoints may need limits on both account and network to prevent credential abuse and targeted lockouts. A report-generation API may need a low concurrency limit and a daily budget. Public read endpoints may allow higher burst capacity while staying within a global service ceiling.
A fixed window is easy to explain but may allow a burst at the boundary between two windows. A sliding window smooths those edges at a cost in storage or computation. A token bucket allows short bursts while enforcing a sustained rate. Leaky-bucket approaches can smooth work into a steady queue. The choice matters less than defining the actual behavior, ensuring it is enforced consistently across instances, and monitoring the effect.
For distributed systems, use a shared counter or gateway-level enforcement so each application instance does not independently grant the full limit. Define behavior during counter-store failure: fail-open risks excess load, while fail-closed can block legitimate traffic. The choice should vary by endpoint risk and availability requirements. Add a global circuit breaker or concurrency cap so many individually compliant clients cannot overwhelm a shared downstream dependency.
Require pagination and maximum page sizes. Limit request body size, array lengths, file size, execution time, and query complexity. Bound retries and fan-out to downstream services. For expensive operations, charge different quota units or use a concurrency queue. An API that accepts a user-controlled “number of records” field without a maximum can still be exhausted even when callers make only a few requests.
Consider business-flow abuse. A single accepted request might send a costly message, create an account, reserve inventory, or trigger a long-running scan. Apply limits and abuse detection to the workflow's impact, not merely the HTTP endpoint. Idempotency keys can prevent accidental duplicate operations, but the server must scope and validate them so they cannot be used to bypass authorization or replay someone else's action.
When a client exceeds a limit, return a consistent status and explain when retrying is appropriate. RFC 6585 defines HTTP 429 for “Too Many Requests” and allows a Retry-After header. Do not return internal counter values or details that help an attacker tune abuse. Document quotas and burst behavior for API consumers, and make client libraries respect retry guidance with exponential backoff and jitter.
Do not use a rate limiter as a substitute for authentication or authorization. A caller who stays under the limit can still access another user's data if the API lacks object-level checks. Rate limiting also cannot stop all distributed denial-of-service traffic; combine it with network-layer protections and provider controls appropriate to the service.
Write tests for the threshold, boundary conditions, bursts, independent identities, and multi-instance behavior. Verify that one user's quota does not affect another unless there is an intentional tenant or global limit. Test trusted-proxy parsing and spoofed headers. Simulate limiter-store latency and failure, then confirm the endpoint follows its documented fail policy without creating unbounded queues.
Monitor accepted and rejected requests by route and identity class, backend latency, concurrency, limiter errors, and downstream saturation. Alert on sudden changes, not just high rejection counts. A large rejection spike may indicate an attack, a client bug, or a limit that became too strict after a product change. Review support reports and conversion impact when tuning thresholds.
Inventory costly operations; identify authenticated and anonymous caller keys; set per-route quotas and global capacity limits; bound request sizes and operation complexity; choose a distributed algorithm; define failure behavior; expose consistent 429 responses and retry hints; prevent proxy-header spoofing; test bursts and concurrency; and review metrics after rollout. Give each limit an owner and reason so future teams can change it safely.
Effective API rate limiting protects capacity while making expected behavior predictable for clients. Combine request-rate policies with cost bounds, authorization, concurrency controls, and monitoring. Tune with production evidence and business context, because a limit that protects a service but blocks ordinary users can be an availability problem of its own.