0
0
Redisquery~30 mins

Why sorted sets combine uniqueness with ordering in Redis - See It in Action

Choose your learning style9 modes available
Why Sorted Sets Combine Uniqueness with Ordering
📖 Scenario: Imagine you are managing a leaderboard for a game. You want to keep track of players' scores so that each player appears only once, and the list is always sorted from highest to lowest score.
🎯 Goal: Build a Redis sorted set that stores player names with their scores, ensuring each player is unique and the set is ordered by score.
📋 What You'll Learn
Create a sorted set with specific player-score pairs
Add a new player with a score to the sorted set
Retrieve the players ordered by their scores
Ensure no duplicate players exist in the sorted set
💡 Why This Matters
🌍 Real World
Leaderboards in games, ranking websites, or any system where unique items need to be sorted by a score or priority.
💼 Career
Understanding sorted sets is essential for roles involving caching, real-time analytics, and building efficient ranking systems using Redis.
Progress0 / 4 steps
1
Create a sorted set with initial players and scores
Use the Redis command ZADD to create a sorted set called game_leaderboard with these exact player-score pairs: 100 Alice, 150 Bob, 120 Charlie.
Redis
Need a hint?

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

2
Add a new player with a score to the sorted set
Add a new player David with a score of 130 to the existing sorted set game_leaderboard using the ZADD command.
Redis
Need a hint?

Use ZADD game_leaderboard 130 David to add David with score 130.

3
Retrieve players ordered by their scores
Use the Redis command ZREVRANGE to get all players from game_leaderboard ordered from highest to lowest score, including their scores.
Redis
Need a hint?

Use ZREVRANGE game_leaderboard 0 -1 WITHSCORES to get all players sorted from highest to lowest score.

4
Add a duplicate player with a new score to update the score
Add the player Alice again with a new score of 160 using ZADD to update her score in game_leaderboard. This should keep the player unique and update the score.
Redis
Need a hint?

Use ZADD game_leaderboard 160 Alice to update Alice's score and keep her unique in the set.