Response Cache Policy
Use this policy for highly recurring and static responses to manage latency and response times, which can reduce the load on the upstream API as well as the API version or Proxy server. An incoming HTTP request to one of your API version or Proxy endpoints returns the cached response that’s stored for a specified time period. The policy owner can configure the cache time-to-live (TTL) per policy and the cache key used to cache each request (with every key-value pair).
In the policy settings, you specify the cache key parameters that the HTTP requests map to and set the cache response time and refresh interval.
Policy execution order
This policy runs after every request and response policy.
Known issues
Both the HTTP Cache Response and HTTP Retry Rules fail to evaluate expressions. As a result, the When this policy should be applied field should not be set. Doing so can cause issues with the way the policy is applied.
Architecture
Each Snaplex node has its own cache, so the cache for an incoming request might be missed on each node until properly distributed and hydrated on each node.
By default, the following cache keys are used:
- Protocol
- Host
- Path
- HTTP Method
The policy supports incorporating headers and query parameters as part of the cache key (not to access the cache key). The headers and query parameter values are used as part of the generated cache key to ensure unique cache entries as needed.
Usage guidelines
- We limit each policy to store 100 entries in the cache per policy. Accordingly, the policy evicts the least used entry after 100.
- Expired caches cannot be accessed, and new entries overwrite any existing ones. In the default configuration, each response cache can store up to 85.83 MB of payload data.
Best Practices
- Do not use any headers and query string parameters, like a timestamp, as part of your cache key, or it will never result in a fulfilled request. You can optionally remove those headers using the Request Transformer policy. Or, avoid headers query string parameters as part of the cache key and only as needed when a unique value is present per request.
- To invalidate cached data, use the Invalidate Response Cache API. The Invalidate Response Cache API enables you to keep cached data up to date with the latest changes. You can define caching policies for the specific API endpoint, specifying parameters such as cache duration, cache key generation, and cache storage. The cache invalidation mechanism monitors changes in the data source and updates the cache as configured to ensure data consistency.
Limitations
- Each response cache can only contain 85 MB. The policy always returns the response, but any data over the limit renders the payload incomplete without notice.
- A response cache is not effective to use for POST and PUT HTTP methods because these operations are meant to alter the state of data, and therefore should not be cached.
| Field/Field set | Description |
|---|---|
| When this policy should be applied | An expression that defines one or more conditions that must be
true for the policy to execute.
Example: The expression |
| Cache Interval |
The time period of the current cache before it is evicted. Default value: 1 |
| Time Unit | The time unit for the Cache Interval value. Default value: Hour |
| Use HTTP Request Headers to Create Cache Keys |
The value for the header. If this field is an expression, it can return a list to simulate the header being repeated multiple times in the original response. Enables use of all header values as part of the generated cache key. Default value: Unselected |
| Use HTTP Request Query Parameter to Create Cache Keys |
Enables use of all query parameter values as part of the generated cache key. Default value: Unselected |
| Description | Default value: Requests that meet the specified criteria are returning cached responses. |