Bird
Raised Fist0
Redisquery~10 mins

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

Choose your learning style10 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.

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

  1. Step 1: Understand the INCRBY command

    The INCRBY command increases the integer value stored at the specified key by the given amount.
  2. Step 2: Apply the command to the example

    Using INCRBY key 5 adds 5 to the current value of key.
  3. Final Answer:

    Adds 5 to the current integer value stored at key. -> Option D
  4. 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

  1. Step 1: Identify the correct command for subtraction

    The Redis command to subtract a number from a key is DECRBY key number.
  2. 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.
  3. Final Answer:

    DECRBY counter 3 -> Option C
  4. 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

  1. Step 1: Set initial value of score

    SET score 10 sets the value of score to 10.
  2. Step 2: Apply INCRBY and DECRBY commands

    INCRBY score 7 adds 7, making it 17. Then DECRBY score 4 subtracts 4, resulting in 13.
  3. Final Answer:

    13 -> Option B
  4. 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

  1. 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.
  2. Step 2: Analyze the error cause

    The error likely means visits exists but is not an integer, causing DECRBY to fail.
  3. Final Answer:

    The key visits is not an integer. -> Option A
  4. Quick Check:

    Non-integer key causes DECRBY error [OK]
Hint: DECRBY needs integer key value; else error occurs [OK]
Common Mistakes:
  • Thinking DECRBY needs 3 arguments
  • Believing DECRBY only adds
  • Assuming server is down without checking
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

  1. 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.
  2. 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.
  3. Final Answer:

    12, because Redis treats missing keys as 0 and updates accordingly. -> Option A
  4. 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 unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard