0
0
Redisquery~30 mins

Why sorted sets solve ranking problems in Redis - See It in Action

Choose your learning style9 modes available
Why Sorted Sets Solve Ranking Problems
📖 Scenario: Imagine you are building a leaderboard for a game. Players earn points, and you want to keep track of their ranks in real time. You need a way to store player scores and quickly find who is on top.
🎯 Goal: Build a Redis sorted set to store player scores, add a configuration for minimum score to consider, retrieve players above that score, and finally get the rank of a specific player.
📋 What You'll Learn
Create a sorted set called game_leaderboard with three players and their exact scores
Add a variable called min_score set to 50 to filter players
Use a Redis command to get all players with scores greater than or equal to min_score
Retrieve the rank of the player named player2 in the leaderboard
💡 Why This Matters
🌍 Real World
Leaderboards in games, ranking products by popularity, or sorting items by ratings.
💼 Career
Understanding sorted sets is key for backend developers working with real-time ranking systems and efficient data retrieval.
Progress0 / 4 steps
1
DATA SETUP: Create a sorted set with players and scores
Use the Redis command ZADD game_leaderboard 100 player1 75 player2 60 player3 to add three players with scores 100, 75, and 60 respectively to the sorted set called game_leaderboard.
Redis
Need a hint?

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

2
CONFIGURATION: Set a minimum score to filter players
Create a variable called min_score and set it to 50 to use as a threshold for filtering players in the leaderboard.
Redis
Need a hint?

Just assign the number 50 to a variable named min_score.

3
CORE LOGIC: Retrieve players with scores above the minimum score
Use the Redis command ZREVRANGEBYSCORE game_leaderboard +inf 50 WITHSCORES to get all players with scores greater than or equal to min_score, including their scores.
Redis
Need a hint?

Use ZREVRANGEBYSCORE with +inf as max and 50 as min to get players with high scores.

4
COMPLETION: Get the rank of player2 in the leaderboard
Use the Redis command ZREVRANK game_leaderboard player2 to find the rank of player2 in the leaderboard, where rank 0 is the highest score.
Redis
Need a hint?

Use ZREVRANK to get the rank of a player with highest score as rank 0.