Author : MD TAREQ HASSAN | Updated : 2020/11/24
Caching in APIM
- 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
- APIM has this feature built in caching mechanism
- You can also configure an external cache to work with an APIM
- Links:
Caching policies
- Response caching policies
- Get from cache - Perform cache look up and return a valid cached responses when available.
- Store to cache - Caches responses according to the specified cache control configuration.
- Value caching policies
- Get value from cache - Retrieve a cached item by key.
- Store value in cache - Store an item in the cache by key.
- Remove value from cache - Remove an item in the cache by key.
Response caching
- Response caching is just one of the features
- Response caching means that when similar requests are called APIM will automatically respond without your back end ever getting hit thus saving load and bandwidth consumption
- How response caching works:
- the unique request comes in and the inbound processor checks the cache to see if it has the response for that request
- If not, your back end is called. Then, on the way back, the outbound processor stores that response data in the cache
- The next time a request comes in, the inbound processor identifies that it has an existing response in the cache and it returns it, never calling your back end service
Configuring response caching
- Go to resource groups: https://portal.azure.com/#blade/HubsExtension/BrowseResourceGroups
- Select resource group > API Management service
- APIs section > APIs
- Select target API > Select All operations
- Click on ‘
</>
’ to open policy editor (either Inbound or outbount, both will open the same policy editor)- Add inbound and outbound policies
- To make it work, both Inbound and Outbound policies need to be set
- Save
Inbound 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>
Outbound policy
<cache-store duration="20" />