Bird
Raised Fist0
Redisquery~10 mins

INCR and DECR for counters in Redis - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - INCR and DECR for counters
Start with a key holding a number
↓
Receive INCR or DECR command
↓
Fetch current value of key
↓
Add 1 if INCR, subtract 1 if DECR
↓
Store new value back to key
↓
Return new value as result
↓
End
The flow shows how Redis increments or decrements a numeric value stored at a key, updating and returning the new value.
Execution Sample
Redis
SET counter 5
INCR counter
DECR counter
INCR counter
This sequence sets a counter to 5, increments it, decrements it, then increments it again.
Execution Table
StepCommandCurrent ValueOperationNew ValueOutput
1SET counter 5N/ASet to 55OK
2INCR counter5+166
3DECR counter6-155
4INCR counter5+166
5END6No more commands6Execution stops
💡 No more commands to execute, final counter value is 6
Variable Tracker
VariableStartAfter 1After 2After 3Final
counterN/A5656
Key Moments - 2 Insights
Why does the counter value change after each INCR or DECR command?
Each INCR adds 1 and each DECR subtracts 1 from the current value, as shown in steps 2, 3, and 4 of the execution_table.
What happens if the key does not exist before INCR or DECR?
Redis treats a missing key as 0, so INCR sets it to 1 and DECR sets it to -1, but this example starts with SET so the key exists.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the output after the first INCR command?
A6
B5
C7
DError
💡 Hint
Check row 2 in the execution_table under Output column
At which step does the counter value return to 5?
AStep 1
BStep 3
CStep 2
DStep 4
💡 Hint
Look at the New Value column in execution_table row 3
If we remove the initial SET command, what would INCR counter return on the first call?
A0
BError
C1
DN/A
💡 Hint
Redis treats missing keys as 0 before incrementing, so INCR returns 1
Concept Snapshot
INCR and DECR commands in Redis:
- INCR key: increases the integer value by 1
- DECR key: decreases the integer value by 1
- If key does not exist, treated as 0 before operation
- Returns the new value after operation
- Useful for counters and simple numeric tracking
Full Transcript
This visual execution trace shows how Redis commands INCR and DECR work on a counter key. Starting with setting the counter to 5, each INCR adds 1 and each DECR subtracts 1. The execution table tracks each step's command, current value, operation, new value, and output. The variable tracker shows how the counter changes over time. Key moments clarify why values change and what happens if the key is missing. The quiz tests understanding of outputs and behavior. This helps beginners see exactly how Redis updates counters step-by-step.

Practice

(1/5)
1. What does the Redis command INCR mycounter do if mycounter does not exist yet?
easy
A. It creates mycounter with value 1.
B. It returns an error because the key does not exist.
C. It creates mycounter with value 0.
D. It sets mycounter to -1.

Solution

  1. Step 1: Understand INCR behavior on missing keys

    When INCR is called on a key that does not exist, Redis treats it as 0 before incrementing.
  2. Step 2: Apply increment to initial value

    Incrementing 0 by 1 results in 1, so Redis sets mycounter to 1.
  3. Final Answer:

    It creates mycounter with value 1. -> Option A
  4. Quick Check:

    INCR on missing key = 1 [OK]
Hint: INCR on missing key starts at 1 automatically [OK]
Common Mistakes:
  • Thinking INCR returns error if key missing
  • Assuming key starts at 0 after INCR
  • Confusing INCR with setting key manually
2. Which of the following is the correct syntax to decrease a Redis key named pageviews by 1?
easy
A. DECREMENT pageviews
B. DECR pageviews
C. INCR pageviews -1
D. DECR pageviews 1

Solution

  1. Step 1: Recall Redis command for decrement by 1

    The correct Redis command to decrease a key by 1 is DECR key.
  2. Step 2: Check options for correct syntax

    DECR pageviews uses DECR pageviews, which is valid. DECREMENT pageviews is invalid command, C is incorrect syntax, and D is invalid because DECR does not accept an amount argument.
  3. Final Answer:

    DECR pageviews -> Option B
  4. Quick Check:

    Use DECR key to subtract 1 [OK]
Hint: Use DECR key to subtract 1 exactly [OK]
Common Mistakes:
  • Using DECREMENT instead of DECR
  • Trying INCR with negative number
  • Confusing DECR and DECRBY syntax
3. Given the following Redis commands executed in order:
INCR visits
INCR visits
DECR visits
INCR visits

What is the final value of the key visits?
medium
A. 4
B. 3
C. 1
D. 2

Solution

  1. Step 1: Track each command's effect on visits

    Start: visits does not exist, treated as 0.
    INCR visits -> 1
    INCR visits -> 2
    DECR visits -> 1
    INCR visits -> 2
  2. Step 2: Recalculate carefully

    Re-check: Actually, after DECR visits from 2, visits = 1.
    Then INCR visits from 1, visits = 2.
  3. Final Answer:

    2 -> Option D
  4. Quick Check:

    INCR twice, DECR once, INCR once = 2 [OK]
Hint: Count increments and decrements stepwise [OK]
Common Mistakes:
  • Adding all INCRs without subtracting DECR
  • Assuming visits starts at 1
  • Mixing order of commands
4. You run the command DECR mycount on a Redis key mycount that currently holds the string value "hello". What will happen?
medium
A. Redis will convert "hello" to 0 and then decrement to -1.
B. The value will become "hello-1".
C. Redis will return an error because the value is not an integer.
D. The value will remain "hello" unchanged.

Solution

  1. Step 1: Understand DECR command requirements

    DECR expects the key's value to be an integer or missing (treated as 0). A non-integer string causes an error.
  2. Step 2: Apply to given value

    Since mycount holds "hello", which is not an integer, Redis returns an error.
  3. Final Answer:

    Redis will return an error because the value is not an integer. -> Option C
  4. Quick Check:

    DECR on non-integer string = error [OK]
Hint: DECR only works on integer values [OK]
Common Mistakes:
  • Assuming Redis converts strings to 0 automatically
  • Expecting string concatenation
  • Thinking DECR silently fails
5. You want to track the number of active users on a website using Redis. Which sequence of commands correctly increases the count when a user logs in and decreases it when a user logs out, ensuring the count never goes below zero?
hard
A. Use INCR active_users on login and before DECR active_users on logout, check if value > 0.
B. Use INCRBY active_users 1 on login and DECRBY active_users 1 on logout without checks.
C. Use INCR active_users on login and DECR active_users on logout without checks.
D. Use INCR active_users on login and DEL active_users on logout.

Solution

  1. Step 1: Understand counting active users

    Increment on login is simple with INCR. Decrement on logout must avoid negative counts.
  2. Step 2: Prevent negative counts on logout

    Before calling DECR, check if active_users > 0 to avoid going below zero.
  3. Final Answer:

    Use INCR on login and check value > 0 before DECR on logout. -> Option A
  4. Quick Check:

    Check before DECR to avoid negatives [OK]
Hint: Check count > 0 before DECR to prevent negatives [OK]
Common Mistakes:
  • Not checking before DECR causes negative counts
  • Deleting key on logout resets count incorrectly
  • Assuming DECRBY and DECR behave differently here
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