Response Cache rule

For external endpoints, caches reoccurring and static HTTP responses to manage latency, decrease response times, and reduce load on the external server. By default, cached HTTP responses are stored based on the Service's URL and request method. Additionally, you can configure cache responses for specified request header keys and query string keys. Following rule configuration, cached responses are returned to HTTP clients instead of requesting resources from the external server.

Use this rule for highly reoccurring and static responses to manage latency and response times, which can reduce the load on the upstream API as well as the Service server.An incoming HTTP request to one of your Service endpoints returns the cached response that’s stored for a specified time period. The rule owner can configure the cache time-to-live (TTL) per rule and the cache key used to cache each request (with every key-value pair).

In the rule settings, you specify the cache key parameters that the HTTP requests map to and set the cache response time and refresh interval.

Attention: To customize the amount of cache stored in memory, consult your CSM.
Important: The entire HTTP response is cached, including headers.

Rule execution order

This rule runs after every request and response rule.

Known issues

Both the HTTP Cache Response and HTTP Retry Rules fail to evaluate expressions. As a result, the When this rule should be applied field should not be set. Doing so can cause issues with the way the rule 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 rulesupports 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 rule to store 100 entries in the cache per rule. Accordingly, the rule evicts the least used entry after 100.
  • Expired caches cannot be accessed, and new entries overwrite any existing ones. The maximum quantity of bytes that an Response Caches in the default configuration is 85.83 MB of the payload.

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 rule. 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 rules 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.
Important: These properties are configurable through Feature Flags. Refer to your CSM to modify these property settings.

Limitations

  • Each response cache can only contain 85 MB. The rule 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 Description
When this rule should be applied An expression that defines one or more conditions that must be true for the rule to execute.

Example: The expression request.method == "POST" causes the rule to execute only on POST requests.

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