Caching requests and responses is a critical ability. The router enables very detailed control of the cache control policy for the whole graph, as well as subgraphs
Cache-Control
header is used to define the caching behavior of responses. It tells browsers and intermediary servers how to handle content caching. Some of the common Cache-Control
directives include:
max-age
: Specifies how long (in seconds) a response can be considered fresh.
no-cache
: Forces caches to submit the request to the origin server for validation before serving the cached copy.
no-store
: Prevents the storage of any part of the response by caches.
Expires
is an older header used to specify an exact expiration time for cached content. If both Cache-Control
and Expires
are present, Cache-Control
takes precedence.Cache Control Policy
To enable a restrictive cache control policy, insert the following snippet into your config.yaml file and adjust it according to your needs.
Cache-Control
header (and related ones, such as Expires
). This is critical for cases where different subgraphs have varying caching requirements, and you want to ensure that sensitive or time-sensitive data is properly handled.
enabled
: false in your cache_control_policy
configurationno-cache
and no-store
directives take priority, and these will override any other directives.
max-age
values: The smallest max-age
value from any subgraph (or the default, if specified in the configuration) is selected.
Expires
header: The earliest expiration date will be used if Expires
headers are provided.
no-cache
/no-store
always winsWhen any subgraph returns no-cache
or no-store
directives, they will take precedence over all other cache settings, regardless of max-age
values. This guarantees that sensitive data will not be stored in caches, providing an extra layer of security.no-cache
If a global cache control policy is enabled, we will automatically set Cache-Control: no-cache
to GraphQL mutation request for security reasons, ensuring that mutation results are never cached.no-cache
If the final response to the client has any errors, we automatically set Cache-Control: no-store, no-cache, must-revalidate
to prevent clients or caches re-serving the errored response.max-age
Cache-Control: max-age=600
(10 minutes)
Cache-Control: max-age=300
(5 minutes)
Cache-Control: max-age=300
, since Subgraph A’s max-age=300
is more restrictive than the global default.no-cache
with Global DefaultCache-Control: max-age=600
Cache-Control: no-cache
Cache-Control: max-age=300
Cache-Control: no-cache
overrides everything due to its strictness.Expires
in SubgraphsCache-Control: max-age=300
, Expires=Wed, 15 Sep 2024 18:00:00 GMT
Cache-Control: max-age=600
, Expires=Wed, 15 Sep 2024 17:00:00 GMT
Expires=Wed, 15 Sep 2024 17:00:00 GMT
, the earlier expiration time.no-cache
Cache-Control: max-age=600
Cache-Control: no-store
Cache-Control: no-store
takes precedence, as it’s stricter than the global default.enabled: true
, value
unset
Cache-Control: max-age=300
subgraph B
will have Cache-Control: max-age=300
set, but requests which don’t access subgraph B won’t have any cache-control setsubgraph B
will have an error, and thus Cache-Control: no-store, no-cache, must-revalidate
set, but requests which don’t access subgraph B won’t have any cache-control set.no-cache
or no-store
directives for security sensitive subgraphs.
set
operation in their header propagation rules, users can overwrite the cache control policy if necessary.specific-subgraph
will have the desired subgraph cache control value set (max-age=5400
).