Bird
Raised Fist0
Redisquery~10 mins

ZADD for adding scored members in Redis - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - ZADD for adding scored members
Start ZADD command
↓
Parse key and score-member pairs
↓
Check if sorted set exists
↓
Add/update members with scores
↓
Return number of new elements added
↓
End
ZADD adds members with scores to a sorted set, creating it if missing, and returns how many new members were added.
Execution Sample
Redis
ZADD myset 10 "apple" 20 "banana" 15 "cherry"
Adds three members with scores to the sorted set 'myset'.
Execution Table
StepActionKey State BeforeMembers Added/UpdatedKey State AfterReturn Value
1Start ZADD commandmyset does not existNonemyset created emptyNone
2Add member 'apple' with score 10emptyapple added{"apple":10}1
3Add member 'banana' with score 20{"apple":10}banana added{"apple":10, "banana":20}2
4Add member 'cherry' with score 15{"apple":10, "banana":20}cherry added{"apple":10, "banana":20, "cherry":15}3
5Return total new members added{"apple":10, "banana":20, "cherry":15}None{"apple":10, "banana":20, "cherry":15}3
💡 All members processed, ZADD returns count of new members added (3).
Variable Tracker
VariableStartAfter 1After 2After 3Final
mysetdoes not exist{"apple":10}{"apple":10, "banana":20}{"apple":10, "banana":20, "cherry":15}{"apple":10, "banana":20, "cherry":15}
new_members_count01233
Key Moments - 2 Insights
Why does ZADD create the sorted set if it does not exist?
Because the execution_table row 1 shows the key 'myset' does not exist initially, ZADD creates it to add members.
What does the return value represent in ZADD?
As seen in execution_table rows 2-5, the return value counts how many new members were added, not updated.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the state of 'myset' after step 2?
A{"apple":10}
B{"banana":20}
Cempty
Ddoes not exist
💡 Hint
Check the 'Key State After' column in row 2 of the execution_table.
At which step does ZADD add the member 'cherry'?
AStep 2
BStep 4
CStep 3
DStep 5
💡 Hint
Look at the 'Action' column in the execution_table for when 'cherry' is added.
If 'banana' was already in 'myset' with score 20, what would the return value be after adding it again with the same score?
A1
B2
C0
D3
💡 Hint
Recall that ZADD returns the count of new members added, not updated ones, as shown in the variable_tracker.
Concept Snapshot
ZADD key score member [score member ...]
- Adds members with scores to a sorted set
- Creates the set if it doesn't exist
- Returns number of new members added
- Updates score if member exists but does not count as new
- Useful for ranking or sorted data storage
Full Transcript
ZADD is a Redis command to add members with scores to a sorted set. If the sorted set does not exist, ZADD creates it first. Each member is added with its score, and if the member already exists, its score is updated but not counted as a new addition. The command returns the number of new members added. For example, adding 'apple' with score 10, 'banana' with 20, and 'cherry' with 15 to a new sorted set 'myset' results in 'myset' containing these three members and the return value being 3. This process helps store and retrieve data sorted by scores efficiently.

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

  1. Step 1: Understand the purpose of ZADD

    The ZADD command is used to add members with associated scores to a sorted set in Redis.
  2. 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.
  3. Final Answer:

    Adds members with scores to a sorted set -> Option A
  4. 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

  1. Step 1: Recall ZADD syntax

    The correct syntax is: ZADD key score member. So the key comes first, then the score, then the member.
  2. Step 2: Match the syntax to options

    ZADD game_scores 100 player1 matches this order: ZADD game_scores 100 player1. Others have wrong order.
  3. Final Answer:

    ZADD game_scores 100 player1 -> Option A
  4. 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

  1. 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.
  2. Step 2: Sort members by score ascending

    Scores: Alice=50, Charlie=60, Bob=70. Sorted ascending: Alice, Charlie, Bob.
  3. Final Answer:

    ["Alice", "50", "Charlie", "60", "Bob", "70"] -> Option B
  4. 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

  1. 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.
  2. 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.
  3. Final Answer:

    The score and member are reversed; score must come before member -> Option C
  4. 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?
hard
A. ZADD ranking Anna 85 Ben 90 Cara 80
B. ZADD ranking Cara 80 85 Anna 90 Ben
C. ZADD ranking 85 Anna Cara 80 90 Ben
D. ZADD ranking 85 Anna 90 Ben 80 Cara

Solution

  1. Step 1: Recall ZADD syntax for multiple members

    ZADD syntax allows multiple score-member pairs: ZADD key score1 member1 score2 member2 ...
  2. Step 2: Match pairs correctly

    ZADD ranking 85 Anna 90 Ben 80 Cara lists pairs as 85 Anna, 90 Ben, 80 Cara, matching the players and scores in the order given.
  3. Final Answer:

    ZADD ranking 85 Anna 90 Ben 80 Cara -> Option D
  4. Quick Check:

    ZADD multiple pairs as score member [OK]
Hint: List pairs as score then member repeatedly [OK]
Common Mistakes:
  • Swapping member and score order
  • Mixing up pairs order
  • Using inconsistent score-member pairs
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