Add caching to improve performance in Azure API Management

APPLIES TO: Developer | Basic | Basic v2 | Standard | Standard v2 | Premium | Premium v2

APIs and operations in API Management can be configured with response caching. Response caching can significantly reduce latency for API callers and backend load for API providers.

Important

Built-in cache is volatile and is shared by all units in the same region in the same API Management service. Regardless of the cache type being used (internal or external), if the cache-related operations fail to connect to the cache due to the volatility of the cache or any other reason, the API call that uses the cache related operation doesn't raise an error, and the cache operation completes successfully. In the case of a read operation, a null value is returned to the calling policy expression. Your policy code should be designed to ensure that there's a "fallback" mechanism to retrieve data not found in the cache. For more detailed information about caching, see API Management caching policies and Custom caching in Azure API Management.

cache policies

What you'll learn:

  • Add response caching for your API
  • Verify caching in action

Note

Internal cache is not available in the Consumption tier of Azure API Management. You can use an external Azure Cache for Redis instead. You can also configure an external cache in other API Management service tiers.

Prerequisites

To complete this tutorial:

Add the caching policies

With caching policies shown in this example, the first request to a test operation returns a response from the backend service. This response is cached, keyed by the specified headers and query string parameters. Subsequent calls to the operation, with matching parameters, will have the cached response returned, until the cache duration interval has expired.

  1. Sign in to the Azure portal.

  2. Browse to your API Management instance.

  3. Select APIs > APIs from the menu on the left.

  4. Select an API for which you want to configure caching.

  5. On the top of the screen, select Design tab.

  6. In the Inbound processing section, select the </> icon. Screenshot of the API Management policy editor in the portal.

  7. In the inbound element, add the following policy:

    <cache-lookup vary-by-developer="false" vary-by-developer-groups="false">
        <vary-by-header>Accept</vary-by-header>
        <vary-by-header>Accept-Charset</vary-by-header>
        <vary-by-header>Authorization</vary-by-header>
    </cache-lookup>
    
  8. In the outbound element, add the following policy:

    <cache-store duration="20" />
    

    In this policy, duration specifies the expiration interval of the cached responses. In this example, the interval is 20 seconds.

Tip

If you are using an external cache, as described in Use an external Azure Cache for Redis in Azure API Management, you may want to specify the caching-type attribute of the caching policies. See API Management caching policies for more details.

Call an operation and test the caching

To see the caching in action, call an operation from the portal.

  1. In the Azure portal, browse to your API Management instance.
  2. Select APIs > APIs from the menu on the left.
  3. Select the API to which you added caching policies.
  4. Select an operation to test.
  5. Select the Test tab in the top right menu.
  6. Select Trace two or three times in quick succession.
  7. Under HTTP response, select the Trace tab.
  8. Jump to the Inbound section and scroll to the cache-lookup policy. You should see a message similar to the one in the following screenshot, indicating a cache hit: Screenshot of cache-lookup when testing an API in the portal.