Docs
Launch GraphOS Studio

Distributed caching for the Apollo Router

Redis-backed caching for query plans and APQ


This feature is only available with a GraphOS Enterprise plan.
You can test it out by signing up for a free Enterprise trial.

If you have multiple instances, those instances can share a Redis-backed cache for their and (). This means that if any of your instances caches a particular value, all of your instances can look up that value to significantly improve responsiveness. For more details on and , see the article on in-memory caching.

Prerequisites

To use this feature:

How it works

Whenever a instance requires a or query string to resolve a client :

  1. The instance checks its own in-memory cache for the required value and uses it if found.
  2. If not found, the instance then checks the distributed Redis cache for the required value and uses it if found. It also then replicates the found value in its own in-memory cache.
  3. If not found, the instance generates the required or requests the full string from the client for .
  4. The instance stores the obtained value in both the distributed cache and its in-memory cache.

Redis URL configuration

The configuration must contain one or more URLs using different schemes depending on the expected deployment:

  • redis - TCP connected to a centralized server.
  • rediss - TLS connected to a centralized server.
  • redis-cluster - TCP connected to a cluster.
  • rediss-cluster - TLS connected to a cluster.
  • redis-sentinel - TCP connected to a centralized server behind a sentinel layer.
  • rediss-sentinel - TLS connected to a centralized server behind a sentinel layer.

The URLs must have the following format:

One node

redis|rediss :// [[username:]password@] host [:port][/database]

Example: redis://localhost:6379

Clustered

redis|rediss[-cluster] :// [[username:]password@] host [:port][?[node=host1:port1][&node=host2:port2][&node=hostN:portN]]

or, if configured with multiple URLs:

[
"redis|rediss[-cluster] :// [[username:]password@] host [:port]",
"redis|rediss[-cluster] :// [[username:]password@] host1 [:port1]",
"redis|rediss[-cluster] :// [[username:]password@] host2 [:port2]"
]

Sentinel

redis|rediss[-sentinel] :// [[username1:]password1@] host [:port][/database][?[node=host1:port1][&node=host2:port2][&node=hostN:portN]
[&sentinelServiceName=myservice][&sentinelUsername=username2][&sentinelPassword=password2]]

or, if configured with multiple URLs:

[
"redis|rediss[-sentinel] :// [[username:]password@] host [:port][/database][?[&sentinelServiceName=myservice][&sentinelUsername=username2][&sentinelPassword=password2]]",
"redis|rediss[-sentinel] :// [[username1:]password1@] host [:port][/database][?[&sentinelServiceName=myservice][&sentinelUsername=username2][&sentinelPassword=password2]]"
]

Router configuration

💡 TIP

In your 's YAML config file, you should specify your Redis URLs via environment variables and variable expansion. This prevents your Redis URLs from being committed to version control, which is especially dangerous if they include authentication information like a username and/or password.

Distributed query plan caching

To enable of , add the following to your 's YAML config file:

router.yaml
supergraph:
query_planning:
cache:
redis:
urls: ["redis://..."]
username: admin/123 # Optional, can be part of the urls directly, mainly useful if you have special character like '/' in your password that doesn't work in url. This field takes precedence over the username in the URL
password: admin # Optional, can be part of the urls directly, mainly useful if you have special character like '/' in your password that doesn't work in url. This field takes precedence over the password in the URL
timeout: 5ms # Optional, by default: 2ms
ttl: 24h # Optional, by default no expiration

The value of urls is a list of URLs for all Redis instances in your cluster.

All cache entries will be prefixed with plan. within the distributed cache.

Distributed APQ caching

To enable of (), add the following to your 's YAML config file:

router.yaml
apq:
router:
cache:
redis:
urls: ["redis://..."]
timeout: 5ms # Optional, by default: 2ms
ttl: 24h # Optional, by default no expiration

The value of urls is a list of URLs for all Redis instances in your cluster.

All cache entries will be prefixed with apq followed by a null byte character (referenced by the escape sequence \0 in most programming languages) within the distributed cache.

Previous
In-memory caching
Next
Entity caching
Edit on GitHubEditForumsDiscord

© 2024 Apollo Graph Inc.

Privacy Policy

Company