Skip to content
API and Integration

Rate Limiting When Your API Runs on More Than One Instance

9 min read

A rate limit counted separately on each instance of your API lets through the limit multiplied by the instance count, which rises as you scale out. Most layers count locally: ASP.NET Core per instance, Azure Front Door per edge location, API Management per regional gateway. Use those for coarse protection, key signed-in limits on the account rather than the IP address, keep shared counters in a store such as Redis, and enforce exact business limits in the database.

Why does a rate limit stop working when you add instances?

A rate limit stops working because each instance counts only the requests it receives. If every instance allows 100 requests a minute and you run three, a client whose requests are spread across them can make up to 300.

Microsoft’s ASP.NET Core rate limiting documentation says this directly: the middleware keeps its counters in memory in each app instance, and the effective limit is the configured limit multiplied by the number of instances. Its own example is a limit of 100 a minute becoming up to 300 across three instances.

Autoscaling makes it worse. If your API scales out under load, the effective limit rises with every instance added, which tends to happen during the busy periods when the limit matters most. A limit that looked right in testing against one instance can be several times looser in production.

This is one of the gaps we look for when we review an API, alongside rules that the app enforces but the API does not. Our guide to why a pen test can pass while bots still get in covers why it rarely shows up in a penetration test.

Where does each layer count requests?

Each layer in a typical Azure API stack can enforce a rate limit, and each counts in a different place. None of the gateway and edge options shares a single exact counter across everything, which is fine for protection against floods and spikes but not for limits that must be exact.

Where API rate limits are counted: per edge location, per regional gateway, per API instance, or once in shared state
Four layers of a typical Azure API stack and where each counts requests for rate limiting. Edge, Azure Front Door WAF: rate limit rules are counted at each edge location, by IP address by default. Gateway, Azure API Management: rate-limit-by-key is counted at each gateway, so once per region, and quota-by-key uses one counter for the whole instance. API instances, your code: instances 1, 2 and 3 each keep an in-memory counter. Shared state, highlighted, with one copy of each counter: Redis holds shared counters for limits across instances, and the database holds exact business limits, checked in the transaction.
  • Azure Front Door WAF. Rate limit rules count requests per group over one or five minutes, grouped by socket IP address by default. Microsoft says counts are not shared between edge locations or aggregated globally, so a limit can be reached separately at each one. It also notes that low thresholds, below about 200 requests a minute, can let some requests through above the limit.
  • Azure Application Gateway WAF v2. Counters are kept for each association, so a policy attached to three gateways keeps three sets, and Microsoft says thresholds are not always enforced exactly as defined.
  • Azure API Management. The rate-limit and rate-limit-by-key policies track calls independently at each gateway, and in a multi-region deployment each regional gateway has a separate counter. Quota policies use a single counter for the whole instance. Microsoft adds that rate limiting is never completely accurate, because of the distributed nature of the throttling architecture.
  • Your API code. In-process limiters, such as the ASP.NET Core middleware, count per instance unless you give them a shared store.
  • A shared store. Counters held in Redis, or in your database, are shared by every instance that uses them, because there is only one copy.

The practical conclusion is to use edge and gateway limits to absorb floods and keep load predictable, and to put any limit that protects a business rule somewhere with a single counter.

Which key should a limit use?

A limit is only as good as the key it counts against, and for a mobile API the IP address is usually the weakest choice.

  • IP address. Mobile networks commonly use carrier-grade NAT, which RFC 6888 describes as sharing a public IPv4 address among several subscribers. An IP limit can therefore throttle many real customers at once, while a script can spread its requests across many addresses. Microsoft’s API Management guidance makes the same point about NAT. IP limits still have a place on unauthenticated endpoints, such as sign-in, and at the edge.
  • Account. For signed-in traffic, key on the user identity in the access token. API Management can read it with context.Request.Headers.GetValueOrDefault("Authorization","").AsJwt()?.Subject, and the ASP.NET Core middleware can partition on the user’s identity. This is the right default for most mobile endpoints.
  • Attested app or device. App attestation tells you whether a request came from your genuine app, which lets you give unattested requests a tighter limit. It does not always give you a device key. With Apple App Attest, your server stores a public key for each user on each device, which you can count against. A Firebase App Check token’s subject is your app ID rather than a device, and Google’s Play Integrity device recall feature, in beta, is designed so the app can recall a few bits of data without accessing device identifiers. Our guide to App Attest and Play Integrity covers the details.
  • Business keys. For scarce stock, count against the item or release as well as the account, such as two purchases per account per product drop. Our guide to customers using AI bots on scarce stock covers why these limits matter more now.

Avoid keying on values the client controls without checking them. Microsoft’s ASP.NET Core documentation warns that partitioning on unbounded user input creates a denial of service risk, because every new key creates and caches a new limiter.

How do you share one limit across every instance?

You share a limit by keeping one counter and making every instance read and update it. There are three common ways to do that, and they suit different kinds of limit.

Enforce it at a single gateway

If you run API Management in one region, its rate-limit-by-key policy gives you one counter per gateway in front of every API instance. That works well for protective limits, as long as you accept Microsoft’s caution that it is never completely accurate, and remember that a second region adds a second counter.

