Azure API Management – How to enforce a 60-second lockout for all subsequent API requests after exceeding the rate limit?

Pooja Parmar 0 Reputation points
2026-11-20T04:38:10.41+00:00

We have configured IP-based and user-based rate limiting in Azure API Management with the following requirements:

IP-based rate limit: 150 requests per 60 seconds.

User-based rate limit: 30 requests per 60 seconds.

User identification is based on the JWT token.

IP identification is based on context.Request.IpAddress.

When a user exceeds the configured limit, we want APIM to immediately return HTTP 429 (Too Many Requests) for all subsequent API requests.

For example, if a user sends 30 requests within 60 seconds:

Requests 1–30 should be processed successfully.

Request 31 should return HTTP 429.

Requests 32, 33, 34, and all subsequent requests should also immediately return HTTP 429.

All API endpoints accessed by the same user should be blocked during the lockout period, not just the endpoint that exceeded the limit.

After 60 seconds from the first rate-limit violation, the user should be allowed to make API requests again.

We want the same behavior for IP-based rate limiting.

2. Current APIM Configuration

We are using the following policies:

set-variable to generate IP and user keys.

cache-lookup-value to check whether an IP or user is already locked.

rate-limit-by-key to enforce IP and user request limits.

return-response to return HTTP 429 when an existing lock is found.

Our current rate-limit policies are:

<!-- IP limit: 150 requests per 60 seconds -->
<rate-limit-by-key
    calls="150"
    renewal-period="60"
    counter-key="@("ip-" + context.Request.IpAddress)"
    remaining-calls-header-name="X-IP-RateLimit-Remaining"
    total-calls-header-name="X-IP-RateLimit-Limit" />

<!-- User limit: 30 requests per 60 seconds -->
<rate-limit-by-key
    calls="30"
    renewal-period="60"
    counter-key="@((string)context.Variables["userKey"])"
    remaining-calls-header-name="X-User-RateLimit-Remaining"
    total-calls-header-name="X-User-RateLimit-Limit" />

We also have custom cache lookup logic to check existing IP and user locks.

3. Current Issue

When we perform load testing using Burp Suite:

  • Requests initially receive HTTP 200.
  • Once the configured threshold is exceeded, one request receives HTTP 429.
  • Subsequent requests remain pending or do not receive an immediate response.
  • We want all subsequent requests to immediately receive HTTP 429 instead of waiting or being processed after the rate-limit window expires.We have configured IP-based and user-based rate limiting in Azure API Management with the following requirements:
    • IP-based rate limit: 150 requests per 60 seconds.
    • User-based rate limit: 30 requests per 60 seconds.
    • User identification is based on the JWT token.
    • IP identification is based on context.Request.IpAddress.
    When a user exceeds the configured limit, we want APIM to immediately return HTTP 429 (Too Many Requests) for all subsequent API requests. For example, if a user sends 30 requests within 60 seconds:
    1. Requests 1–30 should be processed successfully.
    2. Request 31 should return HTTP 429.
    3. Requests 32, 33, 34, and all subsequent requests should also immediately return HTTP 429.
    4. All API endpoints accessed by the same user should be blocked during the lockout period, not just the endpoint that exceeded the limit.
    5. After 60 seconds from the first rate-limit violation, the user should be allowed to make API requests again.
    We want the same behavior for IP-based rate limiting.

    4. Expected Behavior

    We are looking for a solution where APIM can maintain a separate lockout state for each user and IP address. For example:
    Request Expected response
    Request 1–30 200 OK
    Request 31 429 Too Many Requests
    Request 32–40 429 Too Many Requests
    Any subsequent request during lockout 429 Too Many Requests
    After 60-second lockout expires Allow requests again
    The lockout should apply across all API operations for the same user or IP address.

Our objectiv is we want to enforce both IP-based and user-based rate limiting centrally at the APIM level, ensuring that all subsequent API requests are immediately rejected with HTTP 429 for 60 seconds after the first violation, across all API endpoints.

Azure API Management
Azure API Management

An Azure service that provides a hybrid, multi-cloud management platform for APIs.

0 comments No comments

1 answer

Sort by: Most helpful
  1. AI answer

    2026-11-20T04:40:36.31+00:00

    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-value writes 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:

    1. Check whether the key is already locked.
    2. If locked, return locked and its remaining TTL.
    3. Otherwise increment the key’s current count.
    4. If the increment exceeds the threshold, create a 60-second lock in the same atomic transaction.
    5. 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

    1. Limit call rate by key
    2. How to implement rate limiting in Azure API Management
    3. Store value in cache
    4. Caching overview
    5. Troubleshoot Azure API Management performance issues in API calls
    6. Error handling in API Management policies
    AI-generated content may be incorrect. Read our transparency notes for more information.

    Was this answer helpful?

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.