SADD and SREM for membership in Redis - Time & Space Complexity
When working with Redis sets, it is important to know how the time to add or remove members changes as the set grows.
We want to understand how the cost of adding or removing items scales with the number of items in the set.
Analyze the time complexity of the following Redis commands.
SADD myset member1
SADD myset member2
SREM myset member1
SREM myset member3
This code adds members to a set and removes members from it, checking membership as needed.
Look at what happens when adding or removing members.
- Primary operation: Checking if a member exists and then adding or removing it.
- How many times: Each command works on one member at a time, so operations happen once per command.
Adding or removing a member takes about the same time no matter how many members are in the set.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 1 operation per add or remove |
| 100 | 1 operation per add or remove |
| 1000 | 1 operation per add or remove |
Pattern observation: The time to add or remove a member stays about the same even as the set grows.
Time Complexity: O(1)
This means adding or removing a member takes constant time, no matter how big the set is.
[X] Wrong: "Adding or removing members gets slower as the set grows because it has to check all members."
[OK] Correct: Redis uses efficient data structures for sets that let it find and update members quickly without scanning the whole set.
Knowing that Redis set operations like SADD and SREM run in constant time helps you understand how Redis handles data efficiently, a useful skill for real-world projects and interviews.
"What if we used a Redis list instead of a set for membership? How would the time complexity of adding and removing change?"
Practice
SADD myset member1 do?Solution
Step 1: Understand the SADD command
SADD adds one or more unique members to a Redis set.Step 2: Apply to the example
Using SADD with 'myset' and 'member1' adds 'member1' if not already in 'myset'.Final Answer:
Adds 'member1' to the set 'myset' if it is not already present. -> Option AQuick Check:
SADD adds unique members [OK]
- Confusing SADD with SREM
- Thinking SADD deletes sets
- Assuming SADD returns all members
Solution
Step 1: Identify the command to remove members
SREM is the Redis command used to remove members from a set.Step 2: Check syntax correctness
The correct syntax is SREM followed by the set name and the member to remove.Final Answer:
SREM myset member2 -> Option CQuick Check:
SREM removes members correctly [OK]
- Using SADD instead of SREM to remove
- Using DEL which deletes keys, not members
- Using non-existent REMOVE command
SADD fruits apple banana orangeSREM fruits bananaWhat will the command
SMEMBERS fruits return?Solution
Step 1: Add members to the set 'fruits'
SADD adds 'apple', 'banana', and 'orange' to the set 'fruits'.Step 2: Remove 'banana' from the set
SREM removes 'banana' from 'fruits', leaving 'apple' and 'orange'.Step 3: Retrieve members with SMEMBERS
SMEMBERS returns all current members: 'apple' and 'orange'.Final Answer:
apple, orange -> Option AQuick Check:
Removed banana, so only apple and orange remain [OK]
- Assuming removed members still appear
- Confusing SREM with DEL
- Expecting order in SMEMBERS output
SREM myset memberX but get no error and the member still appears in SMEMBERS myset. What is the most likely reason?Solution
Step 1: Understand SREM behavior when member absent
SREM does not error if the member is not in the set; it simply does nothing.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.Step 3: Consider other options
Syntax errors cause errors; wrong key type causes errors; server down causes no response.Final Answer:
'memberX' was never in 'myset' to begin with. -> Option DQuick Check:
SREM silently ignores missing members [OK]
- Expecting error if member not present
- Confusing key type errors with SREM behavior
- Assuming server down causes silent no-op
Solution
Step 1: Add user on login
Use SADD to add 'alice' to 'online_users' when she logs in.Step 2: Remove user on logout
Use SREM to remove 'alice' from 'online_users' when she logs out.Step 3: Evaluate other options
DEL deletes the entire set, which is not desired for single user updates.Final Answer:
SADD online_users alice
SREM online_users alice -> Option BQuick Check:
Add with SADD, remove with SREM for membership [OK]
- Using DEL which deletes whole set
- Removing before adding user
- Confusing SADD and SREM order
Ready to prove it?
Pass the test and this pattern moves unknown
