Cache warming strategies in Redis - Time & Space Complexity
When we warm a cache, we load data into it before it is needed. This helps speed up future requests.
We want to know how the time to warm the cache grows as the amount of data increases.
Analyze the time complexity of the following Redis cache warming code.
for i, key in ipairs(keys) do
local value = redis.call('GET', key)
if not value then
value = fetch_from_db(key)
redis.call('SET', key, value)
end
end
This code checks each key in a list. If the key is not in Redis, it fetches the data from the database and stores it in Redis.
- Primary operation: Looping over each key to check and set cache entries.
- How many times: Once for each key in the list.
As the number of keys grows, the code does more work because it checks each key one by one.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 checks and possible sets |
| 100 | About 100 checks and possible sets |
| 1000 | About 1000 checks and possible sets |
Pattern observation: The work grows directly with the number of keys; doubling keys doubles the work.
Time Complexity: O(n)
This means the time to warm the cache grows in a straight line with the number of keys.
[X] Wrong: "Cache warming is instant no matter how many keys we have."
[OK] Correct: Each key requires a check and possibly a fetch and set, so more keys mean more time.
Understanding how cache warming scales helps you design systems that stay fast as data grows. This skill shows you can think about real-world performance.
"What if we batch multiple keys in one Redis call instead of checking them one by one? How would the time complexity change?"
Practice
cache warming in Redis?Solution
Step 1: Understand cache warming concept
Cache warming means loading data into cache before users request it to speed up access.Step 2: Identify the correct purpose
Preloading data matches the definition of cache warming, unlike clearing, backing up, or monitoring.Final Answer:
To preload data into cache before it is requested -> Option CQuick Check:
Cache warming = preload data [OK]
- Confusing cache warming with cache clearing
- Thinking cache warming is about backups
- Mixing cache warming with monitoring tasks
Solution
Step 1: Recall Redis commands for setting data
SET sets one key-value pair; MSET sets multiple pairs at once.Step 2: Identify command for multiple keys
MSET is designed to preload many keys efficiently, perfect for cache warming.Final Answer:
MSET -> Option BQuick Check:
MSET = preload multiple keys [OK]
- Using GET instead of SET to preload data
- Confusing DEL (delete) with setting commands
- Using EXPIRE which only sets timeouts
MSET user:1 "Alice" user:2 "Bob" user:3 "Carol"
What will
GET user:2 return immediately after?Solution
Step 1: Understand MSET effect
MSET sets multiple keys: user:1 to "Alice", user:2 to "Bob", user:3 to "Carol".Step 2: Check GET user:2 result
GET user:2 returns the value set by MSET, which is "Bob".Final Answer:
"Bob" -> Option DQuick Check:
GET user:2 = "Bob" [OK]
- Assuming GET returns first key's value
- Expecting nil if not refreshed
- Confusing key names or values
MSET user:1 "Alice" user:2
What is the problem with this command?
Solution
Step 1: Check MSET argument rules
MSET expects pairs of key and value, so arguments count must be even.Step 2: Analyze given command
The command ends with key 'user:2' but no value, so arguments are odd.Final Answer:
MSET requires an even number of arguments -> Option AQuick Check:
MSET args count even = true [OK]
- Thinking MSET accepts single key without value
- Believing keys must have special prefixes
- Assuming MSET can set non-string values only
users = {"1": "Alice", "2": "Bob", "3": "Carol"}
active_users = ["1", "3"]Which Redis command sequence correctly warms cache only for active users?
Solution
Step 1: Identify active users to cache
Only users "1" and "3" are active, so only their data should be cached.Step 2: Choose command that sets only active users
MSET with keys user:1 and user:3 sets only active users, matching requirement.Final Answer:
MSET user:1 "Alice" user:3 "Carol" -> Option AQuick Check:
Cache only active users = MSET user:1 and user:3 [OK]
- Caching all users ignoring active status
- Using DEL or SET for inactive users incorrectly
- Confusing which users to preload
Ready to prove it?
Pass the test and this pattern moves unknown
