-
Notifications
You must be signed in to change notification settings - Fork 124
docs: add l2 cache documentation for persisted documents #7463
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
adambenhassen
wants to merge
6
commits into
main
Choose a base branch
from
adam/console-1275-docs
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
c8adf0b
docs: add l2 cache documentation for persisted documents
adambenhassen 5189fc6
add doc
adambenhassen 7501a56
update problem
adambenhassen 51f335f
prettier
adambenhassen d65d3e0
prettier
adambenhassen c8e9899
fixes
adambenhassen File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
96 changes: 96 additions & 0 deletions
96
...rc/app/product-updates/(posts)/2026-01-07-persisted-documents-l2-cache/page.mdx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,96 @@ | ||
| --- | ||
| title: Layer 2 Cache for Persisted Documents | ||
| description: Add a distributed cache layer between in-memory cache and CDN for persisted documents. | ||
| date: 2026-01-07 | ||
| authors: [adam] | ||
| --- | ||
|
|
||
| You can now configure a Layer 2 (L2) cache for persisted documents, reducing CDN calls and improving | ||
| performance in serverless and multi-instance deployments. | ||
|
|
||
| ## The Problem | ||
|
|
||
| The in-memory LRU cache (L1) for persisted documents has limitations: | ||
|
|
||
| - **Cache lost on restart**: Serverless cold starts, instance restarts, and scaling events clear the | ||
| cache | ||
| - **No shared state**: Each instance maintains its own cache, leading to redundant CDN calls | ||
| - **Scale constraints**: Large supergraphs with many queries can hit LRU cache limits quickly | ||
| - **No CDN resilience**: If the CDN is unavailable, there's no fallback layer | ||
|
|
||
| Teams with high-scale requirements often needed to build custom persisted document implementations | ||
| to use Redis or other distributed caches. | ||
|
|
||
| ## The Solution | ||
|
|
||
| Add a distributed cache (Redis, Memcached, etc.) as an L2 layer between the in-memory cache and CDN: | ||
|
|
||
| **L1 (memory) → L2 (Redis) → CDN** | ||
|
|
||
| ```typescript | ||
| import { createYoga } from 'graphql-yoga' | ||
| import { createClient } from 'redis' | ||
| import { useHive } from '@graphql-hive/yoga' | ||
|
|
||
| const redis = createClient({ url: 'redis://localhost:6379' }) | ||
| await redis.connect() | ||
|
|
||
| const yoga = createYoga({ | ||
| plugins: [ | ||
| useHive({ | ||
| experimental__persistedDocuments: { | ||
| cdn: { | ||
| endpoint: 'https://cdn.graphql-hive.com/artifacts/v1/<target_id>', | ||
| accessToken: '<cdn_access_token>' | ||
| }, | ||
| layer2Cache: { | ||
| cache: { | ||
| get: key => redis.get(`hive:pd:${key}`), | ||
| set: (key, value, opts) => | ||
| redis.set(`hive:pd:${key}`, value, opts?.ttl ? { EX: opts.ttl } : {}) | ||
| }, | ||
| ttlSeconds: 3600, | ||
| notFoundTtlSeconds: 60 | ||
| } | ||
| } | ||
| }) | ||
| ] | ||
| }) | ||
| ``` | ||
|
|
||
| ## Hive Gateway | ||
|
|
||
| For Hive Gateway, enable caching with just TTL options: | ||
|
|
||
| ```typescript | ||
| persistedDocuments: { | ||
| type: 'hive', | ||
| endpoint: 'https://cdn.graphql-hive.com/artifacts/v1/<target_id>', | ||
| token: '<cdn_access_token>', | ||
| cacheTtlSeconds: 3600, | ||
| cacheNotFoundTtlSeconds: 60 | ||
| } | ||
| ``` | ||
|
|
||
| Or via CLI: | ||
|
|
||
| ```bash | ||
| hive-gateway supergraph \ | ||
| --hive-persisted-documents-cache-ttl 3600 \ | ||
| --hive-persisted-documents-cache-not-found-ttl 60 | ||
| ``` | ||
|
|
||
| ## Features | ||
|
|
||
| | Option | Description | | ||
| | ------------------------------------------------ | ------------------------------------------------------------ | | ||
| | `ttlSeconds` / `cacheTtlSeconds` | TTL for found documents | | ||
| | `notFoundTtlSeconds` / `cacheNotFoundTtlSeconds` | TTL for not-found documents (negative caching). Default: 60s | | ||
| | `waitUntil` | Register cache writes in serverless environments | | ||
|
|
||
| - Apollo Server automatically uses `ctx.cache` as the L2 cache when available. | ||
| - Hive Gateway automatically uses the gateway cache when TTL options are provided. | ||
|
|
||
| --- | ||
|
|
||
| - [Learn more about App Deployments](/docs/schema-registry/app-deployments) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -336,6 +336,56 @@ const yoga = createYoga({ | |
| For further configuration options, please refer to the | ||
| [Hive Client API reference](/docs/api-reference/client#persisted-documents). | ||
|
|
||
| #### Layer 2 Cache (Optional) | ||
|
|
||
| For serverless environments or multi-instance deployments, you can add a Layer 2 (L2) cache between | ||
| the in-memory cache and the CDN. This is useful when in-memory cache is lost between invocations or | ||
| to share cached documents across server instances. | ||
|
|
||
| ```typescript filename="Persisted Documents with L2 Cache (Redis)" {14-24} | ||
| import { createYoga } from 'graphql-hive' | ||
| import { createClient } from 'redis' | ||
| import { useHive } from '@graphql-hive/yoga' | ||
| import schema from './schema.js' | ||
|
|
||
| const redis = createClient({ url: 'redis://localhost:6379' }) | ||
| await redis.connect() | ||
|
|
||
| const yoga = createYoga({ | ||
| schema, | ||
| plugins: [ | ||
| useHive({ | ||
| enabled: false, | ||
| experimental__persistedDocuments: { | ||
| cdn: { | ||
| endpoint: 'https://cdn.graphql-hive.com/artifacts/v1/<target_id>', | ||
| accessToken: '<cdn_access_token>' | ||
| }, | ||
| layer2Cache: { | ||
| cache: { | ||
| get: key => redis.get(`hive:pd:${key}`), | ||
| set: (key, value, opts) => | ||
| redis.set(`hive:pd:${key}`, value, opts?.ttl ? { EX: opts.ttl } : {}) | ||
| }, | ||
| ttlSeconds: 3600, // 1 hour for found documents | ||
| notFoundTtlSeconds: 60 // 1 minute for not-found (negative caching) | ||
| } | ||
| } | ||
| }) | ||
| ] | ||
| }) | ||
| ``` | ||
|
|
||
| The lookup flow is: **L1 (memory) -> L2 (Redis/external) -> CDN** | ||
|
|
||
| | Option | Description | | ||
| | -------------------- | ----------------------------------------------------------------------------------------------- | | ||
| | `cache.get` | Async function to get a value from the cache. Returns `null` for cache miss. | | ||
| | `cache.set` | Async function to set a value in the cache. Receives an optional `ttl` option. | | ||
| | `ttlSeconds` | TTL in seconds for successfully found documents. | | ||
| | `notFoundTtlSeconds` | TTL in seconds for not-found documents (negative caching). Set to `0` to disable. Default: `60` | | ||
| | `waitUntil` | Optional function for serverless environments to ensure cache writes complete. | | ||
|
|
||
| </Tabs.Tab> | ||
|
|
||
| {/* Apollo Server */} | ||
|
|
@@ -372,6 +422,44 @@ const server = new ApolloServer({ | |
| For further configuration options, please refer to the | ||
| [Hive Client API reference](/docs/api-reference/client#persisted-documents). | ||
|
|
||
| #### Layer 2 Cache (Optional) | ||
|
|
||
| Apollo Server automatically uses the server's context cache for L2 caching if available. You can | ||
| also configure a custom L2 cache: | ||
|
|
||
| ```typescript filename="Persisted Documents with L2 Cache" {10-20} | ||
| import { createClient } from 'redis' | ||
| import { ApolloServer } from '@apollo/server' | ||
| import { useHive } from '@graphql-hive/apollo' | ||
| import schema from './schema.js' | ||
|
|
||
| const redis = createClient({ url: 'redis://localhost:6379' }) | ||
| await redis.connect() | ||
|
|
||
| const server = new ApolloServer({ | ||
| schema, | ||
| plugins: [ | ||
| useHive({ | ||
| experimental__persistedDocuments: { | ||
| cdn: { | ||
| endpoint: 'https://cdn.graphql-hive.com/artifacts/v1/<target_id>', | ||
| accessToken: '<cdn_access_token>' | ||
| }, | ||
| layer2Cache: { | ||
| cache: { | ||
| get: key => redis.get(`hive:pd:${key}`), | ||
| set: (key, value, opts) => | ||
| redis.set(`hive:pd:${key}`, value, opts?.ttl ? { EX: opts.ttl } : {}) | ||
| }, | ||
| ttlSeconds: 3600, | ||
| notFoundTtlSeconds: 60 | ||
| } | ||
| } | ||
| }) | ||
| ] | ||
| }) | ||
| ``` | ||
|
|
||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For consistency with the GraphQL Yoga example and for completeness, it would be beneficial to add the lookup flow description and the options table here as well. |
||
| </Tabs.Tab> | ||
|
|
||
| {/* Apollo Router */} | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For consistency with the GraphQL Yoga example, it would be helpful to add comments explaining the TTL values here as well.