📖 Scenario: You run a website and want to track unique visitors each day. You will use Redis sets to store visitor IDs because sets automatically keep only unique values.
🎯 Goal: Build a Redis data structure that stores unique visitor IDs for a specific day and allows you to count how many unique visitors came that day.
📋 What You'll Learn
Create a Redis set key for visitors on a specific date
Add visitor IDs to the set
Use a variable for the date key
Count the number of unique visitors using the set's cardinality
💡 Why This Matters
🌍 Real World
Websites and apps often need to count unique visitors daily to understand traffic and user engagement.
💼 Career
Knowing how to use Redis sets for unique tracking is useful for backend developers and data engineers working with real-time analytics.
Progress0 / 4 steps
1
Create a Redis set key for today's visitors
Create a Redis key called visitors:2023-06-01 to represent the set of unique visitors for June 1, 2023. Use the Redis command SADD to add visitor IDs user1, user2, and user3 to this set.
Redis
Hint
Use SADD followed by the key name and the visitor IDs separated by spaces.
2
Use a variable for the date key
Create a Redis variable called date_key and set it to visitors:2023-06-01. This variable will hold the key name for the visitor set.
Redis
Hint
Use the SET command to create a variable holding the key name.
3
Add more visitor IDs using the date_key variable
Use the SADD command with the variable date_key to add visitor IDs user4 and user5 to the set.
Redis
Hint
Use SADD with the key name to add new visitors. Redis CLI does not support variable substitution like $date_key.
4
Count the number of unique visitors
Use the SCARD command with the variable date_key to get the count of unique visitors for June 1, 2023.
Redis
Hint
SCARD returns the number of unique elements in a set.
Practice
(1/5)
1. What is the main advantage of using Redis sets for unique visitor tracking?
easy
A. They automatically prevent duplicate visitor IDs.
B. They store visitor data in sorted order.
C. They allow storing large text data efficiently.
D. They provide built-in encryption for visitor IDs.
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 A
Quick Check:
Redis sets = unique elements [OK]
Hint: Remember: sets never store duplicates [OK]
Common Mistakes:
Thinking sets store elements in order
Confusing sets with lists or hashes
Assuming sets encrypt data automatically
2. Which Redis command correctly adds a visitor ID 'user123' to a set named 'visitors'?
easy
A. SET visitors user123
B. ADD visitors user123
C. SADD visitors user123
D. APPEND visitors user123
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 C
Quick Check:
SADD adds members to sets [OK]
Hint: Use SADD to add members to sets [OK]
Common Mistakes:
Using SET which is for strings, not sets
Using ADD which is invalid in Redis
Using APPEND which is for strings
3. Given the following Redis commands executed in order:
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 A
Quick Check:
Unique visitors count = 2 [OK]
Hint: SCARD counts unique set members [OK]
Common Mistakes:
Counting duplicates as separate members
Confusing SCARD with SMEMBERS output
Assuming SCARD returns all added commands count
4. You run the command SADD visitors without specifying any visitor ID. What will happen?
medium
A. A new empty set is created.
B. An error occurs due to missing member argument.
C. The command adds a null member to the set.
D. The command is ignored silently.
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 B
Quick Check:
SADD needs members, else error [OK]
Hint: SADD must have at least one member argument [OK]
Common Mistakes:
Assuming empty sets can be created with SADD alone
Thinking null or empty string is added automatically
Expecting silent failure without error
5. You want to track unique visitors for multiple days using Redis sets. Which approach correctly counts total unique visitors across days stored as sets 'day1', 'day2', and 'day3'?
hard
A. Use DEL day1 day2 day3 then SCARD total.
B. Use SINTERSTORE total day1 day2 day3 then SCARD total.
C. Use SMEMBERS day1 then count manually.
D. Use SUNIONSTORE total day1 day2 day3 then SCARD total.
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 D
Quick Check:
Union sets + SCARD = total unique visitors [OK]
Hint: Use SUNIONSTORE to combine sets before counting [OK]
Common Mistakes:
Using SINTERSTORE which finds common visitors only
Counting members manually instead of using SCARD
Deleting sets before counting unique visitors
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.