Bird
Raised Fist0
Redisquery~30 mins

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

Choose your learning style10 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
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
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
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
Hint

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

Practice

(1/5)
1. Why are Redis sorted sets ideal for ranking problems like leaderboards?
easy
A. Because they store elements with scores, keeping them automatically ordered.
B. Because they store data without any order, making insertion faster.
C. Because they only allow unique string values without scores.
D. Because they use hashes to store key-value pairs.

Solution

  1. Step 1: Understand sorted set structure

    Sorted sets store each element with a numeric score that determines its order.
  2. Step 2: Relate ordering to ranking

    This automatic ordering by score makes it easy to rank items, like players in a leaderboard.
  3. Final Answer:

    Because they store elements with scores, keeping them automatically ordered. -> Option A
  4. Quick Check:

    Sorted sets = ordered by score [OK]
Hint: Sorted sets keep scores ordered automatically for rankings [OK]
Common Mistakes:
  • Thinking sorted sets store unordered data
  • Confusing sorted sets with hashes
  • Ignoring the role of scores in ordering
2. Which Redis command correctly adds a member with a score to a sorted set named game_scores?
easy
A. HSET game_scores player1 1500
B. SADD game_scores player1 1500
C. ZADD player1 1500 game_scores
D. ZADD game_scores 1500 player1

Solution

  1. Step 1: Recall ZADD syntax

    ZADD command syntax is: ZADD key score member.
  2. Step 2: Match syntax to options

    ZADD game_scores 1500 player1 uses ZADD with key 'game_scores', score 1500, and member 'player1' correctly.
  3. Final Answer:

    ZADD game_scores 1500 player1 -> Option D
  4. Quick Check:

    ZADD key score member = correct syntax [OK]
Hint: ZADD syntax: ZADD key score member [OK]
Common Mistakes:
  • Using SADD instead of ZADD for sorted sets
  • Swapping member and score positions
  • Using hash commands like HSET incorrectly
3. Given the sorted set ZADD leaderboard 100 Alice 200 Bob 150 Carol, what does ZREVRANGE leaderboard 0 1 WITHSCORES return?
medium
A. ["Bob", "200", "Carol", "150"]
B. ["Alice", "100", "Carol", "150"]
C. ["Carol", "150", "Alice", "100"]
D. ["Bob", "200", "Alice", "100"]

Solution

  1. Step 1: Understand ZREVRANGE behavior

    ZREVRANGE returns members ordered from highest to lowest score.
  2. Step 2: Identify top two members

    Scores: Bob=200, Carol=150, Alice=100. Top two are Bob and Carol.
  3. Final Answer:

    ["Bob", "200", "Carol", "150"] -> Option A
  4. Quick Check:

    Highest scores first = Bob, Carol [OK]
Hint: ZREVRANGE returns highest scores first [OK]
Common Mistakes:
  • Confusing ZRANGE with ZREVRANGE order
  • Ignoring WITHSCORES option format
  • Mixing member order in output
4. You run ZADD scores player1 50 player2 100 but get an error. What is the likely cause?
medium
A. Scores must be strings, not numbers.
B. ZADD cannot add multiple members at once.
C. ZADD requires score and member pairs; this command has incorrect pairs.
D. The key 'scores' must exist before adding members.

Solution

  1. Step 1: Recall ZADD syntax

    ZADD expects pairs of score and member: score1 member1 score2 member2 ...
  2. Step 2: Analyze given command

    Command is ZADD scores player1 50 player2 100 -- after key, 'player1' (string) where score (number) expected, incorrect pairs.
  3. Final Answer:

    ZADD requires score and member pairs; this command has incorrect pairs. -> Option C
  4. Quick Check:

    Score must come before member in pairs [OK]
Hint: Score must always come before member in ZADD [OK]
Common Mistakes:
  • Swapping score and member order
  • Trying to add multiple members with wrong syntax
  • Assuming key must pre-exist
5. You want to create a leaderboard that shows the top 3 players with their ranks and scores using Redis sorted sets. Which sequence of commands achieves this correctly?
hard
A. ZADD leaderboard 300 Alice 250 Bob 400 Carol; ZRANGE leaderboard 0 2 WITHSCORES
B. ZADD leaderboard 300 Alice 250 Bob 400 Carol; ZREVRANGE leaderboard 0 2 WITHSCORES
C. ZADD leaderboard Alice 300 Bob 250 Carol 400; ZRANGE leaderboard 0 2
D. SADD leaderboard Alice Bob Carol; ZRANGE leaderboard 0 2 WITHSCORES

Solution

  1. Step 1: Add players with scores correctly

    ZADD syntax requires score before member, so 'ZADD leaderboard 300 Alice 250 Bob 400 Carol' is correct.
  2. Step 2: Retrieve top 3 players by highest score

    ZREVRANGE with 0 2 and WITHSCORES returns top 3 players in descending order with scores.
  3. Final Answer:

    ZADD leaderboard 300 Alice 250 Bob 400 Carol; ZREVRANGE leaderboard 0 2 WITHSCORES -> Option B
  4. Quick Check:

    Use ZADD with scores, then ZREVRANGE for top ranks [OK]
Hint: Use ZADD then ZREVRANGE for top ranked results [OK]
Common Mistakes:
  • Using SADD which ignores scores
  • Swapping score and member in ZADD
  • Using ZRANGE instead of ZREVRANGE for descending order
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard