0
0
Redisquery~10 mins

SADD and SREM for membership in Redis - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - SADD and SREM for membership
Start with empty set
SADD adds members
Check membership with SISMEMBER
SREM removes members
Check membership again
End with updated set
This flow shows how we add members to a set, check if they exist, remove some, and check membership again.
Execution Sample
Redis
SADD myset apple banana cherry
SISMEMBER myset banana
SREM myset banana
SISMEMBER myset banana
Add three fruits to a set, check if banana is in it, remove banana, then check membership again.
Execution Table
StepCommandActionResultSet State
1SADD myset apple banana cherryAdd members apple, banana, cherry3 (added count){apple, banana, cherry}
2SISMEMBER myset bananaCheck if banana is member1 (yes){apple, banana, cherry}
3SREM myset bananaRemove banana from set1 (removed count){apple, cherry}
4SISMEMBER myset bananaCheck if banana is member0 (no){apple, cherry}
💡 All commands executed; membership updated accordingly.
Variable Tracker
VariableStartAfter Step 1After Step 3Final
myset{}{apple, banana, cherry}{apple, cherry}{apple, cherry}
Key Moments - 2 Insights
Why does SISMEMBER return 1 before removal and 0 after?
Because before removal (Step 2), banana is in the set (Step 1 added it). After removal (Step 4), banana is no longer in the set (Step 3 removed it).
What does the number returned by SADD and SREM mean?
SADD returns how many new members were added (Step 1 returns 3). SREM returns how many members were removed (Step 3 returns 1).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the set after Step 1?
A{apple, cherry}
B{banana, cherry}
C{apple, banana, cherry}
D{}
💡 Hint
Check the 'Set State' column in Step 1.
At which step does banana get removed from the set?
AStep 2
BStep 3
CStep 4
DBanana is never removed
💡 Hint
Look at the 'Command' and 'Action' columns to find when SREM is called.
If we add 'banana' again after Step 3, what would SADD return?
A1
B0
C2
DError
💡 Hint
SADD returns how many new members were added; banana was removed before, so adding it again counts as 1.
Concept Snapshot
SADD key member [member ...] - Adds members to a set, returns count added
SREM key member [member ...] - Removes members, returns count removed
SISMEMBER key member - Checks if member exists, returns 1 or 0
Use SADD to add, SREM to remove, SISMEMBER to check membership.
Full Transcript
This example shows how Redis set commands SADD and SREM work for membership management. We start with an empty set 'myset'. Using SADD, we add three members: apple, banana, and cherry. The command returns 3, meaning all three were added. Next, SISMEMBER checks if banana is in the set and returns 1 (true). Then, SREM removes banana and returns 1, indicating one member removed. Finally, SISMEMBER checks banana again and returns 0 (false), confirming banana is no longer in the set. The set changes from empty to {apple, banana, cherry}, then to {apple, cherry} after removal. This shows how to add, check, and remove members in Redis sets.