0
0
Redisquery~30 mins

Cache-aside pattern in Redis - Mini Project: Build & Apply

Choose your learning style9 modes available
Implementing Cache-Aside Pattern with Redis
📖 Scenario: You are building a simple product information service. To improve performance, you want to use Redis as a cache to store product details temporarily. When a product is requested, the system should first check Redis cache. If the product is not found in cache, it should fetch from the main database (simulated here), store it in Redis, and then return the data.
🎯 Goal: Build a cache-aside pattern using Redis commands to fetch product data. You will create a product data dictionary, configure a Redis key prefix, implement the cache lookup and fallback to database retrieval, and finally set the cache expiration time.
📋 What You'll Learn
Create a dictionary called product_db with exact product entries.
Create a string variable called cache_key_prefix with the exact value 'product:'.
Write a Redis command to check if a product key exists in cache and if not, get it from product_db and set it in Redis.
Set the cache expiration time to exactly 300 seconds for the cached product data.
💡 Why This Matters
🌍 Real World
This pattern is used in web applications to speed up data retrieval by caching frequently accessed data in Redis.
💼 Career
Understanding cache-aside pattern is essential for backend developers and database engineers to optimize application performance.
Progress0 / 4 steps
1
Create the product database dictionary
Create a dictionary called product_db with these exact entries: '101': 'Red T-shirt', '102': 'Blue Jeans', '103': 'Black Sneakers'.
Redis
Need a hint?

Use curly braces to create a dictionary with keys as strings and values as strings.

2
Set the Redis cache key prefix
Create a string variable called cache_key_prefix and set it to the exact value 'product:'.
Redis
Need a hint?

Assign the string 'product:' to the variable cache_key_prefix.

3
Implement cache lookup and fallback to database
Write Redis commands to check if the key cache_key_prefix + '101' exists. If it does, get the value. If not, get the product from product_db['101'], set it in Redis with the key, and then get it.
Redis
Need a hint?

Use redis.exists() to check key, redis.get() to get value, and redis.set() to store value.

4
Set cache expiration time
Add a Redis command to set the expiration time of the key cache_key_prefix + '101' to exactly 300 seconds after setting the product in cache.
Redis
Need a hint?

Use redis.expire(key, seconds) to set the expiration time.