0
0
Redisquery~10 mins

INCRBY and DECRBY in Redis - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - INCRBY and DECRBY
Start with a key holding a number
Receive INCRBY or DECRBY command
Add or subtract the given amount
Update the key with new value
Return the new value
End
The flow shows how Redis takes a key with a number, changes it by adding or subtracting a specified amount, updates the key, and returns the new value.
Execution Sample
Redis
SET counter 10
INCRBY counter 5
DECRBY counter 3
This sequence sets a counter to 10, increases it by 5, then decreases it by 3.
Execution Table
StepCommandKey Value BeforeOperationKey Value AfterReturned Value
1SET counter 10N/ASet to 1010OK
2INCRBY counter 51010 + 51515
3DECRBY counter 31515 - 31212
4END12No more commands12N/A
💡 No more commands to execute; final value of 'counter' is 12.
Variable Tracker
VariableStartAfter 1After 2Final
counterN/A101512
Key Moments - 2 Insights
Why does INCRBY return the new value instead of the old one?
INCRBY returns the updated value after adding the increment, as shown in step 2 of the execution_table where the returned value is 15, reflecting the new state.
What happens if the key does not exist before INCRBY or DECRBY?
If the key does not exist, Redis treats it as 0 before applying INCRBY or DECRBY, so the operation still works and sets the key to the increment or decrement value.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of 'counter' after step 2?
A15
B10
C12
D5
💡 Hint
Check the 'Key Value After' column in row for step 2.
At which step does the 'counter' value become 12?
AStep 1
BStep 2
CStep 3
DStep 4
💡 Hint
Look at the 'Key Value After' column and find when it changes to 12.
If we changed INCRBY counter 5 to INCRBY counter 2, what would be the new value after step 2?
A17
B12
C10
D7
💡 Hint
Add 2 to the initial value 10 as shown in the variable_tracker.
Concept Snapshot
INCRBY key increment
DECRBY key decrement

Both commands change the integer value stored at key by adding or subtracting the given number.
If key does not exist, it is treated as 0.
They return the new value after the operation.
Full Transcript
This visual execution shows how Redis commands INCRBY and DECRBY work. We start with a key 'counter' set to 10. Then INCRBY adds 5, making it 15, and DECRBY subtracts 3, making it 12. Each step updates the key and returns the new value. If the key did not exist, Redis would treat it as zero before changing it. This helps understand how these commands modify numeric values stored in Redis keys.