📖 Scenario: You are managing a small online store. You want to keep track of unique customers who have visited your website each day. You will use Redis sets to store customer IDs and then count how many unique customers visited on a particular day.
🎯 Goal: Build a Redis data structure to store unique customer IDs for a day and use the SCARD command to find out how many unique customers visited that day.
📋 What You'll Learn
Create a Redis set with a specific key for storing customer IDs
Add multiple customer IDs to the set
Use a variable to hold the key name for the set
Use the SCARD command to get the count of unique customers
Complete the Redis commands in the correct order
💡 Why This Matters
🌍 Real World
Tracking unique visitors or users is common in websites and apps to understand user engagement.
💼 Career
Knowing how to use Redis sets and SCARD helps in building efficient real-time analytics and user tracking systems.
Progress0 / 4 steps
1
Create a Redis set with customer IDs
Create a Redis set with the key "customers:2024-06-01" and add these customer IDs: "cust1", "cust2", "cust3" using the SADD command.
Redis
Hint
Use the SADD command followed by the set key and the customer IDs separated by spaces.
2
Set a variable for the Redis set key
Create a variable called key and set it to the string "customers:2024-06-01" to hold the Redis set key.
Redis
Hint
Use the SET command to create a variable named key with the value of the set key.
3
Add more customer IDs to the set
Use the SADD command to add two more customer IDs: "cust4" and "cust5" to the existing set.
Redis
Hint
Use SADD with the same set key to add new customer IDs.
4
Get the count of unique customers with SCARD
Use the SCARD command to get the number of unique customers in the set.
Redis
Hint
Use SCARD followed by the set key to get the count of unique members.
Practice
(1/5)
1. What does the Redis command SCARD do when used on a set?
easy
A. Returns all elements in the set
B. Deletes all elements from the set
C. Adds a new element to the set
D. Returns the number of unique elements in the set
Solution
Step 1: Understand the purpose of SCARD
The SCARD command is designed to count elements in a set, not modify or list them.
Step 2: Identify the correct behavior
SCARD returns the count of unique elements stored in the set.
Final Answer:
Returns the number of unique elements in the set -> Option D
Quick Check:
SCARD = set size count [OK]
Hint: SCARD counts set items without listing them [OK]
Common Mistakes:
Confusing SCARD with commands that list or delete elements
Thinking SCARD adds elements
Assuming SCARD returns elements instead of count
2. Which of the following is the correct syntax to get the size of a set named myset in Redis?
easy
A. SCARD myset
B. SCARD(myset)
C. GETSIZE myset
D. SIZE myset
Solution
Step 1: Recall Redis command syntax
Redis commands use the format COMMAND key without parentheses.
Step 2: Match correct command for set size
SCARD is the command to get set size, so SCARD myset is correct.
Final Answer:
SCARD myset -> Option A
Quick Check:
Correct Redis syntax = SCARD myset [OK]
Hint: Redis commands have no parentheses, just command and key [OK]
Common Mistakes:
Using parentheses like a function call
Using non-existent commands like GETSIZE or SIZE
Confusing SCARD with GET
3. Given the following Redis commands executed in order:
4. You run the command SCARD myset but get an error: WRONGTYPE Operation against a key holding the wrong kind of value. What is the most likely cause?
medium
A. myset is not a set but another data type like string or list
B. The key myset does not exist
C. SCARD command is misspelled
D. Redis server is down
Solution
Step 1: Understand the error message
The error indicates the key exists but is not a set type.
Step 2: Identify cause of WRONGTYPE error
SCARD only works on sets; if myset is string or list, this error occurs.
Final Answer:
myset is not a set but another data type like string or list -> Option A
Quick Check:
WRONGTYPE = wrong data type for SCARD [OK]
Hint: WRONGTYPE means key is not a set [OK]
Common Mistakes:
Assuming key does not exist causes this error
Thinking SCARD command spelling causes this error
Ignoring data type mismatch
5. You have two sets in Redis: setA with 3 elements and setB with 4 elements, where 2 elements overlap. You want to find the total number of unique elements across both sets without retrieving all elements. Which Redis command or combination should you use?
hard
A. SCARD setA + SCARD setB
B. SUNIONSTORE tempSet setA setB followed by SCARD tempSet
C. SINTER setA setB
D. SDIFF setA setB
Solution
Step 1: Understand the problem of counting unique elements across two sets