-
Notifications
You must be signed in to change notification settings - Fork 235
feat(router): entity caching with L1/L2, shadow mode, and analytics #2777
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
jensneuse
wants to merge
19
commits into
main
Choose a base branch
from
jensneuse/entity-caching-v2
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 2 commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
2958f1d
feat(router): entity caching with L1/L2, shadow mode, and analytics
jensneuse 4bf4040
fix(entity-caching): address PR #2777 review items across composition…
jensneuse 49fb668
Merge remote-tracking branch 'origin/main' into jensneuse/entity-cach…
jensneuse 9dd235a
fix(entity-caching): CI follow-ups — prettier + gqlgen-safe var place…
jensneuse 61f64a1
fix(entity-caching): second round of PR #2777 review items
jensneuse de8a611
fix(composition): run prettier on AGENTS.md
jensneuse 78d9c32
docs: add entity caching pre-release test kit
jensneuse 699c310
fix(composition): third round of PR #2777 review items (#9, #10, #11,…
jensneuse 23d748c
docs: make entity caching pre-release kit self-contained
jensneuse 294b2d9
fix(router): opt-in for entity cache telemetry (PR #2777 review #21)
jensneuse 03cc34f
docs: use Yoga for pre-release test subgraph
jensneuse 9664044
fix(router-tests): entity cache test cleanup (PR #2777 review #3, #4,…
jensneuse 986bca7
docs: load pre-release test schema from SDL
jensneuse 3dc656e
fix(demo): demo hardening (PR #2777 review #13, #14, #15)
jensneuse 3dfab5d
fix(benchmark,docs): rename mode + document TTL divergence (PR #2777 …
jensneuse bc4787a
docs(entity-caching): clarify Coordinate L1 vs Entity L1 (PR #2777 re…
jensneuse 898d8f9
docs: harden entity caching smoke test
jensneuse e966bf8
docs: document entity caching PR checkout
jensneuse 87fe366
docs: explain pre-release cache configuration
jensneuse 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
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
Large diffs are not rendered by default.
Oops, something went wrong.
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
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,5 @@ | ||
| .run/ | ||
| .tmp-metrics/ | ||
| .tmp-pprof/ | ||
| results/* | ||
| !results/.gitkeep |
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,114 @@ | ||
| # Cache Demo Benchmark Suite | ||
|
|
||
| Local benchmark harness for the cache demo on `localhost:3002`, using: | ||
|
|
||
| - the existing `cache-demo` subgraphs | ||
| - a dedicated Redis Docker container for L2 cache storage on `localhost:6399` | ||
| - k6 for request load | ||
| - router Prometheus and pprof for runtime capture | ||
|
|
||
| ## Prerequisites | ||
|
|
||
| - `go` with the repo’s expected toolchain | ||
| - `pnpm` | ||
| - `k6` | ||
| - `docker` | ||
| - free local ports: | ||
| - `3002` | ||
| - `4012` | ||
| - `4013` | ||
| - `4014` | ||
| - `6060` | ||
| - `8088` | ||
| - `6399` | ||
|
|
||
| ## Key Files | ||
|
|
||
| - `benchmark/router-cache.redis.yaml`: Redis-backed router config for the benchmark | ||
| - `benchmark/scenarios/cache-demo.json`: scenario manifest | ||
| - `benchmark/queries/*.graphql`: canonical benchmark operations | ||
| - `benchmark/fixtures/*.response.json`: uncached router response fixtures | ||
| - `benchmark/k6/cache_demo.js`: k6 runner with exact response assertion | ||
|
|
||
| ## Scenarios | ||
|
|
||
| - `article_simple` | ||
| - `articles_by_ids_batch` | ||
| - `listing_composite_key` | ||
| - `venue_nested_key` | ||
| - `user_profile_header_sensitive` | ||
| - `catalogs_partial_load` | ||
| - `request_scoped_viewer_articles` | ||
| - `viewer_articles_deep_nested` | ||
|
|
||
| ## Auth Profiles | ||
|
|
||
| The demo uses fake bearer tokens: | ||
|
|
||
| - `alice` -> `Bearer token-alice` | ||
| - `bob` -> `Bearer token-bob` | ||
| - `charlie` -> `Bearer token-charlie` | ||
|
|
||
| Auth-sensitive scenarios must not fall back to anonymous requests. | ||
|
|
||
| ## Commands | ||
|
|
||
| Validate the manifest and checked-in fixtures: | ||
|
|
||
| ```bash | ||
| make benchmark-cache-demo-validate | ||
| ``` | ||
|
|
||
| Run the full suite with default load settings: | ||
|
|
||
| ```bash | ||
| make benchmark-cache-demo | ||
| ``` | ||
|
|
||
| Run one scenario with default load settings: | ||
|
|
||
| ```bash | ||
| make benchmark-cache-demo-scenario SCENARIO=article_simple | ||
| ``` | ||
|
|
||
| Run one scenario with direct control over k6 stages: | ||
|
|
||
| ```bash | ||
| pnpm dlx tsx benchmark/scripts/run_suite.ts \ | ||
| --scenario article_simple \ | ||
| --vus 10 \ | ||
| --duration 30s \ | ||
| --ramp-up 5s \ | ||
| --ramp-down 5s | ||
| ``` | ||
|
|
||
| ## Output Layout | ||
|
|
||
| Result bundles are written under: | ||
|
|
||
| ```text | ||
| benchmark/results/<timestamp>/<scenario>/<mode>/ | ||
| ``` | ||
|
|
||
| Each mode directory contains: | ||
|
|
||
| - `summary.json` | ||
| - `k6-summary.json` | ||
| - `metrics-before.prom` | ||
| - `metrics-after.prom` | ||
| - `metrics-delta.json` | ||
| - `redis-info-before.txt` | ||
| - `redis-info-after.txt` | ||
| - `redis-docker-stats-before.json` | ||
| - `redis-docker-stats-after.json` | ||
| - `equivalence.json` | ||
| - `pprof/router_cpu.pb.gz` | ||
| - `pprof/router_heap.pb.gz` | ||
|
|
||
| `summary.json` also records the warmup request count, k6 stage config, parsed k6 summary, and selected Redis INFO and Docker stats values so downstream interpretation does not need to scrape raw artifacts first. | ||
|
|
||
| ## Notes | ||
|
|
||
| - The harness owns the stack. If the required ports are already in use, startup fails rather than benchmarking against a dirty environment. | ||
| - L2 cache storage is intentionally externalized to Redis so router memory measurements do not include the full L2 object footprint. | ||
| - The suite always performs deterministic uncached equivalence checks before load generation and serial warmup requests before each mode run. |
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 @@ | ||
| {"data":{"article":{"id":"1","title":"Introduction to GraphQL Caching","authorName":"Alice"}}} |
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 @@ | ||
| {"data":{"articlesByIds":[{"id":"1","title":"Introduction to GraphQL Caching","authorName":"Alice"},{"id":"2","title":"Advanced Federation Patterns","authorName":"Bob"},{"id":"3","title":"Cache Invalidation Strategies","authorName":"Charlie"}]}} |
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 @@ | ||
| {"data":{"catalogs":[{"id":"c1","name":"Electronics","description":"Consumer electronics, gadgets, and accessories.","lastUpdated":"2025-03-15T08:00:00Z","itemCount":342},{"id":"c2","name":"Books","description":"Fiction, non-fiction, technical books, and audiobooks.","lastUpdated":"2025-03-20T12:00:00Z","itemCount":1205},{"id":"c3","name":"Clothing","description":"Men's, women's, and children's apparel.","lastUpdated":"2025-03-25T16:00:00Z","itemCount":567}]}} |
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 @@ | ||
| {"data":{"listing":{"sellerId":"s1","sku":"WIDGET-01","title":"Premium Widget","price":29.99,"currency":"USD","inStock":true}}} |
1 change: 1 addition & 0 deletions
1
benchmark/fixtures/request_scoped_viewer_articles.response.json
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 @@ | ||
| {"data":{"currentViewer":{"id":"v1","name":"Alice","email":"alice@example.com"},"articles":[{"id":"1","title":"Introduction to GraphQL Caching","currentViewer":{"id":"v1","name":"Alice"},"viewCount":12453,"rating":4.7},{"id":"2","title":"Advanced Federation Patterns","currentViewer":{"id":"v1","name":"Alice"},"viewCount":8921,"rating":4.3},{"id":"3","title":"Cache Invalidation Strategies","currentViewer":{"id":"v1","name":"Alice"},"viewCount":15678,"rating":4.9},{"id":"4","title":"Performance Tuning with Entity Caching","currentViewer":{"id":"v1","name":"Alice"},"viewCount":6234,"rating":4.1}]}} |
1 change: 1 addition & 0 deletions
1
benchmark/fixtures/user_profile_header_sensitive.alice.response.json
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 @@ | ||
| {"data":{"userProfile":{"id":"u1","username":"alice","role":"admin","email":"alice@example.com"}}} |
1 change: 1 addition & 0 deletions
1
benchmark/fixtures/user_profile_header_sensitive.bob.response.json
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 @@ | ||
| {"data":{"userProfile":{"id":"u2","username":"bob","role":"editor","email":"bob@example.com"}}} |
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 @@ | ||
| {"data":{"venue":{"address":{"id":"v1"},"name":"Grand Conference Hall","capacity":500,"city":"Berlin"}}} |
Large diffs are not rendered by default.
Oops, something went wrong.
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,123 @@ | ||
| import http from "k6/http"; | ||
| import { check, fail } from "k6"; | ||
| import { Counter, Rate } from "k6/metrics"; | ||
|
|
||
| const payload = JSON.parse(__ENV.BENCHMARK_PAYLOAD || "{}"); | ||
|
|
||
| if (!payload.url || !payload.query) { | ||
| fail("BENCHMARK_PAYLOAD must include url and query"); | ||
| } | ||
|
|
||
| const mismatchRate = new Rate("response_mismatch_rate"); | ||
| const graphqlErrorRate = new Rate("graphql_error_rate"); | ||
| const mismatchCount = new Counter("response_mismatch_count"); | ||
|
|
||
| function normalizeJson(value) { | ||
| if (Array.isArray(value)) { | ||
| return value.map((item) => normalizeJson(item)); | ||
| } | ||
|
|
||
| if (value !== null && typeof value === "object") { | ||
| const out = {}; | ||
| for (const key of Object.keys(value).sort()) { | ||
| out[key] = normalizeJson(value[key]); | ||
| } | ||
| return out; | ||
| } | ||
|
|
||
| return value; | ||
| } | ||
|
|
||
| function normalizeResponseForComparison(value) { | ||
| if (value === null || typeof value !== "object" || Array.isArray(value)) { | ||
| return normalizeJson(value); | ||
| } | ||
|
|
||
| const response = JSON.parse(JSON.stringify(value)); | ||
| const extensions = response.extensions; | ||
|
|
||
| if (extensions && typeof extensions === "object" && !Array.isArray(extensions)) { | ||
| delete extensions.trace; | ||
|
|
||
| if (Object.keys(extensions).length === 0) { | ||
| delete response.extensions; | ||
| } | ||
| } | ||
|
|
||
| return normalizeJson(response); | ||
| } | ||
|
|
||
| const expectedBody = JSON.stringify( | ||
| normalizeResponseForComparison(payload.expectedBody), | ||
| ); | ||
|
|
||
| export const options = payload.options || { | ||
| stages: [ | ||
| { duration: "30s", target: 20 }, | ||
| { duration: "2m", target: 20 }, | ||
| { duration: "10s", target: 0 }, | ||
| ], | ||
| }; | ||
|
|
||
| export default function () { | ||
| const mergedHeaders = Object.assign( | ||
| { "content-type": "application/json" }, | ||
| payload.headers || {}, | ||
| ); | ||
|
|
||
| const response = http.post( | ||
| payload.url, | ||
| JSON.stringify({ | ||
| operationName: payload.operationName, | ||
| query: payload.query, | ||
| variables: payload.variables || {}, | ||
| }), | ||
| { | ||
| headers: mergedHeaders, | ||
| }, | ||
| ); | ||
|
|
||
| const ok = check(response, { | ||
| "http status is 200": (res) => res.status === 200, | ||
| }); | ||
|
|
||
| if (!ok) { | ||
| mismatchRate.add(true); | ||
| mismatchCount.add(1); | ||
| graphqlErrorRate.add(false); | ||
| return; | ||
| } | ||
|
|
||
| let body; | ||
| try { | ||
| body = response.json(); | ||
| } catch (_err) { | ||
| // Parse failure is both a response mismatch and a graphql-error-equivalent | ||
| // (the server returned a non-JSON body or truncated JSON under load). | ||
| mismatchRate.add(true); | ||
| mismatchCount.add(1); | ||
| graphqlErrorRate.add(true); | ||
| return; | ||
| } | ||
|
|
||
| const hasGraphqlErrors = Array.isArray(body?.errors) && body.errors.length > 0; | ||
| graphqlErrorRate.add(hasGraphqlErrors); | ||
|
|
||
| const sameBody = | ||
| JSON.stringify(normalizeResponseForComparison(body)) === expectedBody; | ||
|
|
||
| const assertionOk = check( | ||
| { body, sameBody, hasGraphqlErrors }, | ||
| { | ||
| "graphql errors absent": (data) => !data.hasGraphqlErrors, | ||
| "response matches expected fixture": (data) => data.sameBody, | ||
| }, | ||
| ); | ||
|
|
||
| if (!assertionOk || !sameBody) { | ||
| mismatchRate.add(true); | ||
| mismatchCount.add(1); | ||
| } else { | ||
| mismatchRate.add(false); | ||
| } | ||
| } |
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,7 @@ | ||
| query ArticleSimple($id: ID!) { | ||
| article(id: $id) { | ||
| id | ||
| title | ||
| authorName | ||
| } | ||
| } |
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,7 @@ | ||
| query ArticlesByIdsBatch($ids: [ID!]!) { | ||
| articlesByIds(ids: $ids) { | ||
| id | ||
| title | ||
| authorName | ||
| } | ||
| } |
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,9 @@ | ||
| query CatalogsPartialLoad { | ||
| catalogs { | ||
| id | ||
| name | ||
| description | ||
| lastUpdated | ||
| itemCount | ||
| } | ||
| } |
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,10 @@ | ||
| query ListingCompositeKey($key: ListingKey!) { | ||
| listing(key: $key) { | ||
| sellerId | ||
| sku | ||
| title | ||
| price | ||
| currency | ||
| inStock | ||
| } | ||
| } |
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,17 @@ | ||
| query RequestScopedViewerArticles { | ||
| currentViewer { | ||
| id | ||
| name | ||
| } | ||
| articles { | ||
| id | ||
| title | ||
| currentViewer { | ||
| id | ||
| name | ||
| } | ||
| viewCount | ||
| rating | ||
| } | ||
| } |
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,8 @@ | ||
| query UserProfileHeaderSensitive($id: ID!) { | ||
| userProfile(id: $id) { | ||
| id | ||
| username | ||
| role | ||
| } | ||
| } |
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,10 @@ | ||
| query VenueNestedKey($location: VenueLocationKey!) { | ||
| venue(location: $location) { | ||
| address { | ||
| id | ||
| } | ||
| name | ||
| capacity | ||
| city | ||
| } | ||
| } |
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.