Scrivio Interview library
DebuggingIntermediate7 min read · updated 13 November 2026

A retail site moved its product pages from REST endpoints to one GraphQL endpoint. Since the release the CDN hit ratio for product data fell from 92 percent to almost zero, and origin traffic grew tenfold during a sale.

Explain why the CDN stopped helping, and how you would make GraphQL responses cacheable again.

The CDN stopped helping because clients send GraphQL queries as POST requests to a single /graphql URL, which CDNs do not cache, and even GET queries carry long, varying query strings, so the cache key rarely repeats. We make responses cacheable again with persisted queries called by id or hash over GET with variables in the URL, Cache-Control set per response from the cache hints of the fields involved with the shortest TTL winning and no shared caching of user-specific data, separate public and personalised queries, and server-side resolver caching with DataLoader batching to protect the origin meanwhile.

Why this question is asked

Moving to GraphQL is usually sold as a client-side win: fewer round trips and exactly the fields each page needs. This incident shows the cost on the other side. With REST, the CDN quietly absorbed 92 percent of product traffic; after the migration it absorbed almost none, and a sale sent ten times the traffic to the origin. Interviewers ask this to see whether we understand how HTTP caching actually works and how to make GraphQL play by its rules. In this answer, we will recap why REST cached so well, see why GraphQL broke it, and then rebuild caching with persisted queries, cache hints, query splitting and server-side protection.

1Why the REST Endpoints Cached So Well

A CDN caches responses close to users and uses the request method and URL as the cache key. With REST, a product page fetched GET /products/123, and every shopper looking at product 123 sent exactly that request. The origin returned the response with a header such as Cache-Control: public, max-age=60, so the CDN served the next minute of identical requests itself. With thousands of shoppers per popular product, 92 percent of requests never reached the origin.

Two properties made this work: the method was GET, which caches may store, and the URL was short and stable, so many users produced the same key.

2Why GraphQL Broke It

GraphQL clients usually send every query as a POST to one URL, /graphql, with the query text in the body. CDNs do not cache POST requests by default, because POST is meant for operations that change things, and the URL is the same for every query, so it says nothing about the content. So every request went to the origin.

Even clients that send queries with GET do not fix it. The whole query text goes into the URL, and small differences, such as field order, whitespace, an extra field on one page, or a different client version, produce different URLs for the same data. The cache key rarely repeats, so the hit ratio stays near zero.

Java
POST /graphql -> not cached (method)
{"query":"query { product(id: 123) { name price image } }"}
GET /graphql?query=query%20%7B%20product(id%3A%20123)%20%7B%20name%20price%20image%20%7D%20%7D
GET /graphql?query=query%20%7B%20product(id%3A123)%7Bname%20image%20price%7D%7D
-> two keys for the same data

3Persisted Queries Give Short, Stable Cache Keys

With persisted queries, the full query text is not sent at all. Queries are registered ahead of time, typically extracted from the client code at build time, and each gets an id or the SHA-256 hash of its text. The client then calls GET /graphql with only the id or hash and the variables. Every shopper viewing product 123 with the same app version now sends the same short URL, so the CDN can cache it again.

Java
GET /graphql?extensions={"persistedQuery":{"version":1,"sha256Hash":"9c1e...b7"}}&variables={"id":123}
-> 200 OK
Cache-Control: public, max-age=60
{"data":{"product":{"name":"Desk lamp","price":"39.00","image":"..."}}}
Step throughAutomatic persisted query with a CDN
ClientCDNGraphQL server
GET /graphql?hash=9c1e...&variables={id:123}
cache miss: forward
PersistedQueryNotFound
PersistedQueryNotFound
POST hash + full query (once)
data; server stores hash -> query
next shopper: GET same hash and variables
served from cache

8 messages in order. Play them, or step through them one at a time.

Automatic persisted queries are a variant that needs no build step. The client first sends only the hash. If the server does not know it, it replies with an error saying the query was not found, and the client sends the hash together with the full query once; the server stores it, and from then on the hash alone is enough.

4Setting Cache-Control From Field Hints

