Using ZADD to Add Scored Members in Redis Sorted Sets
📖 Scenario: You are managing a leaderboard for a game. Players earn points, and you want to store their scores in a Redis sorted set so you can quickly find the top players.
🎯 Goal: Build a Redis sorted set called game_leaderboard and add players with their scores using the ZADD command.
📋 What You'll Learn
Create a sorted set key named game_leaderboard
Add three players with exact scores using ZADD: "Alice" with score 1500, "Bob" with score 1200, and "Charlie" with score 1800
Add a new player "Diana" with score 1600 using ZADD
Add two more players "Eve" with score 1100 and "Frank" with score 1700 using a single ZADD command
💡 Why This Matters
🌍 Real World
Leaderboards in games, ranking systems, and real-time scoring applications often use Redis sorted sets to store and retrieve scores efficiently.
💼 Career
Understanding how to use Redis sorted sets and ZADD is valuable for backend developers working on performance-critical applications involving rankings and scoring.
Progress0 / 4 steps
1
Create the initial sorted set with three players
Use the ZADD command to create a sorted set called game_leaderboard and add these exact players with their scores: "Alice" 1500, "Bob" 1200, and "Charlie" 1800.
Redis
Hint
Remember, the syntax is ZADD key score member [score member ...].
2
Add a new player Diana with her score
Use the ZADD command to add the player "Diana" with the score 1600 to the existing game_leaderboard sorted set.
Redis
Hint
You can add one member at a time with ZADD.
3
Add two more players Eve and Frank with one command
Use a single ZADD command to add "Eve" with score 1100 and "Frank" with score 1700 to the game_leaderboard sorted set.
Redis
Hint
You can add multiple members in one ZADD command by listing score-member pairs.
4
Complete the leaderboard setup
Ensure the full set of ZADD commands is present to create game_leaderboard with all six players and their exact scores.
Redis
Hint
Check that all players and scores are added exactly as required.
Practice
(1/5)
1. What does the Redis command ZADD do?
easy
A. Adds members with scores to a sorted set
B. Deletes members from a sorted set
C. Retrieves all members from a sorted set
D. Creates a new key with a string value
Solution
Step 1: Understand the purpose of ZADD
The ZADD command is used to add members with associated scores to a sorted set in Redis.
Step 2: Compare with other commands
Deleting members is done by ZREM, retrieving members by ZRANGE, and creating string keys by SET, so these are not correct.
Final Answer:
Adds members with scores to a sorted set -> Option A
Quick Check:
ZADD adds scored members [OK]
Hint: ZADD always adds scored members to sorted sets [OK]
Common Mistakes:
Confusing ZADD with ZREM or ZRANGE
Thinking ZADD creates string keys
Assuming ZADD deletes members
2. Which of the following is the correct syntax to add a member 'player1' with score 100 to a sorted set 'game_scores' using ZADD?
easy
A. ZADD game_scores 100 player1
B. ZADD 100 game_scores player1
C. ZADD game_scores player1 100
D. ZADD player1 100 game_scores
Solution
Step 1: Recall ZADD syntax
The correct syntax is: ZADD key score member. So the key comes first, then the score, then the member.
Step 2: Match the syntax to options
ZADD game_scores 100 player1 matches this order: ZADD game_scores 100 player1. Others have wrong order.
Final Answer:
ZADD game_scores 100 player1 -> Option A
Quick Check:
ZADD key score member order [OK]
Hint: Remember: ZADD key score member [OK]
Common Mistakes:
Swapping score and member positions
Putting member before key
Using wrong argument order
3. What will be the result of the following commands?
ZADD leaderboard 50 Alice 70 Bob 60 Charlie
ZRANGE leaderboard 0 -1 WITHSCORES
medium
A. ["Bob", "70", "Charlie", "60", "Alice", "50"]
B. ["Alice", "50", "Charlie", "60", "Bob", "70"]
C. ["Alice", "50", "Bob", "70", "Charlie", "60"]
D. ["Charlie", "60", "Alice", "50", "Bob", "70"]
Solution
Step 1: Understand ZADD and ZRANGE behavior
ZADD adds members with scores. ZRANGE with 0 -1 returns all members sorted by score ascending, WITHSCORES returns scores alongside.
Step 2: Sort members by score ascending
Scores: Alice=50, Charlie=60, Bob=70. Sorted ascending: Alice, Charlie, Bob.
Final Answer:
["Alice", "50", "Charlie", "60", "Bob", "70"] -> Option B
Quick Check:
ZRANGE sorts ascending by score [OK]
Hint: ZRANGE returns members sorted by score ascending [OK]
Common Mistakes:
Assuming ZRANGE sorts by insertion order
Mixing up ascending and descending order
Ignoring WITHSCORES option
4. Identify the error in this Redis command: ZADD myset player1 100
medium
A. ZADD cannot add members with numeric scores
B. The key name 'myset' is invalid
C. The score and member are reversed; score must come before member
D. The command is correct and will work as expected
Solution
Step 1: Check ZADD argument order
ZADD syntax requires the key first, then the score, then the member. Here, 'player1' is before '100', which is reversed.
Step 2: Validate other options
Key name 'myset' is valid, numeric scores are allowed, so options B and C are incorrect. The command is not correct, so D is wrong.
Final Answer:
The score and member are reversed; score must come before member -> Option C
Quick Check:
ZADD requires score before member [OK]
Hint: Score always comes before member in ZADD [OK]
Common Mistakes:
Swapping score and member positions
Thinking key name is invalid
Believing numeric scores are disallowed
5. You want to add multiple players with their scores to a sorted set 'ranking' in one command. Which command correctly adds 'Anna' with 85, 'Ben' with 90, and 'Cara' with 80?