📖 Scenario: You are managing a Redis database that stores a set of usernames who have access to a special feature on your website.You want to check if a particular user is in this set to grant or deny access.
🎯 Goal: Build a Redis command sequence to create a set of usernames, add users to it, and check if a specific username is a member of that set using the SISMEMBER command.
📋 What You'll Learn
Create a Redis set called feature_users with the usernames alice, bob, and charlie
Add a configuration variable check_user with the value bob
Use the SISMEMBER command to check if check_user is in the feature_users set
Complete the command sequence to perform the membership check
💡 Why This Matters
🌍 Real World
Checking user permissions or feature access quickly using Redis sets is common in web applications for fast membership tests.
💼 Career
Understanding Redis set commands and membership checks is valuable for backend developers and system administrators managing caching and user access control.
Progress0 / 4 steps
1
Create the Redis set feature_users with usernames
Write the Redis command to create a set called feature_users and add the usernames alice, bob, and charlie to it using the SADD command.
Redis
Hint
Use the SADD command followed by the set name and the usernames separated by spaces.
2
Set the username to check in check_user
Write the Redis command to set a variable called check_user with the value bob using the SET command.
Redis
Hint
Use the SET command to assign the value bob to the key check_user.
3
Use SISMEMBER to check membership of check_user in feature_users
Write the Redis command to check if the value stored in check_user is a member of the set feature_users using the SISMEMBER command with the key feature_users and the value bob.
Redis
Hint
Use SISMEMBER with the set name and the username to check membership.
4
Complete the membership check command sequence
Add the final Redis command to retrieve the value of check_user using the GET command, so you can confirm which username you are checking membership for.
Redis
Hint
Use the GET command with the key check_user to retrieve its value.
Practice
(1/5)
1. What does the Redis command SISMEMBER do?
easy
A. Adds a value to a set
B. Removes a value from a set
C. Checks if a value is a member of a set and returns 1 or 0
D. Returns all members of a set
Solution
Step 1: Understand SISMEMBER purpose
The SISMEMBER command checks membership of a value in a Redis set.
Step 2: Identify return values
It returns 1 if the value is in the set, otherwise 0.
Final Answer:
Checks if a value is a member of a set and returns 1 or 0 -> Option C
Quick Check:
SISMEMBER returns 1 or 0 [OK]
Hint: SISMEMBER returns 1 if present, else 0 [OK]
Common Mistakes:
Confusing SISMEMBER with SADD or SREM
Expecting SISMEMBER to return the value itself
Thinking SISMEMBER returns all set members
2. Which of the following is the correct syntax to check if 'apple' is in the set 'fruits' using Redis?
easy
A. SISMEMBER(fruits, apple)
B. SISMEMBER 'fruits' 'apple'
C. SISMEMBER fruits, apple
D. SISMEMBER fruits apple
Solution
Step 1: Recall Redis command syntax
Redis commands use space-separated arguments without commas or parentheses.
Step 2: Match correct syntax
Correct syntax is SISMEMBER key member, so SISMEMBER fruits apple is correct.
Final Answer:
SISMEMBER fruits apple -> Option D
Quick Check:
Redis commands use spaces, no commas or quotes needed [OK]
Hint: Use space-separated arguments without quotes or commas [OK]
Common Mistakes:
Adding commas between arguments
Using parentheses like a function call
Adding quotes around arguments unnecessarily
3. Given the Redis set 'colors' contains {"red", "blue", "green"}, what is the output of SISMEMBER colors yellow?
medium
A. 0
B. 1
C. red
D. Error
Solution
Step 1: Check if 'yellow' is in the set 'colors'
The set contains "red", "blue", and "green" but not "yellow".
Step 2: Understand SISMEMBER return value
SISMEMBER returns 1 if member exists, else 0. Since "yellow" is not in the set, it returns 0.
Final Answer:
0 -> Option A
Quick Check:
Member absent = 0 [OK]
Hint: If value not in set, SISMEMBER returns 0 [OK]
Common Mistakes:
Assuming it returns the value itself
Confusing 0 with an error
Thinking it returns the whole set
4. You run the command SISMEMBER colors without specifying a member. What will happen?
medium
A. Syntax error
B. Returns 1
C. Returns 0
D. Returns all members
Solution
Step 1: Check command argument requirements
SISMEMBER requires exactly two arguments: the set key and the member to check.
Step 2: Analyze missing argument effect
Omitting the member argument causes a syntax error in Redis.
Final Answer:
Syntax error -> Option A
Quick Check:
Missing arguments cause syntax error [OK]
Hint: SISMEMBER needs two arguments; missing one causes error [OK]
Common Mistakes:
Expecting default member to be checked
Thinking it returns 0 if member missing
Assuming it returns all members
5. You have a Redis set 'users' with members {"alice", "bob", "carol"}. You want to check if 'dave' is a member and add him only if he is not already present. Which sequence of commands correctly does this?
hard
A. SISMEMBER users dave
SREM users dave
B. SISMEMBER users dave
SADD users dave
C. SADD users dave
SISMEMBER users dave
D. SREM users dave
SADD users dave
Solution
Step 1: Check membership before adding
Use SISMEMBER to see if 'dave' is already in 'users'.
Step 2: Add 'dave' only if not present
If SISMEMBER returns 0, then use SADD to add 'dave'.
Final Answer:
SISMEMBER users dave
SADD users dave -> Option B
Quick Check:
Check membership then add if absent [OK]
Hint: Check with SISMEMBER before SADD to avoid duplicates [OK]
Common Mistakes:
Adding before checking membership
Removing member instead of adding
Checking membership after adding
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.