📖 Scenario: You are managing a web application that uses Redis as a cache to speed up data retrieval. To improve user experience, you want to preload (warm) the cache with important data before users request it.
🎯 Goal: Build a simple Redis cache warming script that preloads specific keys with values, sets a time-to-live (TTL) for cache expiration, and verifies the cache is ready for fast access.
📋 What You'll Learn
Create initial Redis keys with exact values
Set a TTL (expiration time) for the cache keys
Use a loop to preload multiple keys
Verify the cache warming by checking key existence
💡 Why This Matters
🌍 Real World
Preloading cache data improves application speed and user experience by reducing delays on first data access.
💼 Career
Cache warming is a common task for backend developers and DevOps engineers working with Redis or other caching systems.
Progress0 / 4 steps
1
Create initial Redis keys with values
Use the Redis SET command to create three keys with these exact names and values: user:1001 with value "Alice", user:1002 with value "Bob", and user:1003 with value "Charlie".
Redis
Hint
Use the SET command followed by the key and value in quotes.
2
Set expiration time for cache keys
Use the Redis EXPIRE command to set a TTL of 3600 seconds (1 hour) for each of the keys: user:1001, user:1002, and user:1003.
Redis
Hint
Use the EXPIRE command with the key name and TTL in seconds.
3
Preload multiple keys using a loop
Use a Redis Lua script with a for loop to preload keys product:1 to product:3 with values "Item1", "Item2", and "Item3" respectively. Use redis.call('SET', key, value) inside the loop.
Redis
Hint
Use the EVAL command with a Lua script string that loops from 1 to 3 and sets keys.
4
Verify cache warming by checking key existence
Use the Redis EXISTS command to check if the keys user:1001 and product:2 exist in the cache.
Redis
Hint
Use the EXISTS command followed by the key name to check if it is in the cache.
Practice
(1/5)
1. What is the main purpose of cache warming in Redis?
easy
A. To clear all data from the cache
B. To backup Redis data to disk
C. To preload data into cache before it is requested
D. To monitor cache hit rates
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 C
Quick Check:
Cache warming = preload data [OK]
Hint: Cache warming means loading data early [OK]
Common Mistakes:
Confusing cache warming with cache clearing
Thinking cache warming is about backups
Mixing cache warming with monitoring tasks
2. Which Redis command is used to preload multiple key-value pairs for cache warming?
easy
A. GET
B. MSET
C. DEL
D. EXPIRE
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 B
Quick Check:
MSET = preload multiple keys [OK]
Hint: Use MSET to set many keys at once [OK]
Common Mistakes:
Using GET instead of SET to preload data
Confusing DEL (delete) with setting commands
Using EXPIRE which only sets timeouts
3. Given the Redis commands:
MSET user:1 "Alice" user:2 "Bob" user:3 "Carol"
What will GET user:2 return immediately after?
medium
A. "Alice"
B. Error
C. nil
D. "Bob"
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 D
Quick Check:
GET user:2 = "Bob" [OK]
Hint: MSET sets all keys, GET returns the exact value [OK]
Common Mistakes:
Assuming GET returns first key's value
Expecting nil if not refreshed
Confusing key names or values
4. You try to warm cache with:
MSET user:1 "Alice" user:2
What is the problem with this command?
medium
A. MSET requires an even number of arguments
B. MSET cannot set string values
C. MSET only accepts one key-value pair
D. MSET keys must start with 'cache:' prefix
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 A
Quick Check:
MSET args count even = true [OK]
Hint: MSET needs pairs: key and value [OK]
Common Mistakes:
Thinking MSET accepts single key without value
Believing keys must have special prefixes
Assuming MSET can set non-string values only
5. You want to warm cache with user data but only for users with active status. Given: