📖 Scenario: You are managing a simple online store's inventory using Redis. You need to keep track of the stock count for a product and update it as customers buy or return items.
🎯 Goal: Build a Redis key that stores the stock count for a product and update it using INCRBY and DECRBY commands to increase or decrease the stock.
📋 What You'll Learn
Create a Redis key called product_stock with an initial stock value.
Set a variable for the number of items sold or returned.
Use DECRBY to decrease the stock when items are sold.
Use INCRBY to increase the stock when items are returned.
💡 Why This Matters
🌍 Real World
Managing inventory counts in an online store or warehouse system where stock levels change frequently.
💼 Career
Understanding how to update numeric values efficiently in Redis is important for backend developers and system administrators working with caching and real-time data.
Progress0 / 4 steps
1
Create the initial stock key
Create a Redis key called product_stock and set its value to 100 to represent the initial stock count.
Redis
Hint
Use the SET command to create a key with a number value.
2
Set the number of items sold
Create a variable called items_sold and set it to 3 to represent how many items were sold.
Redis
Hint
Assign the number 3 to the variable items_sold.
3
Decrease stock using DECRBY
Use the DECRBY command on product_stock with the value of items_sold to reduce the stock count.
Redis
Hint
Use DECRBY product_stock 3 to subtract 3 from the stock.
4
Increase stock using INCRBY
Create a variable called items_returned set to 2 and then use the INCRBY command on product_stock with items_returned to add back to the stock.
Redis
Hint
Assign 2 to items_returned and then increase the stock by 2 using INCRBY.
Practice
(1/5)
1. What does the Redis command INCRBY key 5 do?
easy
A. Sets the value of key to 5.
B. Subtracts 5 from the current integer value stored at key.
C. Deletes the key from Redis.
D. Adds 5 to the current integer value stored at key.
Solution
Step 1: Understand the INCRBY command
The INCRBY command increases the integer value stored at the specified key by the given amount.
Step 2: Apply the command to the example
Using INCRBY key 5 adds 5 to the current value of key.
Final Answer:
Adds 5 to the current integer value stored at key. -> Option D
Quick Check:
INCRBY adds number = Adds 5 [OK]
Hint: INCRBY always adds the number to the key's value [OK]
Common Mistakes:
Confusing INCRBY with DECRBY
Thinking INCRBY sets the value instead of adding
Assuming it deletes the key
2. Which of the following is the correct syntax to subtract 3 from a Redis key named counter?
easy
A. DECR counter 3
B. INCR counter -3
C. DECRBY counter 3
D. SUBTRACT counter 3
Solution
Step 1: Identify the correct command for subtraction
The Redis command to subtract a number from a key is DECRBY key number.
Step 2: Check the syntax options
DECRBY counter 3 correctly subtracts 3 from counter. INCR counter -3 is invalid (INCR takes only one argument). DECR counter 3 and SUBTRACT counter 3 are invalid.
Final Answer:
DECRBY counter 3 -> Option C
Quick Check:
DECRBY subtracts number = DECRBY counter 3 [OK]
Hint: Use DECRBY key number to subtract correctly [OK]
Common Mistakes:
Using DECR with two arguments (only one allowed)
Using INCR with an increment argument (invalid syntax)
Using non-existent commands like SUBTRACT
3. Given the following Redis commands executed in order: SET score 10 INCRBY score 7 DECRBY score 4 What is the final value of score?
medium
A. 10
B. 13
C. 3
D. 17
Solution
Step 1: Set initial value of score
SET score 10 sets the value of score to 10.
Step 2: Apply INCRBY and DECRBY commands
INCRBY score 7 adds 7, making it 17. Then DECRBY score 4 subtracts 4, resulting in 13.
Final Answer:
13 -> Option B
Quick Check:
10 + 7 - 4 = 13 [OK]
Hint: Add then subtract values stepwise to find final number [OK]
Common Mistakes:
Ignoring the order of commands
Subtracting before adding
Confusing initial value with final
4. You run the command DECRBY visits 2 but get an error. Which is the most likely cause?
medium
A. The key visits is not an integer.
B. DECRBY requires three arguments, but only two were given.
C. DECRBY cannot subtract numbers, only add.
D. The Redis server is down.
Solution
Step 1: Understand DECRBY requirements
DECRBY subtracts a number from an integer stored at the key. If the key does not exist, Redis treats it as 0. But if the key holds a non-integer value, an error occurs.
Step 2: Analyze the error cause
The error likely means visits exists but is not an integer, causing DECRBY to fail.
5. You want to track page views in Redis using a key page_views. Initially, it does not exist. You run: INCRBY page_views 10 DECRBY page_views 3 INCRBY page_views 5 What is the final value of page_views and why?
hard
A. 12, because Redis treats missing keys as 0 and updates accordingly.
B. 15, because DECRBY does not affect the value.
C. Error, because the key did not exist initially.
D. 10, because only the first INCRBY counts.
Solution
Step 1: Understand Redis behavior with missing keys
If a key does not exist, Redis treats its value as 0 for INCRBY and DECRBY commands.
Step 2: Calculate the final value step-by-step
Starting at 0, INCRBY page_views 10 sets it to 10. Then DECRBY page_views 3 subtracts 3, making 7. Finally, INCRBY page_views 5 adds 5, resulting in 12.
Final Answer:
12, because Redis treats missing keys as 0 and updates accordingly. -> Option A
Quick Check:
0 + 10 - 3 + 5 = 12 [OK]
Hint: Missing keys start at 0 for INCRBY/DECRBY [OK]
Common Mistakes:
Expecting error on missing key
Ignoring DECRBY effect
Assuming only first command counts
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.