An Azure service that provides a hybrid, multi-cloud management platform for APIs.
APIM’s native rate-limit-by-key policy can centrally reject subsequent requests across APIs, but an exact 60-second lock beginning with request 31 requires one external atomic operation that performs counting, threshold detection, and lock creation for every request.
Native APIM throttling
Apply both policies at APIM instance → APIs → All APIs → Inbound processing, not at individual API or operation scope. A stable counter-key has one counter across every scope where that same key is used, so a user calling different endpoints consumes the same user counter.^1^
Assuming your existing JWT-validation logic has already populated context.Variables["userKey"], the complete central policy can be:
<policies>
<inbound>
<base />
<rate-limit-by-key id="ip-limit"
calls="150" renewal-period="60"
counter-key="@("ip-" + context.Request.IpAddress)"
retry-after-header-name="Retry-After"
remaining-calls-header-name="X-IP-RateLimit-Remaining"
total-calls-header-name="X-IP-RateLimit-Limit" />
<rate-limit-by-key id="user-limit"
calls="30" renewal-period="60"
counter-key="@("user-" + (string)context.Variables["userKey"])"
retry-after-header-name="Retry-After"
remaining-calls-header-name="X-User-RateLimit-Remaining"
total-calls-header-name="X-User-RateLimit-Limit" />
</inbound>
<backend><base /></backend>
<outbound><base /></outbound>
<on-error><base /></on-error>
</policies>
When a key exceeds its limit, APIM returns 429 Too Many Requests with Retry-After; clients that continue sending requests remain throttled.^2^
Do not add increment-condition or an expression-based increment-count for this requirement. Those defer counter updates until the outbound pipeline and can make the 429 occur one request later.^1^
Why this is not an exact post-violation lock
renewal-period="60" defines the limiter’s accounting window; it does not create a separate lock lasting exactly 60 seconds from the first violation. APIM also documents that rate limiting is not completely accurate because its throttling architecture is distributed. Counters are tracked independently by gateways, including regional gateways.^1^
Your cache-based lock cannot close that gap reliably:
-
cache-store-valuewrites asynchronously, so another concurrent request may not see the lock immediately.^3^ - The built-in cache is volatile and uses best-effort availability.^4^
- A sequence of “check lock → run APIM limiter → create lock” has a race: concurrent requests can pass the check before the lock is stored.
Exact lockout design
For strict request-31 semantics, APIM must call a shared atomic rate-control service before forwarding every request. Do this separately for the user and IP keys.
Each call must atomically:
- Check whether the key is already locked.
- If locked, return
lockedand its remaining TTL. - Otherwise increment the key’s current count.
- If the increment exceeds the threshold, create a 60-second lock in the same atomic transaction.
- Return one of:
-
allowed -
newlyLocked -
locked -
retryAfterSeconds
-
APIM then returns 429 immediately for either locked result and sets Retry-After to the returned TTL. A shared atomic store is required if enforcement must span gateways or regions; gateway-local APIM counters cannot provide that guarantee.
Retain rate-limit-by-key only as a best-effort protective fallback if the external decision service is unavailable. Explicitly choose whether failure should:
- Fail closed: return 503/429, preserving protection but reducing availability.
- Fail open: continue to APIM’s native limiter, preserving availability without strict lock guarantees.
Diagnose the pending Burp requests
Capture an APIM inspector trace for a throttled request and inspect the Inbound, Backend, and Outbound timings. Also select Calculate effective policy to find inherited operation, API, product, or global policies.^5^
A rate-limit failure appears as context.LastError.Reason == "RateLimitExceeded" and immediately transfers processing to on-error.^6^ Requests already admitted through inbound processing cannot be retroactively rejected, so high-concurrency tests can have outstanding requests while the new lock becomes visible.
If Azure Front Door is actually in the request path, verify what address reaches context.Request.IpAddress. If it is a proxy address, derive the rate key only from a forwarded-client-IP value that your network configuration prevents callers from spoofing; otherwise unrelated clients can share one IP counter.
References