ZINCRBY helps you add or subtract points from a member's score in a sorted set easily.
ZINCRBY for score updates in Redis
ZINCRBY key increment member
key is the name of the sorted set.increment is the number to add (can be negative to subtract).member is the item whose score you want to update.ZINCRBY leaderboard 10 player1ZINCRBY leaderboard -5 player2ZINCRBY scores 1.5 user123This example first adds player1 with 50 points. Then it adds 20 more points to player1. It subtracts 10 points from player2 (who is not yet in the set, so player2 is added with score -10). Finally, it lists all players with their scores.
ZADD leaderboard 50 player1 ZINCRBY leaderboard 20 player1 ZINCRBY leaderboard -10 player2 ZRANGE leaderboard 0 -1 WITHSCORES
If the member does not exist, ZINCRBY adds it with the increment as its score.
You can use negative increments to reduce scores.
Scores can be floating point numbers.
ZINCRBY updates a member's score by adding a number.
It creates the member if it does not exist.
Use it to keep scores or rankings updated easily.
Practice
What does the Redis command ZINCRBY do?
Solution
Step 1: Understand the purpose of ZINCRBY
ZINCRBY is used to increase the score of a member in a sorted set by a specified increment.Step 2: Compare with other options
Other options describe different commands: deletion, rank retrieval, or creation, which are not what ZINCRBY does.Final Answer:
It increases the score of a member in a sorted set by a given amount. -> Option BQuick Check:
ZINCRBY increments score of member [OK]
- Confusing ZINCRBY with ZREM (removal)
- Thinking it returns rank instead of updating score
- Assuming it creates a new sorted set without members
Which of the following is the correct syntax to increase the score of member user1 by 5 in sorted set leaderboard?
?Solution
Step 1: Recall ZINCRBY syntax
The correct syntax is: ZINCRBY key increment member.Step 2: Match syntax with options
ZINCRBY leaderboard 5 user1 matches the correct order: ZINCRBY leaderboard 5 user1.Final Answer:
ZINCRBY leaderboard 5 user1 -> Option AQuick Check:
Command order = ZINCRBY key increment member [OK]
- Swapping member and increment positions
- Putting member before key
- Using increment before key
Given the following Redis commands executed in order:
ZINCRBY scores 10 alice
ZINCRBY scores 5 bob
ZINCRBY scores -3 alice
ZINCRBY scores 2 charlieWhat is the score of alice after these commands?
Solution
Step 1: Calculate alice's score changes
First increment: +10, then decrement: -3, total = 10 - 3 = 7.Step 2: Confirm final score
Other members' scores do not affect alice's score.Final Answer:
7 -> Option AQuick Check:
10 - 3 = 7 [OK]
- Ignoring negative increments
- Adding scores of other members
- Using only the first increment
What is wrong with this Redis command to increase user2's score by 4 in sorted set game_scores?
ZINCRBY game_scores user2 4Solution
Step 1: Check argument order for ZINCRBY
ZINCRBY syntax is: ZINCRBY key increment member. Here, 'user2' and '4' are swapped.Step 2: Identify the error
The command should be: ZINCRBY game_scores 4 user2.Final Answer:
The increment and member arguments are swapped. -> Option DQuick Check:
Order must be key, increment, member [OK]
- Swapping increment and member
- Using ZADD instead of ZINCRBY for increment
- Thinking increment must be negative
You have a sorted set ranking with members and scores:
"alice" 15
"bob" 20
"carol" 10You want to increase carol's score by 12 and decrease bob's score by 5 using Redis commands. Which sequence of commands correctly updates the scores?
Solution
Step 1: Apply correct ZINCRBY syntax for increments
To increase carol's score by 12: ZINCRBY ranking 12 carol.Step 2: Apply correct ZINCRBY syntax for decrement
To decrease bob's score by 5: ZINCRBY ranking -5 bob.Step 3: Verify other options
ZINCRBY ranking carol 12 ZINCRBY ranking bob -5 swaps increment and member, ZADD ranking 22 carol ZADD ranking 15 bob uses ZADD which replaces scores, ZINCRBY ranking 5 carol ZINCRBY ranking -12 bob uses wrong increments.Final Answer:
ZINCRBY ranking 12 carol ZINCRBY ranking -5 bob -> Option CQuick Check:
Use ZINCRBY with positive and negative increments [OK]
- Swapping member and increment arguments
- Using ZADD which overwrites scores instead of incrementing
- Using wrong increment values
Ready to prove it?
Pass the test and this pattern moves unknown
