0
0
Redisquery~30 mins

Set vs sorted set for membership in Redis - Hands-On Comparison

Choose your learning style9 modes available
Set vs Sorted Set for Membership in Redis
📖 Scenario: You are managing a simple online event registration system. You want to keep track of users who have registered for an event and also maintain a leaderboard of users based on their registration time.
🎯 Goal: Build Redis data structures to store registered users using a Set and a Sorted Set. Then check membership in both structures.
📋 What You'll Learn
Create a Redis Set called registered_users with three users: user1, user2, and user3.
Create a Redis Sorted Set called registration_leaderboard with the same three users and their registration timestamps as scores: user1 with score 100, user2 with score 200, and user3 with score 150.
Check if user2 is a member of the registered_users Set.
Check if user2 is a member of the registration_leaderboard Sorted Set.
💡 Why This Matters
🌍 Real World
Tracking event registrations and maintaining leaderboards are common tasks in web applications and gaming platforms.
💼 Career
Understanding Redis Sets and Sorted Sets is important for backend developers working with fast data storage and retrieval, especially in real-time systems.
Progress0 / 4 steps
1
Create a Redis Set for registered users
Use the Redis command SADD to create a Set called registered_users and add these exact members: user1, user2, and user3.
Redis
Need a hint?

Use SADD followed by the set name and the exact user names separated by spaces.

2
Create a Redis Sorted Set for registration leaderboard
Use the Redis command ZADD to create a Sorted Set called registration_leaderboard with these exact members and scores: user1 with score 100, user2 with score 200, and user3 with score 150.
Redis
Need a hint?

Use ZADD followed by the sorted set name, then pairs of score and member.

3
Check membership of user2 in the Set
Use the Redis command SISMEMBER to check if user2 is a member of the registered_users Set.
Redis
Need a hint?

SISMEMBER returns 1 if the member exists, 0 if not.

4
Check membership of user2 in the Sorted Set
Use the Redis command ZRANK to check if user2 is a member of the registration_leaderboard Sorted Set. If ZRANK returns a number, the user is a member.
Redis
Need a hint?

ZRANK returns the rank index if the member exists, or nil if not.