Cache invalidation strategies in Redis - Time & Space Complexity
When using Redis as a cache, removing or updating old data is important to keep things fresh.
We want to understand how the time to invalidate cache changes as the amount of cached data grows.
Analyze the time complexity of the following Redis commands for cache invalidation.
# Invalidate a single cache key
DEL cache:user:123
# Invalidate multiple keys matching a pattern
SCAN 0 MATCH cache:user:* COUNT 100
DEL cache:user:456
DEL cache:user:789
# Repeat SCAN and DEL until all keys are deleted
This code deletes either one cache entry or many entries matching a pattern by scanning and deleting keys.
Look for repeated actions that take time.
- Primary operation: Scanning keys with SCAN and deleting keys with DEL.
- How many times: SCAN repeats until all matching keys are found; DEL runs once per key to delete.
As the number of cached keys grows, the work to find and delete them grows too.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 keys | About 10 DEL commands plus a few SCAN calls |
| 100 keys | About 100 DEL commands plus more SCAN calls |
| 1000 keys | About 1000 DEL commands and many SCAN calls |
Pattern observation: The number of operations grows roughly in direct proportion to the number of keys to delete.
Time Complexity: O(n)
This means the time to invalidate cache grows linearly with the number of keys you want to remove.
[X] Wrong: "Deleting keys matching a pattern is instant no matter how many keys there are."
[OK] Correct: Redis must find each key first, which takes more time as keys increase, so deletion time grows with the number of keys.
Understanding how cache invalidation scales helps you design systems that stay fast and reliable as they grow.
"What if we used Redis key expiration (TTL) instead of manual deletion? How would the time complexity change?"
Practice
Solution
Step 1: Understand cache invalidation
Cache invalidation means removing or updating cached data to avoid stale information.Step 2: Identify the main goal
The goal is to keep the cached data fresh and accurate so users get the latest information.Final Answer:
To keep cached data fresh and accurate -> Option AQuick Check:
Cache invalidation = fresh data [OK]
- Thinking cache invalidation increases cache size
- Believing it slows down data retrieval
- Assuming cache data never changes
Solution
Step 1: Identify commands for cache invalidation
Manual invalidation requires deleting keys, so the command must remove keys.Step 2: Match command to action
DEL command deletes keys immediately, while EXPIRE sets a timeout, SETEX sets value with expiration, GET retrieves value.Final Answer:
DEL -> Option DQuick Check:
Delete key = DEL command [OK]
- Confusing EXPIRE with immediate deletion
- Using GET which only reads data
- Thinking SETEX deletes keys
SET user:1 "Alice" EX 10What happens after 10 seconds?
Solution
Step 1: Understand the EX option in SET
EX 10 means the key expires after 10 seconds automatically.Step 2: Identify what happens on expiration
When a key expires, Redis deletes it from memory automatically.Final Answer:
The key user:1 is deleted automatically -> Option BQuick Check:
EX sets expiration = auto delete [OK]
- Thinking key stays forever after expiration
- Believing value changes to null instead of deletion
- Assuming expired keys remain in memory
EXPIRE user:2 0What is the problem with this command?
Solution
Step 1: Understand EXPIRE with zero seconds
EXPIRE key 0 removes any existing expiration timeout (equivalent to PERSIST command).Step 2: Identify the problem
The intent was to expire the key, but this sets it to never expire instead.Final Answer:
It sets the key to never expire -> Option CQuick Check:
EXPIRE 0 = remove expiration [OK]
- Thinking it deletes the key immediately
- Believing it causes a syntax error
- Assuming zero seconds expires immediately
Solution
Step 1: Understand trade-offs of expiration and manual deletion
Short expiration causes frequent reloads; manual deletion alone risks stale data.Step 2: Choose balanced approach
Long expiration reduces reloads, manual invalidation on updates keeps data correct without overhead.Final Answer:
Use a long expiration time and rely on manual invalidation when data changes -> Option AQuick Check:
Balance speed and correctness = long expiry + manual invalidation [OK]
- Choosing very short expiration causing overhead
- Never expiring keys causing stale data
- Deleting keys after every read causing slow performance
Ready to prove it?
Pass the test and this pattern moves unknown
