Response caching helps save time by storing answers to repeated questions. This means faster replies and less work for the server.
Response caching strategies in GraphQL
Start learning this pattern below
Jump into concepts and practice - no test required
or
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Introduction
Syntax
GraphQL
type Query {
book(id: ID!): Book @cacheControl(maxAge: 60)
}
# or using HTTP headers for caching
# Cache-Control: max-age=60Use
@cacheControl(maxAge: seconds) directive in GraphQL schema to set caching time.HTTP headers like
Cache-Control can also control caching behavior.Examples
GraphQL
type Query {
user(id: ID!): User @cacheControl(maxAge: 120)
}GraphQL
type Query {
latestNews: [News] @cacheControl(maxAge: 30)
}GraphQL
# HTTP header example
Cache-Control: max-age=300Sample Program
This GraphQL query asks for popular books. The @cacheControl(maxAge: 60) directive caches the response for 60 seconds, so repeated requests within that time get faster answers.
GraphQL
type Query {
popularBooks: [Book] @cacheControl(maxAge: 60)
}
# Query
{
popularBooks {
title
author
}
}Important Notes
Cache duration should match how often data changes to avoid showing old info.
Use caching carefully for sensitive or user-specific data to avoid leaks.
Combining server-side and client-side caching can improve performance.
Summary
Response caching stores answers to speed up repeated requests.
Use @cacheControl directive or HTTP headers to set caching time.
Choose caching times based on how fresh your data needs to be.
Practice
1. What is the main purpose of response caching in GraphQL?
easy
Solution
Step 1: Understand response caching concept
Response caching saves the answers of queries so that if the same query is asked again, the server can quickly return the saved answer instead of recalculating it.Step 2: Identify the main benefit
This speeds up repeated requests and reduces server load.Final Answer:
To store query results and speed up repeated requests -> Option AQuick Check:
Response caching = store and speed up [OK]
Hint: Caching saves answers to reuse later, speeding up requests [OK]
Common Mistakes:
- Confusing caching with encryption
- Thinking caching controls permissions
- Believing caching logs queries
2. Which of the following is the correct way to set a cache duration using the
@cacheControl directive in GraphQL SDL?easy
Solution
Step 1: Recall the correct directive syntax
The@cacheControldirective uses the argumentmaxAgeto specify cache duration in seconds.Step 2: Match the correct argument name
OnlymaxAgeis valid; other argument names likeduration,cacheTime, ortimeare incorrect.Final Answer:
@cacheControl(maxAge: 60) -> Option BQuick Check:
@cacheControl uses maxAge [OK]
Hint: Use maxAge to set cache seconds in @cacheControl [OK]
Common Mistakes:
- Using wrong argument names like duration or time
- Omitting the argument name
- Using invalid directive syntax
3. Given this GraphQL query with caching set to
@cacheControl(maxAge: 120), what happens if the same query is requested twice within 2 minutes?medium
Solution
Step 1: Understand maxAge meaning
ThemaxAge: 120means the response is cached for 120 seconds (2 minutes).Step 2: Analyze repeated request timing
If the second request happens within 2 minutes, the cached response is still valid and returned immediately.Final Answer:
The server returns the cached response on the second request -> Option AQuick Check:
maxAge 120 means cache valid 2 minutes [OK]
Hint: Within maxAge, cached response is reused [OK]
Common Mistakes:
- Thinking cache expires immediately
- Assuming server errors on repeated queries
- Believing cache is ignored always
4. You set
@cacheControl(maxAge: -10) on a field. What is the likely problem?medium
Solution
Step 1: Understand maxAge value meaning
maxAge defines how long the response is cached in seconds. Negative values are invalid for duration.Step 2: Interpret negative maxAge effect
Negative maxAge is treated as cache expired immediately, so no caching occurs effectively.Final Answer:
Negative maxAge causes cache to expire immediately -> Option DQuick Check:
Negative maxAge means cache expires instantly [OK]
Hint: Negative maxAge means cache expires right away [OK]
Common Mistakes:
- Expecting syntax error from negative value
- Thinking negative disables caching explicitly
- Assuming negative means cache forever
5. You want to cache a GraphQL query response for a user profile that updates frequently but not every second. Which caching strategy is best?
hard
Solution
Step 1: Consider data freshness needs
User profiles update frequently but not every second, so caching too long risks stale data.Step 2: Choose a balanced cache duration
5 seconds is too short to gain caching benefits; 1 hour is too long risking stale data. 5 minutes (300 seconds) balances freshness and performance.Final Answer:
Set @cacheControl(maxAge: 300) to cache for 5 minutes -> Option CQuick Check:
Moderate maxAge balances freshness and speed [OK]
Hint: Pick cache time balancing freshness and speed [OK]
Common Mistakes:
- Choosing too short cache time losing benefits
- Choosing too long cache time causing stale data
- Avoiding caching when moderate caching helps
