INCRBY and DECRBY in Redis - Time & Space Complexity
When working with Redis commands like INCRBY and DECRBY, it's important to understand how the time it takes to run these commands changes as the input changes.
We want to know how the cost grows when we increase the numbers or the number of commands.
Analyze the time complexity of the following Redis commands.
INCRBY counter 5
DECRBY counter 3
INCRBY counter 10
DECRBY counter 2
These commands increase or decrease the value stored at key "counter" by a given number.
Look for repeated actions in these commands.
- Primary operation: Incrementing or decrementing the stored number by a fixed amount.
- How many times: Each command runs once per call, no loops inside the command itself.
The time to run each command stays about the same no matter how big the number is.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 1 operation per command |
| 100 | 1 operation per command |
| 1000 | 1 operation per command |
Pattern observation: The time does not increase with the size of the number being added or subtracted.
Time Complexity: O(1)
This means each INCRBY or DECRBY command takes the same amount of time no matter how big the number is.
[X] Wrong: "Increasing by a bigger number takes more time than a smaller number."
[OK] Correct: Redis handles these commands in constant time, so the size of the number does not affect how long the command takes.
Understanding that simple Redis commands like INCRBY and DECRBY run in constant time helps you explain how Redis can handle many updates quickly, which is a useful skill in real-world applications.
"What if we used INCRBY on a key that holds a very large string instead of a number? How would the time complexity change?"
Practice
INCRBY key 5 do?Solution
Step 1: Understand the INCRBY command
TheINCRBYcommand increases the integer value stored at the specified key by the given amount.Step 2: Apply the command to the example
UsingINCRBY key 5adds 5 to the current value ofkey.Final Answer:
Adds 5 to the current integer value stored at key. -> Option DQuick Check:
INCRBY adds number = Adds 5 [OK]
- Confusing INCRBY with DECRBY
- Thinking INCRBY sets the value instead of adding
- Assuming it deletes the key
counter?Solution
Step 1: Identify the correct command for subtraction
The Redis command to subtract a number from a key isDECRBY key number.Step 2: Check the syntax options
DECRBY counter 3correctly subtracts 3 fromcounter.INCR counter -3is invalid (INCR takes only one argument).DECR counter 3andSUBTRACT counter 3are invalid.Final Answer:
DECRBY counter 3 -> Option CQuick Check:
DECRBY subtracts number = DECRBY counter 3 [OK]
- Using DECR with two arguments (only one allowed)
- Using INCR with an increment argument (invalid syntax)
- Using non-existent commands like SUBTRACT
SET score 10INCRBY score 7DECRBY score 4What is the final value of
score?Solution
Step 1: Set initial value of score
SET score 10sets the value ofscoreto 10.Step 2: Apply INCRBY and DECRBY commands
INCRBY score 7adds 7, making it 17. ThenDECRBY score 4subtracts 4, resulting in 13.Final Answer:
13 -> Option BQuick Check:
10 + 7 - 4 = 13 [OK]
- Ignoring the order of commands
- Subtracting before adding
- Confusing initial value with final
DECRBY visits 2 but get an error. Which is the most likely cause?Solution
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.Step 2: Analyze the error cause
The error likely meansvisitsexists but is not an integer, causing DECRBY to fail.Final Answer:
The key visits is not an integer. -> Option AQuick Check:
Non-integer key causes DECRBY error [OK]
- Thinking DECRBY needs 3 arguments
- Believing DECRBY only adds
- Assuming server is down without checking
page_views. Initially, it does not exist. You run:INCRBY page_views 10DECRBY page_views 3INCRBY page_views 5What is the final value of
page_views and why?Solution
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.Step 2: Calculate the final value step-by-step
Starting at 0,INCRBY page_views 10sets it to 10. ThenDECRBY page_views 3subtracts 3, making 7. Finally,INCRBY page_views 5adds 5, resulting in 12.Final Answer:
12, because Redis treats missing keys as 0 and updates accordingly. -> Option AQuick Check:
0 + 10 - 3 + 5 = 12 [OK]
- Expecting error on missing key
- Ignoring DECRBY effect
- Assuming only first command counts
Ready to prove it?
Pass the test and this pattern moves unknown
