A request counter can stay below its limit while an API exhausts its database, queues thousands of exports, or spends its notification budget. The protected resource determines what must be counted. Define the resource budget, the caller's identity, and the point where work becomes expensive before selecting a limiter.
This guide covers application API limits for shared capacity and abuse resistance. It offers design and verification criteria rather than universal thresholds or a provider-specific implementation. Rate limiting reduces particular resource exposures; authentication, authorization, input validation, and infrastructure protection remain separate controls.
Name the resource and its unit of consumption
Inventory the operations that can consume substantial CPU, database time, storage, outbound traffic, or paid provider calls. A cheap metadata read and a full account export should not automatically have the same cost simply because each is one HTTP request.
OWASP's resource consumption guidance covers limits beyond request frequency, including execution time, input sizes, returned records, and spending at downstream services. Use those dimensions to identify the boundary your design needs to protect.
For a synthetic report API, define separate budgets for report creation frequency, simultaneously running reports, maximum selected data, and retained output size. A request-rate allowance alone leaves the system exposed to a few very expensive accepted jobs.
Choose limits from measured capacity, acceptable service behavior, and the intended product allowance. Record whether a number is a hard safety ceiling, a customer quota, or an operational target. Those meanings can justify different rejection and recovery behavior even when enforced by related infrastructure.
Derive quota identity from trusted request context
For authenticated work, derive the account or principal from validated authentication state. Do not let a body field or arbitrary header choose which account's quota is charged. Keep authorization checks in the request path so unused capacity never grants access to another account's resources.
An IP-based rule can provide an early boundary for anonymous traffic, but a shared network may contain many legitimate users and one caller may use several addresses. Treat the address as one signal with an explicit trust model. Accept forwarded addresses only through the configured trusted proxy chain.
Normalize limiter keys into a bounded namespace. Avoid retaining a new permanent counter for every attacker-controlled URL, query string, or claimed identity. Bound key length and lifetime, and keep private account details out of metrics labels and public responses.
Decide how limits combine. A synthetic export request might need to pass both an account allowance and a service-wide concurrency budget. Specify whether a rejection consumes either allowance and how reservations are released after admission fails. Otherwise retries can drain one budget without doing useful work.
Enforce the budget where expensive work begins
Place a coarse traffic boundary before expensive parsing where the stack permits it, then apply authenticated operation limits before allocating substantial resources. Keep request body and decompression limits independent of whether a quota check succeeds.
If several application instances share a quota, require a coordinated decision with atomic consumption at the chosen scope. Independent counters permit each instance to grant its own allowance. A distributed store can support shared enforcement, but its consistency, expiry, and failure behavior become part of the design.
Document burst behavior. A fixed-window design can admit adjacent bursts around a boundary; another design may smooth traffic while permitting an explicit burst allowance. Test the selected algorithm against the reader-visible contract rather than promising that every implementation produces the same schedule.
For long jobs, acquire concurrency capacity when execution begins and define who releases it. A request accepted into a bounded queue has a different lifecycle from a running export. If using expiring leases, handle a worker that continues after losing its lease so capacity bookkeeping does not silently permit uncontrolled overlap.
Make a rejection useful to legitimate clients
RFC 6585 defines HTTP 429 Too Many Requests and permits a Retry-After field. The specification leaves caller identification and counting policy to the service. Document those choices in the endpoint contract instead of expecting the status code to explain them.
Use a stable public error describing the applicable scope and whether retry is useful. Avoid exposing counter storage details or whether a private account exists. Keep identity-sensitive responses consistent with the endpoint's information-disclosure policy.
HTTP semantics permits Retry-After as an HTTP date or a nonnegative number of seconds. Produce a value the client can interpret, and avoid implying that capacity is reserved until that moment. Another caller may consume it first.
Clients should wait within their operation deadline, apply the documented retry policy, and avoid restarting a mutation with a fresh business identity. Use idempotent write design when a prior attempt may already have been accepted. Request rejection and uncertain completion are different states.
Test store failure, fairness, and recovery
Choose behavior for a limiter-store outage by endpoint risk. An expensive paid operation may need to reject admission when its budget cannot be checked. A low-cost public read may use a separately bounded fallback. Explain the capacity and availability trade-off rather than letting an exception handler grant unlimited work.
In an isolated test, send concurrent requests through several instances for the same principal. Include a new time window, an exhausted quota, malformed identity inputs, many unique keys, a lost store connection, and a store response arriving after the request deadline. Verify durable side effects as well as response counts.
Test fairness with two accounts and with several users sharing an address. Confirm that one saturated account does not consume another's allowance except through a documented global limit. Exercise the system-wide ceiling so that the application degrades predictably under permitted traffic too.
Track accepted, delayed, and rejected work alongside actual resource use. Keep rejected payloads out of routine logs. If database load or provider spending continues growing while request limits appear healthy, revisit the cost model and admitted concurrency. The meaningful verification is that the named resource stays within its intended operating boundary while clients retain a clear recovery path.
Was this guide useful?
Your rating helps us prioritize clearer, more practical technical content.
Review diffs, run checks, and prepare the release in Arezgit.
Keep Git review, security scanning, API checks, database inspection, and release preparation together in one local desktop application.
Explore Arezgit