Cache invalidation strategies in Rest API - Time & Space Complexity
When using cache invalidation strategies in REST APIs, it's important to understand how the time to update or clear cache changes as the data grows.
We want to know how the cost of invalidating cache scales with the number of cached items or requests.
Analyze the time complexity of this cache invalidation example.
// Simple cache invalidation by key
function invalidateCache(keys) {
keys.forEach(key => {
cache.delete(key);
});
}
// keys is an array of cache keys to remove
This code removes cached items one by one by their keys.
Look at what repeats in this code.
- Primary operation: Looping over each cache key to delete.
- How many times: Once for each key in the input list.
As the number of keys to invalidate grows, the time to remove them grows too.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 deletions |
| 100 | 100 deletions |
| 1000 | 1000 deletions |
Pattern observation: The time grows directly with the number of keys to invalidate.
Time Complexity: O(n)
This means the time to invalidate cache grows linearly with the number of keys you want to remove.
[X] Wrong: "Invalidating cache keys is always instant, no matter how many keys there are."
[OK] Correct: Each key removal takes time, so more keys mean more work and longer time.
Understanding how cache invalidation scales helps you design efficient APIs and shows you can think about performance in real systems.
"What if the cache invalidation used a pattern to delete keys in bulk instead of one by one? How would the time complexity change?"
Practice
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 CQuick Check:
Automatic removal after time = Time-based invalidation [OK]
- Confusing manual with automatic removal
- Thinking event-based triggers on time
- Mixing versioning with time expiry
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 DQuick Check:
DELETE removes cache entry manually [OK]
- Using GET to remove cache
- Confusing POST with deletion
- Thinking PUT deletes cache
if (dataChangedEvent) {
invalidateCache(key);
}
return getCachedData(key);
What will happen if dataChangedEvent is true?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 AQuick Check:
Event triggers invalidation = fresh data next fetch [OK]
- Assuming cache stays after invalidation
- Thinking cache updates automatically
- Believing invalidation causes errors
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?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 BQuick Check:
Old version key returns stale data [OK]
- Assuming cache auto-updates old versions
- Expecting errors on missing keys
- Thinking client loses all cache access
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 AQuick Check:
Combine time and event for fresh cache [OK]
- Relying only on manual invalidation
- Using too long expiry times
- Ignoring event triggers for changes
Ready to prove it?
Pass the test and this pattern moves unknown
