Add caching to improve performance in Azure API Management

Operations in API Management can be configured for response caching. Response caching can significantly reduce API latency, bandwidth consumption, and web service load for data that does not change frequently.

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

Prerequisites

To complete this tutorial:

Add the caching policies

With caching policies shown in this example, the first request to the GetSpeakers 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 at https://portal.azure.com.
  2. Browse to your APIM instance.
  3. Select the API tab.
  4. Click Demo Conference API from your API list.
  5. Select GetSpeakers.
  6. On the top of the screen, select Design tab.
  7. In the Inbound processing window, click the triangle (next to the pencil).

    code editor

  8. Select Code editor.
  9. 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>
    
  10. In the outbound element, add the following policy:

    <cache-store caching-mode="cache-on" duration="20" />
    

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

Call an operation and test the caching

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

  1. In the Azure portal, browse to your APIM instance.
  2. Select the APIs tab.
  3. Select the API to which you added caching policies.
  4. Select the GetSpeakers operation.
  5. Click the Test tab in the top right menu.
  6. Press Send.

Next steps