Bird
Raised Fist0
Redisquery~10 mins

INCR and DECR for counters in Redis - Interactive Code Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to increase the value of the counter by 1.

Redis
INCR [1]
Drag options to blanks, or click blank then click option'
Acounter
Bdecr
Cset
Dget
Attempts:
3 left
💡 Hint
Common Mistakes
Using the command name as the key.
Using commands like SET or GET instead of the key name.
2fill in blank
medium

Complete the code to decrease the value of the counter by 1.

Redis
DECR [1]
Drag options to blanks, or click blank then click option'
Avalue
Bnumber
Ccounter
Dincrease
Attempts:
3 left
💡 Hint
Common Mistakes
Using a command name instead of the key.
Using a wrong key name that does not exist.
3fill in blank
hard

Fix the error in the code to correctly increase the counter.

Redis
INCR [1]
Drag options to blanks, or click blank then click option'
Aincr
Bcounter_value
Cdecr
Dcounter
Attempts:
3 left
💡 Hint
Common Mistakes
Using command names like INCR or DECR as the key.
Using a different key name than the one initialized.
4fill in blank
hard

Fill both blanks to increase and then decrease the counter.

Redis
INCR [1]
DECR [2]
Drag options to blanks, or click blank then click option'
Acounter
Bvalue
Ccount
Dnumber
Attempts:
3 left
💡 Hint
Common Mistakes
Using different key names for INCR and DECR.
Using keys that do not exist.
5fill in blank
hard

Fill all three blanks to set a counter, increase it, and then get its value.

Redis
SET [1] 10
INCR [2]
GET [3]
Drag options to blanks, or click blank then click option'
Acounter
Dvalue
Attempts:
3 left
💡 Hint
Common Mistakes
Using different key names in SET, INCR, and GET.
Using a key name that was not set.

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