API Rate Limiting: Set Request Limits and Handle 429 Responses
Rate limiting controls how many requests are allowed over time to protect a service or its dependencies from overload. A requests-per-second number alone may not be enough. Decide what is limited, which caller or route it applies to, how much burst to allow, and whether the real bottleneck is request rate or concurrent work. Then give clients a response they can use to recover.
Rate limiting controls how many requests are allowed over time to protect a service or its dependencies from overload. A requests-per-second number alone may not be enough. Decide what is limited, which caller or route it applies to, how much burst to allow, and whether the real bottleneck is request rate or concurrent work. Then give clients a response they can use to recover.
Request rate, quota, and concurrency are different
A request-rate limit controls requests per second or minute. A quota controls a larger-period total, such as daily calls. A concurrency limit controls how many operations may be in flight at once. A low request rate can still exhaust connections or memory if each operation runs for a long time. Short operations can also create bursts even when average traffic is modest.
Find the resource to protect first. Observe whether a database’s request rate, an external API’s call limit, CPU, queue depth, or concurrent connections saturate first. Rate limiting can fit a rate bottleneck, but concurrency caps or a work queue may be more direct when long jobs or fan-out are the problem. Microsoft’s rate-limiting guidance makes the same distinction between request rate and bottlenecks such as concurrent work or resource contention.
Choose the scope and algorithm
Decide who or what is limited: an authenticated user, tenant, API key, method and route, or the whole service. If no user identity exists, limiting only by IP can group many people behind a shared network or be avoided by changing addresses. Use client addresses supplied by trusted proxies, and select keys that match your authentication and delegation model. Per-user protection together with a service-wide limit can reduce the chance that one caller takes all capacity.
A fixed window is simple but can allow a burst at the boundary between windows. A sliding window measures a smoother interval but needs more state and computation. A token bucket refills tokens at a configured rate and consumes one per request; it allows a short burst up to bucket capacity while controlling the average rate. Amazon API Gateway documents separate refill-rate and burst-capacity settings. Gateway limits may be best effort depending on the product, so do not treat them as a strict security boundary or exact billing ledger.
Set limits to match load and policy
Set a sustained rate and burst capacity based on downstream capacity, normal usage, legitimate bursts, and fairness across tenants. For example, imagine an educational API allowing each tenant 20 requests per second with a burst of 40. In a token bucket this means refilling 20 tokens per second with a maximum of 40; it is an illustrative configuration, not a proven ideal or an AWS setting. Tune limits using measured load and the dependency’s contract.
Enforce limits early and near the resource being protected. An edge gateway can apply caller-specific policies, while an internal service limiter can control aggregate calls to a shared database or external dependency. If several applications or workstreams use the same downstream service, coordinate them through a shared limiter or explicitly divide capacity pools. Local counters on every instance can multiply the effective limit as instances scale out, so choose shared storage, distributed coordination, or intentionally partitioned capacity.
Design rejection and retries together
For an exceeded request-rate limit, an HTTP API can return 429 Too Many Requests. RFC 6585 says a response should explain the condition and may use Retry-After to tell the client how long to wait. Give enough information to identify the exceeded scope and retry condition without exposing sensitive policy or other users’ data.
Clients should honor Retry-After and use a bounded number of exponential-backoff retries with small random jitter. Retrying immediately after every failure can create a retry storm and worsen the limit. Before retrying a write, consider whether the server may already have applied it; design the operation to be idempotent where duplicate effects matter. Use 503 consistently for temporary service unavailability rather than mislabeling every overload or outage as 429.
Verify behavior in operation
Before rollout, reproduce expected normal traffic and bursts. Confirm allowed requests pass and excess requests are rejected quickly. Check multiple instances, unauthenticated traffic, proxy chains, limiter-store failures, and policy updates. Decide and document whether a failed limiter should fail open, fail closed, or use a conservative local fallback, based on the protected resource and availability needs.
Observe rejection rates, 429 responses, allowed traffic, queue wait time, downstream latency and errors, and the distribution of limit keys. If many legitimate callers are limited at once, the scope or burst may be too restrictive. If the downstream service saturates without 429s, the limiter may not match the actual bottleneck. The goal is not to make request counts look tidy; it is to reject or defer excess load predictably so the service and its other callers remain protected.
References
[Microsoft Learn: Rate Limiting pattern](https://learn.microsoft.com/en-us/azure/architecture/patterns/rate-limiting-pattern)
[Amazon API Gateway: Throttle requests to REST APIs](https://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-request-throttling.html)
[IETF RFC 6585: 429 Too Many Requests](https://www.rfc-editor.org/rfc/rfc6585.html)