Bird
Raised Fist0
Redisquery~5 mins

INCR and DECR for counters in Redis - Time & Space Complexity

Choose your learning style10 modes available
Time Complexity: INCR and DECR for counters
O(1)
Understanding Time Complexity

When using Redis commands like INCR and DECR, it's important to know how the time to run these commands changes as we use them more.

We want to understand how the cost grows when we increase or decrease counters many times.

Scenario Under Consideration

Analyze the time complexity of the following Redis commands.


INCR counter
DECR counter
INCR counter
DECR counter
    

This code increases and decreases a counter value stored in Redis multiple times.

Identify Repeating Operations

Look for repeated actions that take time.

  • Primary operation: Each INCR or DECR command updates the counter once.
  • How many times: Each command runs once per call, so if you run n commands, you have n operations.
How Execution Grows With Input

Each command takes about the same time no matter the counter's value.

Input Size (n)Approx. Operations
1010 operations
100100 operations
10001000 operations

Pattern observation: The total time grows directly with the number of commands run.

Final Time Complexity

Time Complexity: O(1)

This means each INCR or DECR command takes the same small amount of time, no matter how big the counter is.

Common Mistake

[X] Wrong: "INCR or DECR commands take longer as the counter value gets bigger."

[OK] Correct: Redis stores numbers efficiently, so changing a number is always quick and does not depend on its size.

Interview Connect

Knowing that simple counter updates are fast helps you explain how Redis handles many quick changes efficiently, a useful skill in real projects.

Self-Check

"What if we used INCRBY to add a large number instead of 1? How would the time complexity change?"

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