A stable URL is only half the job; the response must also tell caches how long it may be kept and by whom. In GraphQL, different fields have different freshness: a product name can be cached for an hour, a price for a minute, stock for a few seconds, and a shopper's cart not at all in a shared cache. Servers such as Apollo Server let the schema declare these as cache hints, and compute the response's Cache-Control from the fields actually requested: the shortest max-age wins, and if any field is private, the whole response is private.

Notice the consequence: one private field in a query, for example whether this shopper has the product on a wish list, makes the entire response uncacheable at the CDN. That is correct, because caching it would leak one shopper's data to others, and it is exactly why the next step matters. If your GraphQL server has no hint mechanism, the same rule can be implemented by tagging fields in the schema and computing the header in a response interceptor.

5Splitting Public and Personalised Queries

Product pages mix public data, the same for everyone, with personal data. If one query fetches both, the response is private and the CDN cannot help. So we split the page into two queries: a public catalogue query (name, price, images, reviews) that is persisted, sent with GET and cached at the CDN, and a small personalised query (wish list, recently viewed, member price) sent with the user's credentials and never cached at shared caches. The public query carries almost all the bytes and almost all the traffic, so caching it restores most of the old hit ratio.

6Protecting the Origin Meanwhile

Until CDN caching is back, the origin must survive. Two server-side techniques help immediately. Resolver result caching stores the results of expensive lookups, such as a product's details, in memory or Redis for a short time, so repeated queries do not hit the database. DataLoader batching collects all the lookups a query makes during one execution, for example 48 product ids on a category page, and fetches them in one database call instead of 48; in Spring for GraphQL, @BatchMapping methods provide this. Together they can cut database load by an order of magnitude, buying time for the persisted-query rollout.

7Follow-up questions

How do persisted queries also help with security?

If the server accepts only registered queries, an allowlist sometimes called trusted documents, clients can no longer send arbitrary queries. That blocks attackers from crafting very deep or very expensive queries to overload the service, and from probing the schema for fields the apps never use. Automatic persisted queries alone do not give this, because any client can register a new query; the security benefit comes from registering queries at build time and rejecting unknown ones in production.

How do we update a cached price when it changes before max-age expires?

Either keep max-age short for volatile fields, such as 30 to 60 seconds for prices, or use the CDN's purge API with surrogate keys: the response is tagged with keys such as product-123, and when the price changes, the service purges that key at the CDN.

Do persisted queries break when the app releases a new version?

No. A new app version registers its new queries before release, and old versions keep using their old hashes, so the server keeps both registered until old versions are no longer in use.

8Conclusion

In this answer, we saw that GraphQL broke CDN caching because queries go as POST to one URL and GET queries produce long, varying cache keys. Persisted queries called by hash over GET restore short, stable keys; Cache-Control computed from field cache hints, with the shortest TTL winning and private data never shared, makes responses safely cacheable; splitting public and personalised queries keeps the cacheable part cacheable; and resolver caching with DataLoader batching protects the origin while caching returns.

What to say in the interviewAbout 80 seconds aloud

Open with

The CDN caches by method and URL. With REST, every shopper sent GET /products/123, and with max-age headers the CDN served 92 percent of requests. After the migration, clients send queries as POST to one /graphql URL, and CDNs do not cache POST.

Then, if they want more

Even with GET, the full query text goes in the URL, and small differences in field order or app version make different keys, so the key almost never repeats.

To fix it, I would use persisted queries: queries are registered at build time, or with automatic persisted queries the client sends the SHA-256 hash first, and the client calls GET with just the hash and variables, which gives short, stable cache keys.

Then the server sets Cache-Control from the cache hints of the fields in the query, the shortest max-age wins, and any user-specific field makes the response private so it is never stored at shared caches.

Because one private field makes the whole response uncacheable, I would split the product page into a public catalogue query that is cached at the CDN and a small personalised query. While that rolls out, I would protect the origin with server-side caching of resolver results and DataLoader batching, so one page makes one database call for all its products.

About this answer

Last updated
13 November 2026
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the official documentation for the version you use. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "Explain why the CDN stopped helping, and how you would make GraphQL responses cacheable...": https://scrivio-interview-library.netlify.app/architecture/api-design-at-scale/b4916f00fb/