0
0
Redisquery~10 mins

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

Choose your learning style9 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.