Bird
Raised Fist0
Redisquery~10 mins

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

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

Practice

(1/5)
1. What does the Redis command SADD myset member1 do?
easy
A. Adds 'member1' to the set 'myset' if it is not already present.
B. Removes 'member1' from the set 'myset'.
C. Returns all members of the set 'myset'.
D. Deletes the set 'myset' entirely.

Solution

  1. Step 1: Understand the SADD command

    SADD adds one or more unique members to a Redis set.
  2. Step 2: Apply to the example

    Using SADD with 'myset' and 'member1' adds 'member1' if not already in 'myset'.
  3. Final Answer:

    Adds 'member1' to the set 'myset' if it is not already present. -> Option A
  4. Quick Check:

    SADD adds unique members [OK]
Hint: SADD adds unique items; SREM removes them [OK]
Common Mistakes:
  • Confusing SADD with SREM
  • Thinking SADD deletes sets
  • Assuming SADD returns all members
2. Which of the following is the correct syntax to remove 'member2' from a Redis set named 'myset'?
easy
A. REMOVE myset member2
B. SADD myset member2
C. SREM myset member2
D. DEL myset member2

Solution

  1. Step 1: Identify the command to remove members

    SREM is the Redis command used to remove members from a set.
  2. Step 2: Check syntax correctness

    The correct syntax is SREM followed by the set name and the member to remove.
  3. Final Answer:

    SREM myset member2 -> Option C
  4. Quick Check:

    SREM removes members correctly [OK]
Hint: Use SREM to remove members, not DEL or REMOVE [OK]
Common Mistakes:
  • Using SADD instead of SREM to remove
  • Using DEL which deletes keys, not members
  • Using non-existent REMOVE command
3. Given the following Redis commands executed in order:
SADD fruits apple banana orange
SREM fruits banana
What will the command SMEMBERS fruits return?
medium
A. apple, orange
B. banana, orange
C. apple, banana, orange
D. apple, banana

Solution

  1. Step 1: Add members to the set 'fruits'

    SADD adds 'apple', 'banana', and 'orange' to the set 'fruits'.
  2. Step 2: Remove 'banana' from the set

    SREM removes 'banana' from 'fruits', leaving 'apple' and 'orange'.
  3. Step 3: Retrieve members with SMEMBERS

    SMEMBERS returns all current members: 'apple' and 'orange'.
  4. Final Answer:

    apple, orange -> Option A
  5. Quick Check:

    Removed banana, so only apple and orange remain [OK]
Hint: SREM removes members; SMEMBERS shows current set [OK]
Common Mistakes:
  • Assuming removed members still appear
  • Confusing SREM with DEL
  • Expecting order in SMEMBERS output
4. You run the command SREM myset memberX but get no error and the member still appears in SMEMBERS myset. What is the most likely reason?
medium
A. SREM command syntax is incorrect.
B. Redis server is down.
C. 'myset' is not a set but a string key.
D. 'memberX' was never in 'myset' to begin with.

Solution

  1. Step 1: Understand SREM behavior when member absent

    SREM does not error if the member is not in the set; it simply does nothing.
  2. Step 2: Check if member exists in the set

    If 'memberX' still appears in SMEMBERS output, it means it was never removed or was never absent.
  3. Step 3: Consider other options

    Syntax errors cause errors; wrong key type causes errors; server down causes no response.
  4. Final Answer:

    'memberX' was never in 'myset' to begin with. -> Option D
  5. Quick Check:

    SREM silently ignores missing members [OK]
Hint: SREM ignores missing members without error [OK]
Common Mistakes:
  • Expecting error if member not present
  • Confusing key type errors with SREM behavior
  • Assuming server down causes silent no-op
5. You want to maintain a Redis set 'online_users' that tracks currently logged-in users. Which sequence of commands correctly updates the set when user 'alice' logs in and then logs out?
hard
A. SREM online_users alice
SADD online_users alice
B. SADD online_users alice
SREM online_users alice
C. DEL online_users alice
SADD online_users alice
D. SADD online_users alice
DEL online_users alice

Solution

  1. Step 1: Add user on login

    Use SADD to add 'alice' to 'online_users' when she logs in.
  2. Step 2: Remove user on logout

    Use SREM to remove 'alice' from 'online_users' when she logs out.
  3. Step 3: Evaluate other options

    DEL deletes the entire set, which is not desired for single user updates.
  4. Final Answer:

    SADD online_users alice
    SREM online_users alice
    -> Option B
  5. Quick Check:

    Add with SADD, remove with SREM for membership [OK]
Hint: Add with SADD, remove with SREM; avoid DEL for single members [OK]
Common Mistakes:
  • Using DEL which deletes whole set
  • Removing before adding user
  • Confusing SADD and SREM order
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard