📖 Scenario: You are setting up a Redis database for a small online store. Redis will store product stock counts so the store can quickly check availability.
🎯 Goal: Build a simple Redis setup where you create initial stock data, configure a threshold for low stock, check stock levels, and mark products as low stock if needed.
📋 What You'll Learn
Create a Redis hash called product_stock with exact product stock counts
Add a configuration variable low_stock_threshold with value 5
Write a Redis Lua script that checks each product's stock against low_stock_threshold
Add a Redis set low_stock_products to store products below the threshold
💡 Why This Matters
🌍 Real World
Retailers use Redis to quickly check product availability and alert when stock is low to reorder.
💼 Career
Understanding Redis data structures and Lua scripting is valuable for backend developers working on fast, scalable inventory systems.
Progress0 / 4 steps
1
DATA SETUP: Create initial product stock data
Create a Redis hash called product_stock with these exact entries: "apple": "10", "banana": "3", "orange": "7".
Redis
Hint
Use the HSET command to create a hash with multiple fields and values.
2
CONFIGURATION: Set low stock threshold
Create a Redis string key called low_stock_threshold and set its value to 5.
Redis
Hint
Use the SET command to store a simple configuration value.
3
CORE LOGIC: Check stock levels with Lua script
Write a Redis Lua script that iterates over all products in product_stock, compares each stock count to the low_stock_threshold, and returns a list of products below the threshold. Use redis.call and for loop with variables i and product.
Redis
Hint
Use HKEYS to get all product names, GET to get the threshold, and loop through products to compare stock.
4
COMPLETION: Store low stock products in a Redis set
Use the Lua script to add each product below the threshold into a Redis set called low_stock_products using redis.call('SADD', 'low_stock_products', product) inside the loop.
Redis
Hint
Use SADD inside the loop to add products to the set.
Practice
(1/5)
1. Why is it important to configure Redis properly before using it in a project?
easy
A. Because configuration is only needed for security reasons
B. Because Redis does not work without configuration files
C. Because configuration files store the data permanently
D. Because configuration controls how Redis behaves and performs
Solution
Step 1: Understand Redis configuration role
Redis configuration sets how Redis behaves and performs, affecting speed and features.
Step 2: Evaluate the options
Only Because configuration controls how Redis behaves and performs correctly states the importance of configuration controlling behavior and performance.
Final Answer:
Because configuration controls how Redis behaves and performs -> Option D
Quick Check:
Configuration controls behavior = A [OK]
Hint: Remember: configuration shapes Redis behavior and speed [OK]
Common Mistakes:
Thinking Redis won't work without config files
Confusing configuration with data storage
Assuming config is only for security
2. Which of the following is the correct Redis command to check the current value of a configuration setting?
easy
A. GET CONFIG <setting_name>
B. CONFIG SET <setting_name>
C. CONFIG GET <setting_name>
D. SET CONFIG <setting_name>
Solution
Step 1: Recall Redis commands for configuration
To check a setting, Redis uses CONFIG GET followed by the setting name.
Step 2: Identify correct syntax
Only CONFIG GET <setting_name> uses the correct command and order: CONFIG GET <setting_name>.
Final Answer:
CONFIG GET <setting_name> -> Option C
Quick Check:
Check config with CONFIG GET = A [OK]
Hint: Use CONFIG GET to view settings, CONFIG SET to change [OK]
Common Mistakes:
Mixing up GET and SET commands
Using wrong command order
Assuming GET CONFIG is valid
3. Given the Redis commands below, what will be the output of CONFIG GET maxmemory after running them?
CONFIG SET maxmemory 100mb
CONFIG SET maxmemory-policy allkeys-lru
CONFIG GET maxmemory
medium
A. Error: Invalid command
B. ["maxmemory", "100mb"]
C. ["maxmemory-policy", "allkeys-lru"]
D. ["maxmemory", "0"]
Solution
Step 1: Understand the commands executed
The first command sets maxmemory to 100mb, the second sets eviction policy, the third retrieves maxmemory.
Step 2: Predict output of CONFIG GET maxmemory
CONFIG GET returns the setting name and its current value, so it returns ["maxmemory", "100mb"].
Final Answer:
["maxmemory", "100mb"] -> Option B
Quick Check:
CONFIG GET maxmemory = ["maxmemory", "100mb"] [OK]
Hint: CONFIG GET returns setting name and current value as array [OK]
Common Mistakes:
Expecting policy value from maxmemory GET
Assuming default 0 value after set
Thinking command causes error
4. You run the command CONFIG SET maxmemory 500mb but Redis returns an error. What is the most likely cause?
medium
A. The maxmemory value is too large for the server's available memory
B. The command syntax is incorrect
C. Redis does not support maxmemory configuration
D. You must restart Redis before changing maxmemory
Solution
Step 1: Analyze the error cause for CONFIG SET maxmemory
Redis may reject setting maxmemory if the value exceeds available system memory.
Step 2: Check other options for validity
Syntax is correct, Redis supports maxmemory, and restart is not required for this setting.
Final Answer:
The maxmemory value is too large for the server's available memory -> Option A
Quick Check:
Memory limit too high causes error = B [OK]
Hint: Check server memory before setting maxmemory [OK]
Common Mistakes:
Assuming syntax error without checking
Thinking Redis lacks maxmemory setting
Believing restart is always needed
5. You want Redis to automatically remove least recently used keys when memory is full. Which configuration changes should you apply?
hard
A. CONFIG SET maxmemory 200mb and CONFIG SET maxmemory-policy allkeys-lru
B. CONFIG SET maxmemory 0 and CONFIG SET maxmemory-policy noeviction
C. CONFIG SET maxmemory 200mb and CONFIG SET maxmemory-policy volatile-ttl
D. CONFIG SET maxmemory 0 and CONFIG SET maxmemory-policy volatile-lru
Solution
Step 1: Identify the desired behavior
You want Redis to evict least recently used keys from all keys when memory is full.
Step 2: Match configuration to behavior
Setting maxmemory to a limit (e.g., 200mb) enables memory limit. Setting maxmemory-policy to allkeys-lru evicts least recently used keys from all keys.
Step 3: Evaluate other options
Options with maxmemory 0 disable memory limit. Policies like noeviction or volatile-ttl do not evict all keys LRU.
Final Answer:
CONFIG SET maxmemory 200mb and CONFIG SET maxmemory-policy allkeys-lru -> Option A
Quick Check:
Set memory limit + allkeys-lru policy = C [OK]
Hint: Set maxmemory limit and allkeys-lru policy for LRU eviction [OK]
Common Mistakes:
Setting maxmemory to 0 disables eviction
Choosing wrong eviction policy
Confusing volatile and allkeys policies
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.