GraphQL Caching: HTTP, Persisted Queries, and Clients
Compare HTTP, persisted-query, server, and normalized client caching for GraphQL, including cache keys, authorization boundaries, freshness, and invalidation.
Compare HTTP, persisted-query, server, and normalized client caching for GraphQL, including cache keys, authorization boundaries, freshness, and invalidation.
GraphQL Caching: HTTP, Persisted Queries, and Clients
A GraphQL response can vary with the operation, variables, caller, and cache policy, so the endpoint URL alone may not describe a safe cache key. Decide which layer will store each result and when it becomes stale. For a side-by-side API decision, see GraphQL vs REST.
HTTP and CDN Caching
HTTP caches work best when a request has a stable cache key and the response is safe to share. GraphQL queries are often sent as POST requests, which many shared caches do not store by default. Some clients and servers support GET for queries or persisted operations so a cache can identify a request from its URL. Apollo describes this pattern in its server-side caching guide.
A persisted query hash reduces the operation text sent over the network, but it is not automatically a safelist and does not make every result cacheable. Apollo distinguishes automatic persisted queries from registered persisted-query lists. Keep operation registration, authorization, and cache policy as separate decisions.
Before using a shared cache, make sure the key covers every value that affects the response, including variables and relevant headers. User-specific data should use a private cache policy or avoid shared caching. Include the user, tenant, locale, or permission scope in the cache policy when those values affect the response. A query hash identifies operation text; it does not distinguish callers.
Server-Side Caches
Resolver or data-source caches can avoid repeating an expensive lookup. A response cache can store a complete result when the operation’s fields share a compatible policy. Apollo Server supports field-level cache hints and response caching; its documentation describes maxAge and public or private scope. Choose an explicit freshness period, account for mutations that change the data, and clear or version cache entries when the source changes.
Avoid caching authorization decisions longer than the permissions they depend on. If a result combines public and private fields, use the strictest policy required by any field in that response. Test cache behavior as one user, then as a different user or tenant, before enabling shared storage.
Normalized Client Caches
Client libraries can normalize returned objects so separate queries refer to the same cached entity. Apollo Client’s default cache identity uses __typename together with id or _id; applications can configure key fields. See the official Apollo Client caching overview for its behavior.
Normalization helps when screens share entities, but a mutation must return enough identity and updated fields for the cache to reconcile them. Lists and derived results may still need an explicit update, eviction, or refetch. Subscriptions also need a deliberate policy for updating cached entities from incoming events.
Cache Review Checklist
- Choose the layer: browser, CDN, response cache, resolver/data source, or normalized client cache.
- Include every response-determining operation, variable, and authorization dimension in the cache policy.
- Mark user- or tenant-specific results private unless the shared cache isolates each scope.
- Set freshness and invalidation behavior for writes and external updates.
- Distinguish bandwidth optimization from an operation safelist; a hash is not an authorization check.
- Test one user’s response cannot be served to a different user with a similar query.
REST APIs often get simpler resource-level cache keys from their URLs and HTTP semantics; see RESTful API design. For access-control boundaries, see API authentication vs authorization.
Conclusion
GraphQL can use HTTP, server, and client caches, but each layer needs a policy that matches its key, authorization scope, freshness, and invalidation behavior. Treat persisted-query registration as a separate control from caching and authorization.
Category
Related Posts
Bloom Filters: Space-Efficient Probabilistic Data Structure
How Bloom filters provide memory-efficient membership testing with configurable false positive rates for caches, databases, and distributed systems.
Cache Eviction Policies: LRU, LFU, FIFO, and Random
Compare LRU, LFU, FIFO, and random cache eviction with a shared request trace, then review Redis's version-specific policy behavior.
Cache Patterns: Choosing Read and Write Strategies
Compare cache-aside, read-through, write-through, write-around, and write-behind patterns, their freshness trade-offs, and update races to plan for.