0
0
Redisquery~10 mins

ZINCRBY for score updates in Redis - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - ZINCRBY for score updates
Start with Sorted Set
Call ZINCRBY with key, increment, member
Check if member exists in Sorted Set
Yes No
Increase score
Update Sorted Set with new score
Return new score of member
End
ZINCRBY updates the score of a member in a sorted set by adding the increment. If the member does not exist, it is added with the increment as its score.
Execution Sample
Redis
ZINCRBY leaderboard 10 "player1"
ZINCRBY leaderboard 5 "player2"
ZINCRBY leaderboard -3 "player1"
This code increases player1's score by 10, adds player2 with score 5, then decreases player1's score by 3.
Execution Table
StepCommandMember Exists?ActionNew ScoreResult Returned
1ZINCRBY leaderboard 10 "player1"NoAdd member 'player1' with score 101010
2ZINCRBY leaderboard 5 "player2"NoAdd member 'player2' with score 555
3ZINCRBY leaderboard -3 "player1"YesDecrease 'player1' score by 377
4End----
💡 All commands executed; final scores updated accordingly.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3Final
leaderboard{}{"player1":10}{"player1":10, "player2":5}{"player1":7, "player2":5}{"player1":7, "player2":5}
Key Moments - 2 Insights
What happens if the member does not exist in the sorted set when ZINCRBY is called?
If the member does not exist, ZINCRBY adds it to the sorted set with the increment as its initial score, as shown in steps 1 and 2 of the execution_table.
Can ZINCRBY decrease a member's score?
Yes, ZINCRBY can decrease a score by using a negative increment, as shown in step 3 where 'player1' score decreases from 10 to 7.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the score of 'player1' after step 3?
A7
B10
C5
D3
💡 Hint
Check the 'New Score' column for step 3 in the execution_table.
At which step is 'player2' added to the leaderboard?
AStep 1
BStep 2
CStep 3
DStep 4
💡 Hint
Look at the 'Member Exists?' and 'Action' columns in the execution_table.
If we call ZINCRBY leaderboard 4 "player3" after step 3, what will happen?
ADecrease 'player2' score by 4
BIncrease 'player1' score by 4
CAdd 'player3' with score 4
DNo change because 'player3' does not exist
💡 Hint
Refer to the concept_flow and how new members are handled when they don't exist.
Concept Snapshot
ZINCRBY key increment member
- Adds increment to member's score in sorted set
- If member missing, adds with increment as score
- Supports positive and negative increments
- Returns updated score after operation
Full Transcript
ZINCRBY is a Redis command used to update scores in a sorted set. When you call ZINCRBY with a key, an increment, and a member, it checks if the member exists. If it does, it adds the increment to the current score. If not, it adds the member with the increment as its initial score. This works for both increasing and decreasing scores. The command returns the new score after updating. For example, adding 10 to player1 when they don't exist sets their score to 10. Adding 5 to player2 adds them with score 5. Decreasing player1 by 3 changes their score from 10 to 7.