ZADD for adding scored members in Redis - Time & Space Complexity
When we add members with scores to a sorted set in Redis, it is important to know how the time needed changes as we add more members.
We want to understand how the work grows when we add many scored members using ZADD.
Analyze the time complexity of the following code snippet.
ZADD myset 10 member1
ZADD myset 20 member2
ZADD myset 15 member3
ZADD myset 25 member4
ZADD myset 5 member5
This code adds five members with scores to a sorted set named 'myset'. Each member is placed according to its score.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Inserting each member into the sorted set in the correct order.
- How many times: Once per member added, so the number of insertions equals the number of members.
Each time we add a member, Redis must find the right place in the sorted set to insert it, which takes more time as the set grows.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 30 operations |
| 100 | About 700 operations |
| 1000 | About 10,000 operations |
Pattern observation: The work grows a bit faster than the number of members because finding the right place takes longer as the set grows.
Time Complexity: O(log n)
This means adding each member takes a little more time as the set gets bigger, but it grows slowly and efficiently.
[X] Wrong: "Adding a member with ZADD always takes the same time no matter how big the set is."
[OK] Correct: Actually, Redis must find the correct position for the new member, which takes more steps as the set grows, so time increases slowly with size.
Understanding how Redis handles sorted sets helps you explain efficient data storage and retrieval, a useful skill in many real-world projects.
"What if we added multiple members at once using a single ZADD command? How would the time complexity change?"
Practice
ZADD do?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 AQuick Check:
ZADD adds scored members [OK]
- Confusing ZADD with ZREM or ZRANGE
- Thinking ZADD creates string keys
- Assuming ZADD deletes members
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 AQuick Check:
ZADD key score member order [OK]
- Swapping score and member positions
- Putting member before key
- Using wrong argument order
ZADD leaderboard 50 Alice 70 Bob 60 Charlie ZRANGE leaderboard 0 -1 WITHSCORES
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 BQuick Check:
ZRANGE sorts ascending by score [OK]
- Assuming ZRANGE sorts by insertion order
- Mixing up ascending and descending order
- Ignoring WITHSCORES option
ZADD myset player1 100Solution
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 CQuick Check:
ZADD requires score before member [OK]
- Swapping score and member positions
- Thinking key name is invalid
- Believing numeric scores are disallowed
Solution
Step 1: Recall ZADD syntax for multiple members
ZADD syntax allows multiple score-member pairs: ZADD key score1 member1 score2 member2 ...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.Final Answer:
ZADD ranking 85 Anna 90 Ben 80 Cara -> Option DQuick Check:
ZADD multiple pairs as score member [OK]
- Swapping member and score order
- Mixing up pairs order
- Using inconsistent score-member pairs
Ready to prove it?
Pass the test and this pattern moves unknown
