INCR and DECR for counters in Redis - Time & Space 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.
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.
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.
Each command takes about the same time no matter the counter's value.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 operations |
| 100 | 100 operations |
| 1000 | 1000 operations |
Pattern observation: The total time grows directly with the number of commands run.
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.
[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.
Knowing that simple counter updates are fast helps you explain how Redis handles many quick changes efficiently, a useful skill in real projects.
"What if we used INCRBY to add a large number instead of 1? How would the time complexity change?"
Practice
INCR mycounter do if mycounter does not exist yet?Solution
Step 1: Understand INCR behavior on missing keys
WhenINCRis called on a key that does not exist, Redis treats it as 0 before incrementing.Step 2: Apply increment to initial value
Incrementing 0 by 1 results in 1, so Redis setsmycounterto 1.Final Answer:
It createsmycounterwith value 1. -> Option AQuick Check:
INCR on missing key = 1 [OK]
- Thinking INCR returns error if key missing
- Assuming key starts at 0 after INCR
- Confusing INCR with setting key manually
pageviews by 1?Solution
Step 1: Recall Redis command for decrement by 1
The correct Redis command to decrease a key by 1 isDECR key.Step 2: Check options for correct syntax
DECR pageviews usesDECR 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.Final Answer:
DECR pageviews -> Option BQuick Check:
Use DECR key to subtract 1 [OK]
- Using DECREMENT instead of DECR
- Trying INCR with negative number
- Confusing DECR and DECRBY syntax
INCR visits
INCR visits
DECR visits
INCR visits
What is the final value of the key
visits?Solution
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 -> 2Step 2: Recalculate carefully
Re-check: Actually, after DECR visits from 2, visits = 1.
Then INCR visits from 1, visits = 2.Final Answer:
2 -> Option DQuick Check:
INCR twice, DECR once, INCR once = 2 [OK]
- Adding all INCRs without subtracting DECR
- Assuming visits starts at 1
- Mixing order of commands
DECR mycount on a Redis key mycount that currently holds the string value "hello". What will happen?Solution
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.Step 2: Apply to given value
Sincemycountholds "hello", which is not an integer, Redis returns an error.Final Answer:
Redis will return an error because the value is not an integer. -> Option CQuick Check:
DECR on non-integer string = error [OK]
- Assuming Redis converts strings to 0 automatically
- Expecting string concatenation
- Thinking DECR silently fails
Solution
Step 1: Understand counting active users
Increment on login is simple withINCR. Decrement on logout must avoid negative counts.Step 2: Prevent negative counts on logout
Before callingDECR, check ifactive_users> 0 to avoid going below zero.Final Answer:
Use INCR on login and check value > 0 before DECR on logout. -> Option AQuick Check:
Check before DECR to avoid negatives [OK]
- Not checking before DECR causes negative counts
- Deleting key on logout resets count incorrectly
- Assuming DECRBY and DECR behave differently here
Ready to prove it?
Pass the test and this pattern moves unknown
