Response caching strategies in GraphQL - Time & Space Complexity
Start learning this pattern below
Jump into concepts and practice - no test required
When using response caching in GraphQL, we want to know how the time to get data changes as requests grow.
We ask: How does caching affect the work done when many queries come in?
Analyze the time complexity of the following GraphQL query with response caching.
query GetBooks {
books {
id
title
author {
name
}
}
}
This query fetches a list of books with their authors. We consider caching the full response for repeated queries.
Look for repeated work when many queries happen.
- Primary operation: Fetching and assembling the list of books and authors from the database.
- How many times: Without caching, this happens every time the query runs. With caching, it happens once per unique query.
Think about how work changes as requests increase.
| Input Size (number of queries) | Approx. Operations |
|---|---|
| 10 | 10 database fetches without cache, 1 fetch with cache |
| 100 | 100 fetches without cache, 1 fetch with cache |
| 1000 | 1000 fetches without cache, 1 fetch with cache |
Pattern observation: Without caching, work grows linearly with queries. With caching, work stays almost the same after the first fetch.
Time Complexity: O(1)
This means after the first query, getting the response again takes about the same small amount of time, no matter how many queries come.
[X] Wrong: "Caching makes every query faster by skipping all work every time."
[OK] Correct: The first query still does all the work to build the response. Caching only helps for repeated identical queries.
Understanding how caching changes work helps you explain how to make APIs faster and handle many users smoothly.
"What if we cache parts of the response instead of the whole? How would that change the time complexity?"
Practice
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]
- Confusing caching with encryption
- Thinking caching controls permissions
- Believing caching logs queries
@cacheControl directive in GraphQL SDL?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]
- Using wrong argument names like duration or time
- Omitting the argument name
- Using invalid directive syntax
@cacheControl(maxAge: 120), what happens if the same query is requested twice within 2 minutes?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]
- Thinking cache expires immediately
- Assuming server errors on repeated queries
- Believing cache is ignored always
@cacheControl(maxAge: -10) on a field. What is the likely problem?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]
- Expecting syntax error from negative value
- Thinking negative disables caching explicitly
- Assuming negative means cache forever
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]
- Choosing too short cache time losing benefits
- Choosing too long cache time causing stale data
- Avoiding caching when moderate caching helps
