Cache invalidation is the process of removing or updating stored data in a cache when the original data changes, ensuring clients receive fresh and accurate information.
Click to reveal answer
beginner
Explain the 'Time-to-Live (TTL)' cache invalidation strategy.
TTL sets a fixed time period after which cached data expires automatically. After this time, the cache is refreshed with new data from the server.
Click to reveal answer
intermediate
What is 'Cache Busting' and how is it used?
Cache Busting involves changing the resource's URL (like adding a version number or timestamp) so that clients fetch the updated resource instead of the cached one.
Click to reveal answer
intermediate
Describe the 'Cache-Control' header role in cache invalidation.
The Cache-Control header tells clients and proxies how to cache responses, including how long to keep them and when to revalidate, helping control cache freshness.
Click to reveal answer
advanced
What is the difference between 'push' and 'pull' cache invalidation?
Push invalidation actively notifies caches to update or clear data when changes happen. Pull invalidation relies on clients or caches to check and refresh data after a set time or event.
Click to reveal answer
Which cache invalidation strategy uses a fixed expiration time for cached data?
AManual Refresh
BCache Busting
CPush Invalidation
DTime-to-Live (TTL)
✗ Incorrect
TTL sets a fixed time after which cached data expires automatically.
What does the Cache-Control header NOT control?
AThe URL of the cached resource
BHow long to cache data
CWhen to revalidate cached data
DWhether to allow caching
✗ Incorrect
Cache-Control does not change the resource URL; it controls caching behavior.
Cache Busting typically involves:
AUsing push notifications
BSetting a short TTL
CChanging the resource URL to force refresh
DDisabling cache completely
✗ Incorrect
Cache Busting changes the URL (like adding a version) to force clients to fetch fresh data.
Push cache invalidation means:
AClients request updates periodically
BServer notifies caches to update data
CCache expires after TTL
DCache is never invalidated
✗ Incorrect
Push invalidation actively informs caches to update or clear data.
Which strategy relies on clients or caches to check for updates themselves?
APull Invalidation
BManual Cache Clearing
CCache Busting
DPush Invalidation
✗ Incorrect
Pull invalidation depends on clients or caches to refresh data after some time or event.
Describe three common cache invalidation strategies used in REST APIs and when you might use each.
Think about automatic expiration, URL changes, and active vs passive updates.
You got /3 concepts.
Explain how the Cache-Control header helps manage cache invalidation in REST API responses.
Focus on how HTTP headers guide caching behavior.
You got /3 concepts.
Practice
(1/5)
1. Which cache invalidation strategy automatically removes cached data after a set period of time?
easy
A. Manual invalidation
B. Event-based invalidation
C. Time-based invalidation
D. Versioning invalidation
Solution
Step 1: Understand time-based invalidation
Time-based invalidation removes cached data after a fixed time, ensuring data freshness automatically.
Step 2: Compare with other strategies
Manual invalidation requires user action, event-based depends on data changes, and versioning uses data versions.
Final Answer:
Time-based invalidation -> Option C
Quick Check:
Automatic removal after time = Time-based invalidation [OK]
Hint: Time-based means cache expires after set time [OK]
Common Mistakes:
Confusing manual with automatic removal
Thinking event-based triggers on time
Mixing versioning with time expiry
2. Which of the following is the correct way to manually invalidate a cache entry in a REST API?
easy
A. Send a PUT request to update cached data
B. Send a GET request to the cache endpoint
C. Send a POST request to retrieve cached data
D. Send a DELETE request to the cache endpoint
Solution
Step 1: Identify HTTP method for deletion
DELETE requests are used to remove resources, including cache entries.
Step 2: Understand other methods
GET retrieves data, POST creates, and PUT updates; none remove cache.
Final Answer:
Send a DELETE request to the cache endpoint -> Option D
Quick Check:
DELETE removes cache entry manually [OK]
Hint: DELETE method removes resources like cache entries [OK]
Common Mistakes:
Using GET to remove cache
Confusing POST with deletion
Thinking PUT deletes cache
3. Consider this pseudo-code for event-based cache invalidation:
if (dataChangedEvent) {
invalidateCache(key);
}
return getCachedData(key);
What will happen if dataChangedEvent is true?
medium
A. Cache is invalidated and fresh data is fetched next time
B. Cache remains unchanged and stale data is returned
C. Cache is updated without invalidation
D. An error occurs because cache cannot be invalidated
Solution
Step 1: Analyze the condition
If dataChangedEvent is true, invalidateCache(key) runs, removing cached data.
Step 2: Understand cache behavior after invalidation
After invalidation, next data fetch will get fresh data, not stale cache.
Final Answer:
Cache is invalidated and fresh data is fetched next time -> Option A
Quick Check:
Event triggers invalidation = fresh data next fetch [OK]
Hint: Event true means cache cleared for fresh data [OK]
Common Mistakes:
Assuming cache stays after invalidation
Thinking cache updates automatically
Believing invalidation causes errors
4. You have this code snippet for versioning cache invalidation:
cache = { 'v1': data1 }
function getData(version) {
return cache[version];
}
// Later
cache['v2'] = newData;
What is the main bug if the client still requests v1 after update?
medium
A. Cache throws an error because version key is missing
B. Client gets stale data because old version is still cached
C. Cache automatically updates old version to new data
D. Client cannot access any cached data
Solution
Step 1: Understand versioning cache behavior
Old version 'v1' remains in cache, so requests for it get stale data.
Step 2: Identify bug impact
Client requesting 'v1' does not see 'v2' data, causing stale responses.
Final Answer:
Client gets stale data because old version is still cached -> Option B
Quick Check:
Old version key returns stale data [OK]
Hint: Old version keys keep stale data unless removed [OK]
Common Mistakes:
Assuming cache auto-updates old versions
Expecting errors on missing keys
Thinking client loses all cache access
5. You want to design a cache invalidation strategy for a REST API where data changes frequently but must stay fresh within 5 minutes. Which combined strategy is best?
hard
A. Use time-based invalidation with 5-minute expiry plus event-based invalidation on data changes
B. Use manual invalidation only, requiring user to clear cache
C. Use versioning only without time limits
D. Use time-based invalidation with 1-hour expiry only
Solution
Step 1: Analyze freshness requirement
Data must be fresh within 5 minutes, so time-based expiry should be 5 minutes or less.
Step 2: Combine with event-based invalidation
Event-based invalidation clears cache immediately on data changes, improving freshness.
Step 3: Evaluate other options
Manual invalidation is slow, versioning alone may keep stale data, and 1-hour expiry is too long.
Final Answer:
Use time-based invalidation with 5-minute expiry plus event-based invalidation on data changes -> Option A
Quick Check:
Combine time and event for fresh cache [OK]
Hint: Combine time and event for best freshness [OK]
Common Mistakes:
Relying only on manual invalidation
Using too long expiry times
Ignoring event triggers for changes
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.