APIM caching inbound policy does not work with API request made from different API Product

Bi Tan 60 Reputation points
2025-03-24T12:28:04.81+00:00

Hi MS team and community,

I have set up a caching policy at the API Operation Level of a GET API with the example policies (see the bottom of this comment.

When I am testing, the caching is working as expected when I use the same subscription key. I then try to use different subscription keys, it is still working until I try the subscription keys of different API Product.

In my APIM, I have API Products of different API scopes and different policies. In the above case, there are 2 API Products, Product A and Product B, that both share the same API scope, both granting access to this API (refer as API A).

Since I have specified the 'vary-by-developer="false"'. If I made the first API call with the Product A subs key, I expect a cache hit when I made a second API request within the cache store duration with Product B subs key, but this result into a cache miss. This is verified in my Azure Diagnostic Logs.

I then look into the Trace for each of the API call in APIM's testing console. I noticed that the cacheKey is capturing the API Product ID.

e.g.

"cacheKey": "3_abc.azure-api.net.12_data.74482w-data;rev=12.194748_get-data.194749_8_https_abc.azurewebsites.net_443/api/data-api?dataType=Combined&pageNumber=1&Id=123123",

"cacheKey": "3_abcazure-api.net.12_data-premium.86443w-data;rev=12.194748_get-data.194749_8_https_abc.azurewebsites.net_443/api/data-api?dataType=Combined&pageNumber=1&Id=123123",

Can you please investigate if this is expected and how I can address this? I would like to achieve caching regardless of the API subscription key and the API Product which the key is associated with (i.e. any authenticated/authorized user making the exact same API request should be able to fetch the data from cache if the query string match).

Many thanks.

<policies>
    <inbound>
        <base />
        <cache-lookup vary-by-developer="false" vary-by-developer-groups="false" allow-private-response-caching="false" must-revalidate="false" downstream-caching-type="none">
            <vary-by-query-parameter>Id</vary-by-query-parameter>
            <vary-by-query-parameter>dataType</vary-by-query-parameter>
            <vary-by-query-parameter>pageNumber</vary-by-query-parameter>
        </cache-lookup>
    </inbound>
    <backend>
        <base />
    </backend>
    <outbound>
        <redirect-content-urls />
        <base />
        <cache-store duration="600" />
    </outbound>
    <on-error>
        <base />
    </on-error>
</policies>
Azure API Management
Azure API Management
An Azure service that provides a hybrid, multi-cloud management platform for APIs.
2,371 questions
{count} votes

Accepted answer
  1. Shireesha Eeraboina 2,815 Reputation points Microsoft External Staff
    2025-04-10T05:07:32.74+00:00

    Hi Bi Tan,

    Great to hear you’ve made progress!

    You’re right when using a custom cache key, query parameter order can cause cache misses since the raw query string is treated as-is. To handle this and avoid duplicate cache entries, we recommend normalizing the query parameters by sorting them alphabetically before building the cache key.

    Here’s how you can do it:

    <set-variable name="sortedQuery" value="@{
        var queryParams = context.Request.Url.Query
            .OrderBy(p => p.Name)
            .Select(p => p.Name + "=" + p.Value);
        return string.Join("&", queryParams);
    }" />
    <set-variable name="customCacheKey" value="@($"{context.Request.Url.Path}?{context.Variables["sortedQuery"]}")" />
    
    

    Then use this customCacheKey with your <cache-lookup-value> and <cache-store-value> policies as before. This ensures a consistent key regardless of parameter order and avoids extra latency.

    You can also refer to these documentation here for working with custom cache keys:

    Azure API Management caching policies

    I hope this addresses your query. Please let me know if you need any further assistance or clarification.

    1 person found this answer helpful.
    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

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