Bird
Raised Fist0
Redisquery~10 mins

Unique visitor tracking with sets in Redis - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Unique visitor tracking with sets
User visits website
↓
Add user ID to Redis set
↓
Redis set stores unique IDs
↓
Query set size for unique visitor count
↓
Return count of unique visitors
Each visitor's ID is added to a Redis set, which automatically keeps only unique IDs. The set size gives the count of unique visitors.
Execution Sample
Redis
SADD visitors user1
SADD visitors user2
SADD visitors user1
SCARD visitors
Add user1 and user2 to the 'visitors' set, try adding user1 again, then get the count of unique visitors.
Execution Table
StepCommandActionSet ContentOutput
1SADD visitors user1Add 'user1' to set{user1}1 (added)
2SADD visitors user2Add 'user2' to set{user1, user2}1 (added)
3SADD visitors user1Try add 'user1' again{user1, user2}0 (already exists)
4SCARD visitorsCount unique visitors{user1, user2}2
💡 All commands executed; final unique visitor count is 2
Variable Tracker
VariableStartAfter 1After 2After 3Final
visitors set{}{user1}{user1, user2}{user1, user2}{user1, user2}
Key Moments - 2 Insights
Why does adding 'user1' the second time not increase the set size?
Because Redis sets only store unique elements, the second SADD for 'user1' does not add a duplicate, as shown in execution_table step 3 where output is 0.
How do we get the number of unique visitors?
By using the SCARD command on the set, which returns the count of unique elements, as shown in execution_table step 4.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the output of the second SADD command?
A1 (added)
B0 (already exists)
C2 (added)
DError
💡 Hint
Check execution_table row 2 under Output column
At which step does the set stop changing?
AStep 1
BStep 2
CStep 3
DStep 4
💡 Hint
Look at Set Content column in execution_table rows 3 and 4
If we add a new user 'user3' after step 3, what will SCARD return?
A2
B3
C1
D0
💡 Hint
Adding a new unique user increases set size by 1; see variable_tracker final state
Concept Snapshot
Use Redis sets to track unique visitors.
SADD adds user IDs; duplicates ignored.
SCARD returns count of unique IDs.
Set automatically keeps unique values.
Simple commands give real-time unique visitor count.
Full Transcript
This example shows how Redis sets track unique visitors by adding user IDs with SADD. Each SADD adds a user if not already present. Duplicate adds return 0 and do not change the set. The SCARD command returns the number of unique visitors by counting set members. The execution table traces each command's effect on the set and output. The variable tracker shows the set content after each step. This method efficiently counts unique visitors without duplicates.

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

  1. Step 1: Understand Redis sets behavior

    Redis sets store unique elements only, so duplicates are not saved.
  2. Step 2: Relate to visitor tracking

    This means each visitor ID is stored once, ensuring uniqueness automatically.
  3. Final Answer:

    They automatically prevent duplicate visitor IDs. -> Option A
  4. 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

  1. Step 1: Identify the correct command to add to a set

    The Redis command to add members to a set is SADD.
  2. Step 2: Check syntax correctness

    SADD visitors user123 adds 'user123' to the 'visitors' set correctly.
  3. Final Answer:

    SADD visitors user123 -> Option C
  4. 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:
SADD visitors user1
SADD visitors user2
SADD visitors user1
SCARD visitors
What will be the output of SCARD visitors?
medium
A. 2
B. 0
C. 1
D. 3

Solution

  1. Step 1: Analyze SADD commands

    First adds 'user1', second adds 'user2', third tries to add 'user1' again (ignored as duplicate).
  2. Step 2: Understand SCARD output

    SCARD returns the count of unique members in the set, which are 'user1' and 'user2'.
  3. Final Answer:

    2 -> Option A
  4. 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

  1. Step 1: Check SADD command requirements

    SADD requires at least one member argument to add to the set.
  2. Step 2: Result of missing member argument

    Running SADD without members causes a syntax error in Redis.
  3. Final Answer:

    An error occurs due to missing member argument. -> Option B
  4. 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

  1. Step 1: Understand set operations for unique visitors

    To combine unique visitors from multiple sets, use SUNIONSTORE to create a union set.
  2. Step 2: Count unique visitors in combined set

    After union, SCARD on the combined set gives total unique visitors across days.
  3. Final Answer:

    Use SUNIONSTORE total day1 day2 day3 then SCARD total. -> Option D
  4. 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 unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard