0
0
Redisquery~30 mins

Top-N queries in Redis - Mini Project: Build & Apply

Choose your learning style9 modes available
Top-N Queries with Redis Sorted Sets
📖 Scenario: You are managing a leaderboard for a mobile game. Players earn points, and you want to keep track of the top scorers in real-time.
🎯 Goal: Build a Redis sorted set to store player scores, then query the top 3 players with the highest scores.
📋 What You'll Learn
Create a Redis sorted set called game_leaderboard with 5 players and their scores.
Add a new player score to the game_leaderboard.
Query the top 3 players with the highest scores using a Redis command.
Retrieve the scores along with the player names.
💡 Why This Matters
🌍 Real World
Leaderboards in games, ranking systems in apps, or any scenario where you need to keep track of top scores or ratings in real-time.
💼 Career
Understanding Redis sorted sets and top-N queries is valuable for backend developers working on performance-critical applications like gaming, social media, and analytics platforms.
Progress0 / 4 steps
1
Create the initial leaderboard with 5 players
Use the Redis command ZADD game_leaderboard to add these exact players and scores: "Alice" 1500, "Bob" 1200, "Charlie" 1800, "Diana" 1600, "Eve" 1400.
Redis
Need a hint?

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

2
Add a new player score
Add a new player "Frank" with a score of 1700 to the game_leaderboard using the ZADD command.
Redis
Need a hint?

Use ZADD game_leaderboard 1700 Frank to add Frank's score.

3
Query the top 3 players by score
Use the Redis command ZREVRANGE game_leaderboard 0 2 WITHSCORES to get the top 3 players with the highest scores and their scores.
Redis
Need a hint?

Use ZREVRANGE with 0 2 to get the top 3, and WITHSCORES to include scores.

4
Retrieve and display the top 3 players with their scores
Use the Redis command ZREVRANGE game_leaderboard 0 2 WITHSCORES to retrieve the top 3 players and their scores, completing the leaderboard query.
Redis
Need a hint?

This command returns the top 3 players and their scores in descending order.