0
0
Redisquery~30 mins

ZRANGE and ZREVRANGE for reading in Redis - Mini Project: Build & Apply

Choose your learning style9 modes available
Using ZRANGE and ZREVRANGE to Read Sorted Sets in Redis
📖 Scenario: You are managing a leaderboard for a game. Players earn points, and you want to store their scores in a Redis sorted set. You want to learn how to read the top players and the lowest scoring players using Redis commands.
🎯 Goal: Build Redis commands to create a sorted set of players with scores, then read the players in ascending and descending order using ZRANGE and ZREVRANGE.
📋 What You'll Learn
Create a sorted set called game_leaderboard with exact player-score pairs
Set a variable for the range start and end indexes
Use ZRANGE to get players with lowest scores
Use ZREVRANGE to get players with highest scores
💡 Why This Matters
🌍 Real World
Leaderboards in games, ranking systems, and priority queues often use Redis sorted sets to store and retrieve ordered data efficiently.
💼 Career
Knowing how to read sorted sets with ZRANGE and ZREVRANGE is essential for backend developers working with Redis to implement features like leaderboards, recommendation systems, and real-time analytics.
Progress0 / 4 steps
1
Create the sorted set game_leaderboard with players and scores
Use the Redis command ZADD to create a sorted set called game_leaderboard with these exact player-score pairs: "Alice" 50, "Bob" 75, "Charlie" 62, "Diana" 85, and "Eve" 40.
Redis
Need a hint?

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

2
Set variables for the range start and end indexes
Create two variables called start and end and set them to 0 and 2 respectively. These will be used to read the first three players from the leaderboard.
Redis
Need a hint?

Assign 0 to start and 2 to end.

3
Use ZRANGE to get players with lowest scores
Use the Redis command ZRANGE with the sorted set game_leaderboard and the variables start and end to get the players with the lowest scores. Include the WITHSCORES option to see their scores.
Redis
Need a hint?

Use ZRANGE game_leaderboard $start $end WITHSCORES to get players and their scores.

4
Use ZREVRANGE to get players with highest scores
Use the Redis command ZREVRANGE with the sorted set game_leaderboard and the variables start and end to get the players with the highest scores. Include the WITHSCORES option to see their scores.
Redis
Need a hint?

Use ZREVRANGE game_leaderboard $start $end WITHSCORES to get top players and their scores.