Cache management in GraphQL - Time & Space Complexity
Start learning this pattern below
Jump into concepts and practice - no test required
When using cache in GraphQL, we want to know how the time to get data changes as the data grows.
We ask: How does fetching from cache affect the speed when more data is stored?
Analyze the time complexity of the following GraphQL cache query snippet.
query GetUserData($id: ID!) {
user(id: $id) @cache(key: $id) {
id
name
posts {
id
title
}
}
}
This query fetches a user by ID using a cache key, then retrieves their posts.
Look for repeated actions that affect time.
- Primary operation: Searching the cache for the user data by key.
- How many times: Once per query, but inside, fetching posts loops over each post.
As the number of cached users grows, finding one user depends on the cache structure.
| Input Size (n users) | Approx. Operations |
|---|---|
| 10 | About 10 lookups if cache is simple |
| 100 | About 100 lookups if cache is simple |
| 1000 | About 1000 lookups if cache is simple |
Pattern observation: If cache is a list, time grows linearly with users. If cache uses a fast lookup (like a map), time stays almost the same.
Time Complexity: O(1)
This means fetching cached data by key takes about the same time no matter how many users are cached.
[X] Wrong: "Cache always makes queries faster regardless of how it is built."
[OK] Correct: If the cache is a simple list, searching can still take longer as data grows. The cache structure matters.
Understanding how cache lookup time changes with data size shows you know how to keep apps fast and scalable.
What if the cache was stored as a list instead of a map? How would the time complexity change?
Practice
Solution
Step 1: Understand cache management purpose
Cache management is used to store data temporarily to speed up access.Step 2: Compare options with cache purpose
Only To temporarily store data for faster repeated requests matches the temporary storage for faster repeated requests.Final Answer:
To temporarily store data for faster repeated requests -> Option AQuick Check:
Cache speeds up repeated requests = A [OK]
- Thinking cache stores data permanently
- Confusing cache with encryption
- Assuming cache deletes data immediately
Solution
Step 1: Identify correct syntax for cache key argument
The cache key argument uses @cacheKey with a key string in quotes.Step 2: Check each option's syntax
query { user(id: 1) @cacheKey(key: "id") { name }correctly uses @cacheKey(key: "id") with quotes around key name.Final Answer:
query { user(id: 1) @cacheKey(key: "id") { name } -> Option AQuick Check:
Cache key argument needs quotes = A [OK]
- Omitting quotes around key name
- Using wrong directive name like @cache
- Passing key without key: label
query { product(id: 5) @cacheControl(maxAge: 60) { name price } }What happens if you request the same product again within 30 seconds?
Solution
Step 1: Understand maxAge cache expiry
maxAge: 60 means cache is valid for 60 seconds after storing data.Step 2: Check request timing
Request within 30 seconds is before expiry, so cached data is used.Final Answer:
The cached data is returned without fetching from the server -> Option DQuick Check:
Request before maxAge returns cache = C [OK]
- Assuming cache expires immediately
- Thinking server always refetches
- Confusing maxAge with minimum age
query { user(id: 10) @cacheControl(maxAge: "thirty") { name email } }Solution
Step 1: Check maxAge argument type
maxAge expects a numeric value representing seconds, not a string.Step 2: Analyze the given value
"thirty" is a string, causing a type error in cacheControl directive.Final Answer:
maxAge value must be a number, not a string -> Option CQuick Check:
maxAge needs number, not string = B [OK]
- Using string instead of number for maxAge
- Confusing directive names
- Assuming id type causes cache error
Solution
Step 1: Understand caching by unique keys
Caching each post separately requires using a cache key based on post ID.Step 2: Evaluate options for separate caching
Only Use a cache key argument with the post ID to store each post individually uses cache key argument to store posts individually by ID.Final Answer:
Use a cache key argument with the post ID to store each post individually -> Option BQuick Check:
Cache by unique ID key = D [OK]
- Caching entire list as one entry
- Relying only on global expiry without keys
- Disabling cache unnecessarily
