What if you could instantly find if someone is in your list and also know their rank without any hassle?
Set vs sorted set for membership in Redis - When to Use Which
Imagine you have a big list of friends' names written on paper. You want to check if a certain friend is on that list. You look through the list one by one, which takes a lot of time and effort.
Manually searching through a list is slow and tiring. You might miss names or check the same name multiple times. Also, if you want the list sorted or want to know who joined first, it becomes even more confusing and error-prone.
Using Redis sets and sorted sets helps you quickly check if a name exists without scanning everything. Sets give you fast membership checks, while sorted sets keep your items ordered with scores, making it easy to find and rank members efficiently.
Check each name in list manually: for name in friends_list: if name == target_name: return True return False
Use Redis set command: SISMEMBER friends_set target_name Use Redis sorted set command: ZSCORE friends_sorted_set target_name
This lets you instantly know if something is in your collection and also lets you keep track of order or ranking without extra work.
Think about a social app where you want to see if a user follows another. Using sets, you quickly check membership. Using sorted sets, you can also see who followed first or rank followers by activity.
Manual searching is slow and error-prone.
Sets provide fast membership checks.
Sorted sets add ordering and ranking to membership.
Practice
Solution
Step 1: Understand the purpose of Sets
Sets store unique items and allow fast membership checks without any order.Step 2: Compare with Sorted Sets
Sorted Sets also store unique items but maintain order using scores, which is unnecessary if order doesn't matter.Final Answer:
Set -> Option AQuick Check:
Membership without order = Set [OK]
- Confusing Sorted Set with Set for membership
- Thinking List is best for membership
- Assuming Hash stores unique items like Set
myset?Solution
Step 1: Identify the command for adding to a Set
The commandSADDadds members to a Set in Redis.Step 2: Check other commands
ZADDis for Sorted Sets,LPUSHis for Lists, andHSETis for Hashes.Final Answer:
SADD myset member1 -> Option AQuick Check:
SADD adds to Set [OK]
- Using ZADD for Sets
- Using LPUSH which is for Lists
- Using HSET which is for Hashes
SADD fruits apple banana orangeZADD scores 10 alice 20 bob 15 carolWhich command checks if 'banana' is a member of the Set
fruits?Solution
Step 1: Identify the correct membership check for Sets
SISMEMBERchecks if an item is in a Set.Step 2: Match the key and member
The Set isfruits, and we check for 'banana'. SoSISMEMBER fruits bananais correct.Final Answer:
SISMEMBER fruits banana -> Option DQuick Check:
Check membership in Set = SISMEMBER [OK]
- Using ZSCORE or ZRANK on Sets
- Checking membership on wrong key
- Confusing Sorted Set commands with Set commands
scores. Which command is incorrect and will cause an error?Solution
Step 1: Understand commands for Sorted Sets
ZSCORE,ZRANK, andZREMare valid Sorted Set commands.Step 2: Identify the invalid command
SISMEMBERis only for Sets, not Sorted Sets, so it will cause an error.Final Answer:
SISMEMBER scores alice -> Option CQuick Check:
SISMEMBER invalid for Sorted Set [OK]
- Using SISMEMBER on Sorted Sets
- Confusing ZREM with SREM
- Assuming SISMEMBER works on all collections
leaderboard with player scores. You want to check if a player john exists in the leaderboard and get his rank. Which sequence of commands is correct?Solution
Step 1: Check existence and score in Sorted Set
ZSCOREreturns the score if the member exists, otherwise null.Step 2: Get the rank if member exists
ZRANKreturns the rank of the member in the Sorted Set.Step 3: Validate command order
First check score withZSCORE, then get rank withZRANK. UsingSISMEMBERis invalid for Sorted Sets.Final Answer:
ZSCORE leaderboard john
ZRANK leaderboard john -> Option BQuick Check:
Use ZSCORE then ZRANK for Sorted Set membership and rank [OK]
- Using SISMEMBER on Sorted Sets
- Checking rank before confirming existence
- Mixing Set commands with Sorted Set commands
Ready to prove it?
Pass the test and this pattern moves unknown
