What if you could update scores instantly without any chance of mistakes?
Why INCRBY and DECRBY in Redis? - Purpose & Use Cases
Imagine you have a list of scores for a game stored in a notebook. Every time a player scores, you have to find their score and add the points manually. If the player loses points, you have to subtract them by hand. This gets confusing and slow as the game goes on and the list grows.
Manually updating scores is slow and easy to mess up. You might write the wrong number, forget to update a score, or lose track of who has what points. It's hard to keep everything accurate and up-to-date, especially when many players are involved.
INCRBY and DECRBY commands let you add or subtract numbers directly in the database without reading or rewriting the whole value. This means the database handles the math safely and quickly, so you never lose track or make mistakes.
score = get_score(player) new_score = score + points set_score(player, new_score)
INCRBY player points DECRBY player points
It makes updating numbers fast, safe, and easy, even when many users change data at the same time.
In a live online game, players earn or lose points constantly. Using INCRBY and DECRBY keeps scores accurate instantly without slowing down the game.
Manual updates are slow and error-prone.
INCRBY and DECRBY update numbers directly in the database.
This keeps data accurate and operations fast.
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
