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.
What you'll learn:
- Add response caching for your API
- Verify caching in action
Internal cache is not available in the Consumption tier of Azure API Management. You can use an external Azure Cache for Redis instead.
To complete this tutorial:
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.
Sign in to the Azure portal at https://portal.azure.com.
Browse to your APIM instance.
Select the API tab.
Click Demo Conference API from your API list.
On the top of the screen, select Design tab.
In the Inbound processing section, click the </> icon.
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>
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.
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.
To see the caching in action, call the operation from the developer portal.
- In the Azure portal, browse to your APIM instance.
- Select the APIs tab.
- Select the API to which you added caching policies.
- Select the GetSpeakers operation.
- Click the Test tab in the top right menu.
- Press Send.
- For more information about caching policies, see Caching policies in the API Management policy reference.
- For information on caching items by key using policy expressions, see Custom caching in Azure API Management.
- For more information about using external Azure Cache for Redis, see Use an external Azure Cache for Redis in Azure API Management.