Where to enforce a limit: gateway or service
Several places can say no
Section titled “Several places can say no”A request travels through an edge network, a load balancer or API gateway, a service, and its dependencies. A limit can sit at any of those, and real systems use more than one. The question is not which is right, but what each one is good for.
- At the edge or the gateway. The gateway is the front door that every external request passes through, so it is where limits for each client belong. It sees the credentials (an API key, a token), the path, the method and the source address, and it can refuse a request before any service has spent anything on it. AWS API Gateway shows the pattern: it applies throttling settings in order: the limits of a client’s usage plan or of a method come first, then the per-method settings of the stage, then the account’s limit in a Region, then the Regional limit that AWS sets. An edge network does the same job closer to the client; the lesson on CDNs and the edge shows what lives there, and an API gateway works at the request level, like the layer 7 balancers in what a load balancer does.
- In the service. Only the service knows what a request costs, how many are in flight, and how loaded its own workers are. Stripe’s article describes this layer. Alongside its request rate limiter it uses a concurrent requests limiter, which limits the requests in progress, for expensive endpoints; a fleet usage load shedder, which keeps a share of the fleet for critical requests; and a worker utilization load shedder, which it calls the final line of defense, dropping lower-priority traffic when the workers are overloaded.
- In the client. A well-behaved client slows down when it is told to, and the headers below are how it is told. It cannot be relied on, since the clients you most need to limit are the ones that ignore it.
Mental model. The gateway limits who may ask and how often, because it knows who is asking. The service limits how much it will do at once, because it knows what that costs. Neither knows what the other knows, so a design that relies on one of them leaves a gap.
What happens if every client is within its limit?
Section titled “What happens if every client is within its limit?”Suppose an API has 1,000 clients, and each is allowed 10 requests a second. If the service can handle 2,000 requests a second, the limits add up to 1,000 × 10, which is 10,000 a second, five times what it can take. Each client is polite, every limit is respected, and the service can still be overwhelmed when a fraction of the clients use their allowance at the same moment. A per-client limit protects the other clients from one that is greedy. It does not protect the service from all of them together.
That second job belongs to a limit on the service as a whole, such as a limit for all the requests an account or a Region may send, as in AWS’s account-level throttling, or, as in Stripe’s design, a load shedder that looks at the state of the fleet and not at who sent the request. Load shedding has a lesson of its own in overload, backpressure and load shedding. Stripe lists both kinds of limiter as parts of one system for that reason. Then there is the kind of limit that works on something other than a rate: a concurrency limit bounds the number of requests in progress. Stripe’s concurrent requests limiter is aimed at CPU-intensive endpoints, and ASP.NET Core ships a concurrency limiter beside the rate-based ones.
The gateway’s limits and the service’s limits should also agree in the order they apply. The gateway should be the first to refuse in ordinary use, so that a request that is going to be refused is refused where it costs least, and the service’s own limits should be the backstop for what gets past.
Choosing the key
Section titled “Choosing the key”A limit counts requests for a key, and the key is the most important choice after the algorithm.
- The user or the API key. The fairest key, and the one for a tiered service: ASP.NET Core’s documentation shows a premium key with a higher limit than other keys. It needs the request to be identified first, so it comes after authentication.
- The source IP address. Available before any identification, so it is the usual choice for anonymous endpoints such as a sign-in. Many users can share one address, and the documentation warns that a limit by address is exposed to attacks that spoof the source address.
- The path or the method. A cheap read and an expensive search should not share a budget. ASP.NET Core’s documentation shows limits that differ by path, and AWS lets you set throttling for each method.
Each key creates a counter. The documentation for ASP.NET Core warns that building partitions from input a client controls can exhaust memory and become a denial-of-service attack of its own. It also reminds you that rate limiting is not a full defense against a distributed denial of service attack.
What to tell the client
Section titled “What to tell the client”A refused client needs to know that it was refused for rate and not for an error, and when to come back.
- 429 Too Many Requests. RFC 6585 defines it for a user who has sent too many requests in a given amount of time. The response should explain the condition and may include
Retry-After. nginx’slimit_reqanswers 503 by default, and a directive changes it to 429. Stripe’s article leaves the choice between 429 and 503 to the case. Retry-After. The time to wait. A token bucket can compute it exactly, as in the C# example of the first lesson. A fixed window can give the time to the next boundary.- The RateLimit header fields. An Internet-Draft of the HTTP API working group,
draft-ietf-httpapi-ratelimit-headers, proposes two response header fields:RateLimit-Policy, which advertises the server’s quota policies, andRateLimit, which reports the quota currently available. Its first example isRateLimit-Policy: "burst";q=100;w=60,"daily";q=1000;w=86400withRateLimit: "default";r=50;t=30, whereqis the quota of a policy,wits window in seconds,rthe quota available now andtthe time within which the client can use it. It is a draft, not a standard. The version read for this lesson is version 11, dated 23 May 2026, with the intended status of Standards Track and an expiry of 24 November 2026, and it may change before it is published as an RFC or may not be published. The draft itself says clients must not assume that a positive available quota is a guarantee that further requests will be served. Treat the fields as hints and do not build a client that depends on them.
Check yourself
Section titled “Check yourself”Check your understanding: where to enforce
This quiz needs JavaScript to check your answers.
A short checklist
Section titled “A short checklist”- What is the limit protecting: a client’s fairness, the service’s capacity, a bill, or a downstream dependency?
- What is the key, and is it known at the place where the check runs?
- Which algorithm fits: a burst allowance, a steady feed, a quota, a strict window? (the algorithms)
- Is the count shared between the servers that enforce it, and what happens when the shared store cannot be reached? (distributed limits)
- What does a refused client receive, and does it know when to retry?
- What protects the service when every client is within its limit?
- A limit can sit at the edge or gateway, in the service, or in the client. The gateway knows who is asking. The service knows what it costs and how loaded it is.
- A limit for each client does not bound the load on the service: 1,000 clients at 10 a second is 10,000 a second against a capacity of 2,000. A limit for the whole service, a concurrency limit or a load shedder does that job.
- The key decides what the limit means, and it carries a risk: addresses can be spoofed or shared, and a key space a client controls can use up memory.
- A refused client gets a 429, preferably with
Retry-After. The RateLimit header fields are an Internet-Draft, not a standard.
Your progress
Built by GhaziAlibi.
© ShardCraft. All rights reserved.