Unique visitor tracking with sets in Redis - Time & Space Complexity
When tracking unique visitors, we want to know how the work grows as more visitors come in.
We ask: How much time does it take to add and count unique visitors as the list grows?
Analyze the time complexity of the following code snippet.
SADD unique_visitors user1
SADD unique_visitors user2
SADD unique_visitors user3
SCARD unique_visitors
This code adds visitors to a set and then counts how many unique visitors there are.
- Primary operation: Adding a visitor to the set with
SADD. - How many times: Once per visitor added.
- Counting operation:
SCARDcounts unique visitors once.
Each new visitor is checked and added if unique. Counting is quick regardless of size.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 adds + 1 count |
| 100 | 100 adds + 1 count |
| 1000 | 1000 adds + 1 count |
Pattern observation: Adding visitors grows linearly; counting stays constant time.
Time Complexity: O(n)
This means the time to add visitors grows directly with the number of visitors.
[X] Wrong: "The total time to track n unique visitors is O(1)."
[OK] Correct: Although each SADD is O(1), performing n additions requires O(n) time total.
Understanding how adding unique items scales helps you explain data handling in real apps clearly and confidently.
"What if we used a list instead of a set to track visitors? How would the time complexity change?"
Practice
Solution
Step 1: Understand Redis sets behavior
Redis sets store unique elements only, so duplicates are not saved.Step 2: Relate to visitor tracking
This means each visitor ID is stored once, ensuring uniqueness automatically.Final Answer:
They automatically prevent duplicate visitor IDs. -> Option AQuick Check:
Redis sets = unique elements [OK]
- Thinking sets store elements in order
- Confusing sets with lists or hashes
- Assuming sets encrypt data automatically
Solution
Step 1: Identify the correct command to add to a set
The Redis command to add members to a set is SADD.Step 2: Check syntax correctness
SADD visitors user123 adds 'user123' to the 'visitors' set correctly.Final Answer:
SADD visitors user123 -> Option CQuick Check:
SADD adds members to sets [OK]
- Using SET which is for strings, not sets
- Using ADD which is invalid in Redis
- Using APPEND which is for strings
SADD visitors user1 SADD visitors user2 SADD visitors user1 SCARD visitorsWhat will be the output of
SCARD visitors?Solution
Step 1: Analyze SADD commands
First adds 'user1', second adds 'user2', third tries to add 'user1' again (ignored as duplicate).Step 2: Understand SCARD output
SCARD returns the count of unique members in the set, which are 'user1' and 'user2'.Final Answer:
2 -> Option AQuick Check:
Unique visitors count = 2 [OK]
- Counting duplicates as separate members
- Confusing SCARD with SMEMBERS output
- Assuming SCARD returns all added commands count
SADD visitors without specifying any visitor ID. What will happen?Solution
Step 1: Check SADD command requirements
SADD requires at least one member argument to add to the set.Step 2: Result of missing member argument
Running SADD without members causes a syntax error in Redis.Final Answer:
An error occurs due to missing member argument. -> Option BQuick Check:
SADD needs members, else error [OK]
- Assuming empty sets can be created with SADD alone
- Thinking null or empty string is added automatically
- Expecting silent failure without error
Solution
Step 1: Understand set operations for unique visitors
To combine unique visitors from multiple sets, use SUNIONSTORE to create a union set.Step 2: Count unique visitors in combined set
After union, SCARD on the combined set gives total unique visitors across days.Final Answer:
Use SUNIONSTORE total day1 day2 day3 then SCARD total. -> Option DQuick Check:
Union sets + SCARD = total unique visitors [OK]
- Using SINTERSTORE which finds common visitors only
- Counting members manually instead of using SCARD
- Deleting sets before counting unique visitors
Ready to prove it?
Pass the test and this pattern moves unknown