Keep counters in a shared store

For limits that need to hold across instances and regions, keep the counters in a shared cache such as Redis, where every instance increments the same key atomically with an expiry. Libraries exist for the common algorithms (fixed window, sliding window and token bucket), and the ASP.NET Core documentation recommends a shared store, or a gateway, for exactly this case.

Three details matter in practice:

  • Decide what happens when the store is unavailable. Failing open keeps the API running without limits, and failing closed protects the business rule but blocks every customer. One middle course is to fall back to a looser per-instance limit until the store is back.
  • Watch the extra round trip. Every limited request now makes a call to the store, so keep the store close to the API.
  • Check your Redis plans. Microsoft is retiring Azure Cache for Redis, with the Basic, Standard and Premium tiers retiring on 30 September 2028 and Enterprise tiers on 31 March 2027. New work should use Azure Managed Redis.

Enforce exact business limits in the database

Some limits are business rules rather than protection: two tickets per customer, one claim per voucher, one referral reward per account. These need to be exact, so enforce them in the same database transaction that writes the purchase or claim, with the check and the write as one atomic step, rather than with a rate limiter that Microsoft itself describes as approximate.

In our experience, separating these two kinds of limit early saves a lot of rework, because protective limits can tolerate some inaccuracy and sit at the edge and gateway, while business limits need to be exact and sit with the data.

What should the API return when a limit is reached?

The API should return 429 Too Many Requests, which RFC 6585 defines for a client that has sent too many requests in a given time, with a Retry-After header saying how long to wait. The RFC also says a 429 response must not be stored by a cache.

Check what each layer actually sends. API Management’s rate-limit policies return 429 with a Retry-After header, but its quota-by-key policy returns 403 Forbidden, which a mobile app may treat as a permissions error. In ASP.NET Core, set the rejection status code to 429 explicitly and copy the retry-after value into the response in the rejection callback, as Microsoft’s documentation shows.

The IETF is standardising headers that tell clients their remaining allowance in advance. The RateLimit header fields draft defines RateLimit and RateLimit-Policy headers, and at the time of writing it is still an Internet-Draft rather than an RFC.

On the app side, respect Retry-After, back off rather than retrying at once, and show the customer a clear message. Our guide to mobile API best practices covers retry strategy for mobile clients.

How do you check that your limits hold?

You check that limits hold by testing them the way production runs, not against a single instance on a developer’s machine:

  1. List every limit and where it is enforced. Note the layer, the key, the window and whether the counter is per instance, per gateway, per edge location or shared.
  2. Test at production scale. Run a load test against an environment with the same instance count and regions as production, and measure how many requests get through compared with the configured limit.
  3. Test the business limits under contention. Send simultaneous requests for the same scarce item from one account and confirm the database rule holds.
  4. Log every limit decision. Record the key, the layer and the outcome, so you can see who a limit caught and tune it with evidence.
  5. Run new limits in a logging mode first. Where the layer supports it, record what a limit would have blocked before you let it block real customers.

How do we approach this?

We look at rate limiting as part of the whole path from the app to the data, because a limit only works if every route to the same action passes through it. Our mobile and API security review traces each exposed endpoint through the code, checks where limits are enforced and what they are keyed on, and uses your telemetry to see how they behave under real load.

We recommend changes sized to the real risk, which might be as small as moving one limit into a shared store, and we can make the changes across the app, the API and your Azure configuration. If you are building a new mobile backend, our mobile app backend services build this in from the start.

Frequently asked questions

Does Azure API Management share rate limit counters across regions?
No. Microsoft's documentation says each regional gateway keeps a separate counter for rate-limit and rate-limit-by-key, while quota policies use a single counter for the whole instance. It also says rate limiting is never completely accurate, because of the distributed nature of the throttling architecture.
Should a mobile API rate limit by IP address?
Not as the main key. On mobile networks, carrier-grade NAT means many subscribers can share one public IPv4 address, so an IP limit can block real customers while scripts spread across addresses. Key signed-in traffic on the account, and keep IP limits for unauthenticated endpoints and coarse protection at the edge.
Is the ASP.NET Core rate limiting middleware enough?
For a single instance, often yes. Microsoft's documentation says it keeps counters in memory in each app instance, so the effective limit is the configured limit multiplied by the number of instances. Once you scale out, enforce the limit at a gateway or in a shared store as well.
What should an API return when a client hits a rate limit?
Return 429 Too Many Requests, defined in RFC 6585, with a Retry-After header saying how long to wait. Check what each layer sends: API Management's rate-limit policies return 429, but its quota-by-key policy returns 403 Forbidden with a Retry-After header.
What is the difference between a rate limit and a quota?
A rate limit protects against short bursts, such as 10 calls in a minute. A quota controls usage over a longer period, such as calls per month. Microsoft's API Management documentation draws the same distinction and implements them differently, which affects how accurately each is shared across gateways.

Ready to transform your software?

Let's talk about your project. Contact us for a free consultation and see how we can deliver a business-critical solution at startup speed.