0
0
Redisquery~10 mins

Set vs sorted set for membership in Redis - Visual Side-by-Side Comparison

Choose your learning style9 modes available
Concept Flow - Set vs sorted set for membership
Start
Check if element is in Set
|Yes
Return True
End
No
Return False
End
Start
Check if element is in Sorted Set
|Yes
Return True
End
No
Return False
End
Both Set and Sorted Set check if an element exists and return true or false. Sorted Set also maintains order but membership check works similarly.
Execution Sample
Redis
SADD myset apple banana cherry
SISMEMBER myset banana
ZADD myzset 1 apple 2 banana 3 cherry
ZSCORE myzset banana
Add elements to Set and Sorted Set, then check membership and score for 'banana'.
Execution Table
StepCommandActionResultExplanation
1SADD myset apple banana cherryAdd elements to Set33 elements added to Set 'myset'
2SISMEMBER myset bananaCheck if 'banana' in Set1'banana' is a member, returns 1 (true)
3ZADD myzset 1 apple 2 banana 3 cherryAdd elements with scores to Sorted Set33 elements added to Sorted Set 'myzset'
4ZSCORE myzset bananaGet score of 'banana' in Sorted Set2'banana' has score 2
5ZSCORE myzset orangeGet score of 'orange' in Sorted Setnil'orange' not in Sorted Set, returns nil
6ZSCORE myzset appleGet score of 'apple' in Sorted Set1'apple' has score 1
7ZSCORE myzset grapeGet score of 'grape' in Sorted Setnil'grape' not member, returns nil
💡 Commands complete; membership checks return 1 or nil indicating presence or absence.
Variable Tracker
VariableStartAfter Step 1After Step 3Final
mysetempty{apple, banana, cherry}{apple, banana, cherry}{apple, banana, cherry}
myzsetemptyempty{apple:1, banana:2, cherry:3}{apple:1, banana:2, cherry:3}
Key Moments - 2 Insights
Why does SISMEMBER return 1 or 0 but ZSCORE returns a score or nil?
SISMEMBER returns 1 if element exists and 0 if not (boolean style). ZSCORE returns the element's score if it exists, or nil if it doesn't. So ZSCORE can be used to check membership by seeing if the result is nil or not (see execution_table rows 4-7).
Can we use Sorted Set membership check as fast as Set membership?
Yes, both Set and Sorted Set membership checks are fast (O(1) or O(log N)) in Redis. But Sorted Set stores scores which adds extra data, while Set only stores elements (see variable_tracker showing extra score data in myzset).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what does SISMEMBER myset banana return at step 2?
A0
B1
Cnil
Dbanana
💡 Hint
Check the 'Result' column in execution_table row 2.
At which step does ZSCORE return nil indicating the element is not in the sorted set?
AStep 4
BStep 6
CStep 5
DStep 7
💡 Hint
Look for 'nil' in the 'Result' column in execution_table.
If we add 'orange' to myzset with score 4, what will ZSCORE myzset orange return?
A1
Bnil
C4
Dbanana
💡 Hint
ZSCORE returns the score of the element if it exists, see step 4 for banana's score.
Concept Snapshot
Set stores unique elements with fast membership checks.
Sorted Set stores elements with scores, maintaining order.
SISMEMBER returns 1 if element exists, 0 if not.
ZSCORE returns element's score or nil if absent.
Use SISMEMBER for simple membership, ZSCORE for membership plus score info.
Full Transcript
This visual execution compares Redis Set and Sorted Set for membership checks. First, elements are added to a Set and a Sorted Set. Then, SISMEMBER checks if 'banana' is in the Set, returning 1 for true. ZSCORE retrieves the score of 'banana' in the Sorted Set, returning 2. If an element is not present, ZSCORE returns nil. Both data structures allow fast membership checks, but Sorted Set also stores scores for ordering. SISMEMBER returns 1 or 0, while ZSCORE returns a score or nil. This helps decide which to use based on whether you need ordering or just membership.