0
0
Redisquery~30 mins

Range queries for scoring in Redis - Mini Project: Build & Apply

Choose your learning style9 modes available
Range Queries for Scoring in Redis
📖 Scenario: You are managing a leaderboard for a game. Players earn points, and you want to store their scores in Redis. Later, you want to find players who scored within certain ranges to reward them.
🎯 Goal: Build a Redis sorted set to store player scores, set a minimum score threshold, query players within a score range, and finally add a new player with a score.
📋 What You'll Learn
Create a sorted set called game_scores with three players and their exact scores
Create a variable called min_score and set it to 50
Use the ZREVRANGEBYSCORE command to get players with scores between 100 and min_score
Add a new player player4 with score 75 to the game_scores sorted set
💡 Why This Matters
🌍 Real World
Leaderboards in games or apps often use Redis sorted sets to store and query user scores efficiently.
💼 Career
Understanding Redis sorted sets and range queries is valuable for backend developers working on real-time scoring, ranking, or recommendation systems.
Progress0 / 4 steps
1
Create the sorted set with initial player scores
Create a Redis sorted set called game_scores and add these exact members with scores: player1 with 120, player2 with 90, and player3 with 60.
Redis
Need a hint?

Use the ZADD command with the sorted set name, followed by score and member pairs.

2
Set the minimum score threshold
Create a variable called min_score and set it to 50.
Redis
Need a hint?

Assign the number 50 to the variable min_score.

3
Query players within the score range
Use the ZREVRANGEBYSCORE command to get players from game_scores with scores between 100 and the variable min_score. Use the syntax: ZREVRANGEBYSCORE game_scores 100 min_score.
Redis
Need a hint?

Use ZREVRANGEBYSCORE with the sorted set name and score range to get players in descending order.

4
Add a new player with a score
Add a new player called player4 with a score of 75 to the game_scores sorted set using the ZADD command.
Redis
Need a hint?

Use ZADD with the sorted set name, score, and member name to add the new player.