Repository navigation
draft: add response caching support #1628
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
Closed
Closed
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
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,135 @@ | ||
| # Live-subgraph testing report | ||
|
|
||
| Findings from running the execution engine's response caching against real Apollo-federation JS subgraphs, | ||
| 2026-08-11. | ||
| Suite written by an agent from a detailed guide; all headline findings re-verified by hand, | ||
| including an independent rerun reproducing the correctness bug. | ||
|
|
||
| ## Environment | ||
|
|
||
| - Suite: `apollo-response-caching/gqlt-live/` (standalone Go module, | ||
| `replace`-linked to the local graphql-go-tools checkout). | ||
| Run from that directory: `gotestsum --format=short -- ./... -count=1`. | ||
| - Live origins: the two Apollo-federation subgraphs from the apollo-response-caching workspace, | ||
| `products` on :4301 (`Cache-Control: max-age=60, public`) and `reviews` on :4302 (`max-age=30, public`), | ||
| each with a request counter and body log. | ||
| Both must be running (`node index.js`); the suite does not start them. | ||
| - Redis 7.2 container `apollo-cache-redis` on :6380 for the Redis-store subtests. | ||
| - Composition: wgc over SDL copies fetched from the live subgraphs. | ||
| wgc rejects Apollo's `@cacheTag` directive; | ||
| the copies drop it from the `@link` import and declare it locally, | ||
| preserving all applications. | ||
| Inert for what is tested: this engine's directive is `@cache`, so `@cacheTag` affects no key, TTL, or tag. | ||
| - Every datasource is reached through a transparent recording reverse proxy so tests pin exact wire bodies; | ||
| the subgraphs' own counters are the independent cross-check. | ||
| A control subtest runs one operation both proxied and direct | ||
| and asserts identical responses, store ops, and counter deltas. | ||
|
|
||
| Result: 14 subtests, 13 pass, 1 deliberate known-failure pinning a real engine bug (finding 1). | ||
| Stable across `-race` reruns. | ||
|
|
||
| ## Finding 1 — BUG: two calls of one cached root field in one operation share a key | ||
|
|
||
| ```graphql | ||
| query($x: Int, $y: Int) { one: topProducts(first: $x) { upc } two: topProducts(first: $y) { upc } } | ||
| # variables {"x":1,"y":3} | ||
| ``` | ||
|
|
||
| The cold response is correct, and the two isolated fetches are genuinely different at the origin | ||
| (wire bodies carry `{"a":1}` and `{"b":3}` after variable remapping). | ||
| But both fetches render the SAME cache key, both read it, and both write it — | ||
| one silently overwrites the other. | ||
| The warm request serves the surviving entry to BOTH aliases: | ||
| observed `{"one":[1],"two":[1]}` and `{"one":[1,2,3],"two":[1,2,3]}` across runs, | ||
| i.e. which alias gets corrupted is nondeterministic (whichever isolated fetch finishes last wins). | ||
|
|
||
| Root cause: `rootFieldCacheKey` (`v2/pkg/engine/cache/cache_key_template.go`) | ||
| builds the preimage from the coordinate plus `canonicalVariables(ctx)`, | ||
| which serializes the ENTIRE request variable set rather than the arguments this fetch sends. | ||
| Two calls of one coordinate in one operation therefore have byte-identical preimages. | ||
|
|
||
| This breaks two stated invariants: | ||
| "read key == write key == fetch identity" and "caching never changes a response". | ||
| Entity keys are unaffected (they render from representation values). | ||
| Fix direction: derive the root-field key from the argument values the fetch itself sends | ||
| (its rendered input), mirroring how entity keys already work. | ||
| Pinned by the deliberately-failing subtest [11]. | ||
|
|
||
| ## Finding 2 — root-field entries fragment by client variable name | ||
|
|
||
| `topProducts(first: 1)` inline, `query($first: Int)` + `{"first":1}`, and `query($n: Int)` + `{"n":1}` | ||
| produce three different keys, three misses, and three copies of one value — | ||
| while the recorded wire bodies to the origin are byte-identical. | ||
| Same root cause as finding 1: the preimage serializes client-named request variables. | ||
| Impact is hit rate and store size, not correctness. | ||
| Pinned as a passing characterization subtest [10] (documents current behavior, not a fix). | ||
|
|
||
| Doc impact: 2-behavior.md §3.7 ("variable names never affect keys") was contradicted by §3.5 | ||
| (root-field preimages include the name-sorted variable set); | ||
| the docs now carry the known-deviation note pointing here. | ||
|
|
||
| ## Finding 3 — coverage genuinely prevents ping-pong (works as designed) | ||
|
|
||
| The selection set is not part of a root-field key, so narrow and wide queries share one entry. | ||
| Sequence narrow → wide → narrow → wide: | ||
| the narrow query misses and writes; | ||
| the wide query gets a store HIT but coverage rejects the narrow entry and overwrites it wider; | ||
| both later queries serve from the wide entry. | ||
| Two origin requests total, then stable — | ||
| entries converge on the widest selection, narrower selections are projected down on serve. | ||
|
|
||
| ## Verified working (each with wire-body, op-log, and counter evidence) | ||
|
|
||
| - **Entity L2 lifecycle**: counter deltas `{products:1, reviews:1}` cold, `{1, 0}` warm; | ||
| read key == write key. | ||
| - **Header beats static config**: with `DefaultTTL: 5m` at the global, subgraph AND root-field tiers, | ||
| entries are written with `TTL: 60s` (products) / `30s` (reviews) and `cc.ttl` agrees — | ||
| the origin's `max-age` is the runtime truth. | ||
| - **Root-field caching**: an alias-variant operation is served from the same entry with zero deltas; | ||
| a different `first` argument misses into its own entry; | ||
| root-field entries carry the two coarse tags only. | ||
| - **Batch entity fetch**: one `GetMany` with 3 keys, one `SetMany` with 3 entries, | ||
| one wire request carrying all 3 representations; the warm run is a full hit. | ||
| - **Partial cache load**: with one product primed, the reviews origin received exactly | ||
| `{"representations":[{"__typename":"Product","upc":"2"}]}` | ||
| and only the missing entry was written back. | ||
| - **Client cache answer**: cold `MaxAge` is exactly `30s` (the minimum of 60/30, proving min-fold); | ||
| the warm countdown lands in `(25s, 30s]`; | ||
| one uncached fetch forces `NoStore` while the cacheable part is still stored. | ||
| - **Response invariance**: no-controller, cold-cached, and fully-warm runs are byte-identical. | ||
| - **Redis store (prototype)**: the envelope crosses into a real Redis verbatim with the header TTL; | ||
| a fresh controller serves from it; | ||
| delete-by-tag on the entity tag removes exactly one entry and forces a refetch. | ||
|
|
||
| ## Not tested, and why | ||
|
|
||
| Both origins serve one hardcoded `Cache-Control` and never vary it, | ||
| so `no-store` / `no-cache` / `private` / malformed directives / the `MaxTTL` clamp are unreachable live. | ||
| Making the two `index.js` files vary the header per operation | ||
| is the highest-value extension to this suite. | ||
| Also uncovered: the negative sentinel (no empty-entity path in the fixtures), | ||
| privacy partitions (no identity source), shadow mode, `@defer` L1 reuse, store-error fallback — | ||
| all of these are covered by the in-repo `execution/cachingtesting` suites against doubles. | ||
| The reviews subgraph's `extensions.invalidation` hint on `addReview` is ignored by this engine; | ||
| mutation-driven invalidation is documented out of scope. | ||
|
|
||
| ## Suite caveats | ||
|
|
||
| - `Mutation.addReview` permanently mutates the subgraph's in-memory data; | ||
| running it once invalidates pinned bodies until the node process restarts. | ||
| - Pinned keys embed the composition-assigned datasource ordinal (`v1:0` / `v1:1`); | ||
| adding a subgraph to `graph.yaml` re-pins them all. | ||
| - Subtests read shared origin counters and must stay sequential (no `t.Parallel`). | ||
|
|
||
| ## Notes for the router's Redis adapter (defects found in the suite's own prototype) | ||
|
|
||
| All three were caught and fixed during the suite's self-review; | ||
| they are exactly the traps the production adapter must avoid | ||
| (consistent with `4-redis-adapter-report.md` §3–§5): | ||
|
|
||
| - An unconditional tag-set `PEXPIRE` lets a short-lived entry SHORTEN a tag index's lifetime, | ||
| stranding longer-lived entries as permanently unpurgeable — use the `EXPIRE ... GT` form. | ||
| - Returning the tag set's cardinality from delete-by-tag over-reports; | ||
| return the deletion command's own count. | ||
| - A read helper that maps every error to "absent" makes | ||
| "the purge worked" satisfiable by "Redis is unreachable" — errors and misses must stay distinct. |
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,64 @@ | ||
| # Open issues | ||
|
|
||
| ## BUG: root-field cache keys derive from the request variable set, not the fetch's arguments | ||
|
|
||
| Found by live-subgraph testing 2026-08-11 (`5-testing-on-js-subgraphs.md`, findings 1 and 2). | ||
| `rootFieldCacheKey` hashes `{TypeName}.{FieldName}:{canonicalVariables(ctx)}` — the WHOLE request variable set. | ||
|
|
||
| Two consequences: | ||
|
|
||
| 1. CORRECTNESS: two aliased calls of one cached root field in one operation | ||
| (`one: topProducts(first:$x) two: topProducts(first:$y)`) render identical keys; | ||
| one write overwrites the other and the warm response serves the survivor to both aliases, | ||
| nondeterministically. | ||
| Pinned by the deliberately-failing subtest [11] in the gqlt-live suite. | ||
| 2. HIT RATE: client variable names are in the preimage, | ||
| so `($first:Int){"first":1}` and `($n:Int){"n":1}` fragment into separate entries | ||
| for byte-identical origin requests. | ||
|
|
||
| Fix direction: derive the key from the argument values the fetch itself sends | ||
| (its rendered input), mirroring entity keys. | ||
| Until fixed: do not configure root-field caching for fields a single operation may call twice. | ||
|
|
||
| ## Open design: detecting the NEED for invalidation on schema deploys | ||
|
|
||
| The engine has NO schema-drift guard (the envelope `types` map was dropped 2026-08-10): | ||
| an entry written before a schema change is served as long as it decodes and covers the selection. | ||
| Undecided: whether/how a schema publish should purge affected entries — e.g. diff the composed schema at publish | ||
| time and fire `type:{subgraph}:{Type}` tag invalidations for types whose field shapes changed. | ||
| Belongs to the router/composition layer; the tag vocabulary already supports it. | ||
|
|
||
| ## Follow-up: honor `s-maxage` in the header-driven caching model | ||
|
|
||
| The router cache is a SHARED cache, so the semantically correct reading of `Cache-Control: max-age=30, s-maxage=600` | ||
| is: router entry TTL = 600s (`s-maxage` wins for shared caches), client-facing aggregated header = 30s (`max-age`). | ||
| Useful pattern once tag invalidation exists: long router TTL + active purge, short client TTL. | ||
| Deliberately out of v1 (`docs/caching/specs/2026-08-06-header-driven-caching.md` §4.1): it needs a second freshness | ||
| number through the pipeline — entry TTL from `s-maxage`, plus a separate `client_ttl` in the value envelope's `cc` | ||
| metadata for the §10 client-header aggregation. | ||
| Additive change; implement when the long-edge/short-client pattern is requested. | ||
|
|
||
| ## Check: entity interface caching support | ||
|
|
||
| We need to verify whether caching works (and should be supported) for entity interfaces under the representation-derived single-key model | ||
| (implemented 2026-08-04; described in `1-readme.md`, spec text in git history of the 98dff2032..9f15c5c6c commit series). | ||
|
|
||
| Established (verified in `plan/representationvariable/representation_variable.go`): | ||
|
|
||
| - `BuildRepresentationVariableNode` bakes the interfaceObject/entity-interface `__typename` remap into the node: | ||
| for an `@interfaceObject` target the node's `__typename` field is a `resolve.StaticString{Value: <InterfaceTypeName>}`, not an item read. | ||
| - Consequence for representation-derived keys: the key renderer must take `__typename` from the NODE | ||
| (static value when present, item value otherwise), not unconditionally from the item — | ||
| then interfaceObject fetches key under the interface name automatically, inheriting the planner's remap for free. | ||
|
|
||
| Still to answer: | ||
|
|
||
| - Cross-subgraph identity split: a concrete-type subgraph keys the entity as `Article` while the `@interfaceObject` subgraph keys it as `Personalized` — | ||
| disjoint entries for the same logical entity. | ||
| Likely CORRECT (different subgraphs provide different data, and per-policy CacheName usually separates them anyway), but decide and document. | ||
| - The `OnTypeNames`-aware key renderer (merged representation nodes for abstract-type batch fetches) must be tested against interface entity fixtures, | ||
| not only union/concrete ones. | ||
| - Coverage walk (`covers` / `skipFieldForTypeName`): verify type-conditioned fields behave for interface fetches where the cached value's `__typename` | ||
| is a concrete implementer but ProvidesData fields are conditioned on the interface. | ||
| - Fixtures: current caching subgraphs have no interface entities; add one (e.g. `Personalized`-style interface with two implementers, one `@interfaceObject` subgraph) | ||
| before claiming support either way. | ||
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.
🎯 Functional Correctness | 🟠 Major | 🏗️ Heavy lift
Block root-field caching until the key uses fetch arguments.
The documented failure serves one root-field result to another alias when both calls use different arguments. This changes a valid GraphQL response.
Derive the key from the rendered arguments of this fetch. If that fix cannot ship now, reject or disable root-field cache configuration. Documentation alone cannot prevent this corruption.
🤖 Prompt for AI Agents