Bird
Raised Fist0
Redisquery~10 mins

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

Choose your learning style10 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.

Practice

(1/5)
1.

What does the Redis command ZINCRBY do?

easy
A. It creates a new sorted set without any members.
B. It increases the score of a member in a sorted set by a given amount.
C. It returns the rank of a member in a sorted set.
D. It deletes a member from a sorted set.

Solution

  1. 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.
  2. Step 2: Compare with other options

    Other options describe different commands: deletion, rank retrieval, or creation, which are not what ZINCRBY does.
  3. Final Answer:

    It increases the score of a member in a sorted set by a given amount. -> Option B
  4. Quick Check:

    ZINCRBY increments score of member [OK]
Hint: ZINCRBY always adds to a member's score in sorted sets [OK]
Common Mistakes:
  • Confusing ZINCRBY with ZREM (removal)
  • Thinking it returns rank instead of updating score
  • Assuming it creates a new sorted set without members
2.

Which of the following is the correct syntax to increase the score of member user1 by 5 in sorted set leaderboard?

?
easy
A. ZINCRBY leaderboard 5 user1
B. ZINCRBY leaderboard user1 5
C. ZINCRBY 5 leaderboard user1
D. ZINCRBY user1 leaderboard 5

Solution

  1. Step 1: Recall ZINCRBY syntax

    The correct syntax is: ZINCRBY key increment member.
  2. Step 2: Match syntax with options

    ZINCRBY leaderboard 5 user1 matches the correct order: ZINCRBY leaderboard 5 user1.
  3. Final Answer:

    ZINCRBY leaderboard 5 user1 -> Option A
  4. Quick Check:

    Command order = ZINCRBY key increment member [OK]
Hint: Remember: ZINCRBY key increment member [OK]
Common Mistakes:
  • Swapping member and increment positions
  • Putting member before key
  • Using increment before key
3.

Given the following Redis commands executed in order:

ZINCRBY scores 10 alice
ZINCRBY scores 5 bob
ZINCRBY scores -3 alice
ZINCRBY scores 2 charlie

What is the score of alice after these commands?

medium
A. 7
B. 10
C. 12
D. -3

Solution

  1. Step 1: Calculate alice's score changes

    First increment: +10, then decrement: -3, total = 10 - 3 = 7.
  2. Step 2: Confirm final score

    Other members' scores do not affect alice's score.
  3. Final Answer:

    7 -> Option A
  4. Quick Check:

    10 - 3 = 7 [OK]
Hint: Add increments for the same member to get final score [OK]
Common Mistakes:
  • Ignoring negative increments
  • Adding scores of other members
  • Using only the first increment
4.

What is wrong with this Redis command to increase user2's score by 4 in sorted set game_scores?

ZINCRBY game_scores user2 4
medium
A. The increment value must be negative.
B. The key name is missing.
C. The command should be ZADD instead of ZINCRBY.
D. The increment and member arguments are swapped.

Solution

  1. Step 1: Check argument order for ZINCRBY

    ZINCRBY syntax is: ZINCRBY key increment member. Here, 'user2' and '4' are swapped.
  2. Step 2: Identify the error

    The command should be: ZINCRBY game_scores 4 user2.
  3. Final Answer:

    The increment and member arguments are swapped. -> Option D
  4. Quick Check:

    Order must be key, increment, member [OK]
Hint: Increment always comes before member in ZINCRBY [OK]
Common Mistakes:
  • Swapping increment and member
  • Using ZADD instead of ZINCRBY for increment
  • Thinking increment must be negative
5.

You have a sorted set ranking with members and scores:

"alice" 15
"bob" 20
"carol" 10

You 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?

hard
A. ZADD ranking 22 carol ZADD ranking 15 bob
B. ZINCRBY ranking carol 12 ZINCRBY ranking bob -5
C. ZINCRBY ranking 12 carol ZINCRBY ranking -5 bob
D. ZINCRBY ranking 5 carol ZINCRBY ranking -12 bob

Solution

  1. Step 1: Apply correct ZINCRBY syntax for increments

    To increase carol's score by 12: ZINCRBY ranking 12 carol.
  2. Step 2: Apply correct ZINCRBY syntax for decrement

    To decrease bob's score by 5: ZINCRBY ranking -5 bob.
  3. 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.
  4. Final Answer:

    ZINCRBY ranking 12 carol ZINCRBY ranking -5 bob -> Option C
  5. Quick Check:

    Use ZINCRBY with positive and negative increments [OK]
Hint: Use positive or negative increments with ZINCRBY to adjust scores [OK]
Common Mistakes:
  • Swapping member and increment arguments
  • Using ZADD which overwrites scores instead of incrementing
  • Using wrong increment values
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard