0
0
Redisquery~10 mins

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

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