SISMEMBER for membership check in Redis - Time & Space Complexity
We want to understand how long it takes to check if an item is in a Redis set using SISMEMBER.
How does the time needed change when the set gets bigger?
Analyze the time complexity of the following code snippet.
# Check if 'item' is a member of the set 'myset'
SISMEMBER myset item
This command checks if a specific element exists inside a Redis set.
- Primary operation: A direct lookup in the set's internal data structure.
- How many times: The operation happens once per membership check.
Checking membership takes about the same time no matter how many items are in the set.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 1 |
| 100 | 1 |
| 1000 | 1 |
Pattern observation: The time stays roughly constant even as the set grows larger.
Time Complexity: O(1)
This means the membership check takes about the same time no matter how big the set is.
[X] Wrong: "Checking if an item is in a set takes longer as the set grows."
[OK] Correct: Redis sets use a data structure that allows quick lookups, so the time stays almost the same regardless of size.
Knowing how fast membership checks work helps you design efficient data lookups in real projects and shows you understand how Redis handles data internally.
"What if we used a Redis list instead of a set for membership checks? How would the time complexity change?"
Practice
SISMEMBER do?Solution
Step 1: Understand SISMEMBER purpose
The SISMEMBER command checks membership of a value in a Redis set.Step 2: Identify return values
It returns 1 if the value is in the set, otherwise 0.Final Answer:
Checks if a value is a member of a set and returns 1 or 0 -> Option CQuick Check:
SISMEMBER returns 1 or 0 [OK]
- Confusing SISMEMBER with SADD or SREM
- Expecting SISMEMBER to return the value itself
- Thinking SISMEMBER returns all set members
Solution
Step 1: Recall Redis command syntax
Redis commands use space-separated arguments without commas or parentheses.Step 2: Match correct syntax
Correct syntax isSISMEMBER key member, soSISMEMBER fruits appleis correct.Final Answer:
SISMEMBER fruits apple -> Option DQuick Check:
Redis commands use spaces, no commas or quotes needed [OK]
- Adding commas between arguments
- Using parentheses like a function call
- Adding quotes around arguments unnecessarily
SISMEMBER colors yellow?Solution
Step 1: Check if 'yellow' is in the set 'colors'
The set contains "red", "blue", and "green" but not "yellow".Step 2: Understand SISMEMBER return value
SISMEMBER returns 1 if member exists, else 0. Since "yellow" is not in the set, it returns 0.Final Answer:
0 -> Option AQuick Check:
Member absent = 0 [OK]
- Assuming it returns the value itself
- Confusing 0 with an error
- Thinking it returns the whole set
SISMEMBER colors without specifying a member. What will happen?Solution
Step 1: Check command argument requirements
SISMEMBER requires exactly two arguments: the set key and the member to check.Step 2: Analyze missing argument effect
Omitting the member argument causes a syntax error in Redis.Final Answer:
Syntax error -> Option AQuick Check:
Missing arguments cause syntax error [OK]
- Expecting default member to be checked
- Thinking it returns 0 if member missing
- Assuming it returns all members
Solution
Step 1: Check membership before adding
Use SISMEMBER to see if 'dave' is already in 'users'.Step 2: Add 'dave' only if not present
If SISMEMBER returns 0, then use SADD to add 'dave'.Final Answer:
SISMEMBER users dave SADD users dave -> Option BQuick Check:
Check membership then add if absent [OK]
- Adding before checking membership
- Removing member instead of adding
- Checking membership after adding
Ready to prove it?
Pass the test and this pattern moves unknown
