Bird
Raised Fist0
Redisquery~10 mins

Leaderboard implementation in Redis - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Leaderboard implementation
Add player with score
↓
Store in sorted set
↓
Update score if needed
↓
Query top players
↓
Display leaderboard
Players and their scores are stored in a sorted set. Scores can be updated, and the top players are queried to display the leaderboard.
Execution Sample
Redis
ZADD leaderboard 1000 "Alice"
ZADD leaderboard 1500 "Bob"
ZADD leaderboard 1200 "Carol"
ZREVRANGE leaderboard 0 2 WITHSCORES
Add three players with scores and then get the top 3 players with their scores.
Execution Table
StepCommandActionSorted Set StateOutput
1ZADD leaderboard 1000 "Alice"Add Alice with score 1000{"Alice":1000}1 (new element added)
2ZADD leaderboard 1500 "Bob"Add Bob with score 1500{"Alice":1000, "Bob":1500}1 (new element added)
3ZADD leaderboard 1200 "Carol"Add Carol with score 1200{"Alice":1000, "Bob":1500, "Carol":1200}1 (new element added)
4ZRANGE leaderboard 0 2 WITHSCORESGet top 3 players by score ascending{"Alice":1000, "Bob":1500, "Carol":1200}["Alice", "1000", "Carol", "1200", "Bob", "1500"]
5ZREVRANGE leaderboard 0 2 WITHSCORESGet top 3 players by score descending{"Alice":1000, "Bob":1500, "Carol":1200}["Bob", "1500", "Carol", "1200", "Alice", "1000"]
6ZINCRBY leaderboard 300 "Alice"Increase Alice's score by 300{"Alice":1300, "Bob":1500, "Carol":1200}1300 (new score of Alice)
7ZREVRANGE leaderboard 0 2 WITHSCORESGet top 3 players by score descending after update{"Alice":1300, "Bob":1500, "Carol":1200}["Bob", "1500", "Alice", "1300", "Carol", "1200"]
8ZRANK leaderboard "Carol"Get Carol's rank (0-based, ascending){"Alice":1300, "Bob":1500, "Carol":1200}0
9ZREVRANK leaderboard "Carol"Get Carol's rank (0-based, descending){"Alice":1300, "Bob":1500, "Carol":1200}2
10ZREM leaderboard "Bob"Remove Bob from leaderboard{"Alice":1300, "Carol":1200}1 (Bob removed)
11ZRANGE leaderboard 0 -1 WITHSCORESGet all players ascending after removal{"Alice":1300, "Carol":1200}["Carol", "1200", "Alice", "1300"]
💡 All commands executed; leaderboard updated and queried successfully.
Variable Tracker
VariableStartAfter 1After 2After 3After 6After 10Final
leaderboard{}{"Alice":1000}{"Alice":1000, "Bob":1500}{"Alice":1000, "Bob":1500, "Carol":1200}{"Alice":1300, "Bob":1500, "Carol":1200}{"Alice":1300, "Carol":1200}{"Alice":1300, "Carol":1200}
Key Moments - 3 Insights
Why does ZRANGE return players in ascending order of scores?
ZRANGE returns elements from lowest to highest score as shown in execution_table row 4, so the player with the smallest score appears first.
How does ZREVRANGE differ from ZRANGE in output?
ZREVRANGE returns elements from highest to lowest score (row 5 and 7), reversing the order compared to ZRANGE.
What happens when we increase a player's score with ZINCRBY?
ZINCRBY updates the player's score and reorders the leaderboard accordingly, as seen in row 6 and 7 where Alice's score increases and her rank improves.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table row 4. What is the first player returned by ZRANGE leaderboard 0 2 WITHSCORES?
AAlice
BBob
CCarol
DNo player
💡 Hint
Check the Output column in row 4 of execution_table.
At which step does Alice's score increase?
AStep 3
BStep 10
CStep 6
DStep 2
💡 Hint
Look for ZINCRBY command in execution_table.
If Bob is removed at step 10, what is the final number of players in the leaderboard?
A1
B2
C3
D0
💡 Hint
Check the Sorted Set State after step 10 and final in variable_tracker.
Concept Snapshot
Redis Leaderboard uses a sorted set.
ZADD adds or updates player scores.
ZRANGE returns players by ascending score.
ZREVRANGE returns players by descending score.
ZINCRBY increments a player's score.
ZRANK and ZREVRANK get player ranks.
Full Transcript
This visual execution traces how a Redis leaderboard works using sorted sets. Players are added with ZADD commands, each with a score. The leaderboard stores players and their scores in a sorted set. ZRANGE retrieves players ordered by ascending scores, while ZREVRANGE retrieves them by descending scores. Scores can be updated with ZINCRBY, which changes the player's position in the leaderboard. Players can be removed with ZREM. The execution table shows each command, the action taken, the state of the leaderboard after the command, and the output returned. The variable tracker follows the leaderboard state changes after each command. Key moments clarify common confusions about ordering and score updates. The quiz tests understanding of the order of players, score updates, and leaderboard size after removal. The snapshot summarizes the main Redis commands used for leaderboard implementation.

Practice

(1/5)
1. What Redis data structure is best suited for implementing a leaderboard where members have scores?
easy
A. Hash
B. Sorted Set
C. List
D. String

Solution

  1. Step 1: Understand leaderboard requirements

    A leaderboard needs to store members with scores and retrieve them sorted by score.
  2. Step 2: Identify Redis data structure

    Redis Sorted Sets store members with scores and keep them sorted automatically.
  3. Final Answer:

    Sorted Set -> Option B
  4. Quick Check:

    Leaderboard = Sorted Set [OK]
Hint: Leaderboards need sorted data, use Sorted Sets [OK]
Common Mistakes:
  • Choosing List which is ordered by insertion, not score
  • Using Hash which stores key-value but not sorted by score
  • Using String which holds plain text, not multiple members
2. Which Redis command correctly adds or updates a member's score in a leaderboard stored as a sorted set?
easy
A. ZADD leaderboard 100 user1
B. ZADD leaderboard user1 100
C. ZINCRBY leaderboard 100 user1
D. ZREM leaderboard user1

Solution

  1. Step 1: Recall ZADD syntax

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

    ZADD leaderboard 100 user1 uses ZADD leaderboard 100 user1 which matches the correct syntax.
  3. Final Answer:

    ZADD leaderboard 100 user1 -> Option A
  4. Quick Check:

    ZADD syntax = ZADD key score member [OK]
Hint: ZADD syntax: key, score, then member [OK]
Common Mistakes:
  • Swapping score and member order
  • Using ZINCRBY which increments score, not sets it
  • Using ZREM which removes member instead of adding
3. Given the following Redis commands executed in order:
ZADD leaderboard 50 user1
ZADD leaderboard 100 user2
ZADD leaderboard 75 user3
ZREVRANGE leaderboard 0 1 WITHSCORES
What is the output of the last command?
medium
A. ["user2", "100", "user3", "75"]
B. ["user1", "50", "user3", "75"]
C. ["user3", "75", "user2", "100"]
D. ["user2", "100", "user1", "50"]

Solution

  1. Step 1: Add members with scores

    user1=50, user2=100, user3=75 added to sorted set.
  2. Step 2: Use ZREVRANGE 0 1 WITHSCORES

    This fetches top 2 members by highest score descending: user2(100), user3(75).
  3. Final Answer:

    ["user2", "100", "user3", "75"] -> Option A
  4. Quick Check:

    Top 2 by score desc = user2, user3 [OK]
Hint: ZREVRANGE 0 1 gets top 2 highest scores [OK]
Common Mistakes:
  • Confusing ZREVRANGE with ZRANGE (ascending order)
  • Ignoring WITHSCORES option output format
  • Mixing member and score order in output
4. You run the following Redis commands:
ZADD leaderboard 30 user1
ZADD leaderboard 60 user2
ZADD leaderboard 45 user3
ZREVRANGE leaderboard 0 2
But the output is empty. What is the most likely cause?
medium
A. ZREVRANGE returns ascending order by default
B. ZREVRANGE syntax is incorrect, it needs WITHSCORES
C. ZADD commands used wrong order of arguments
D. The leaderboard key was deleted before ZREVRANGE

Solution

  1. Step 1: Check command correctness

    ZADD commands have correct syntax and add members with scores.
  2. Step 2: Analyze empty output from ZREVRANGE

    Empty output means the key 'leaderboard' likely does not exist or was deleted before ZREVRANGE.
  3. Final Answer:

    The leaderboard key was deleted before ZREVRANGE -> Option D
  4. Quick Check:

    Empty output = missing key [OK]
Hint: Empty ZREVRANGE means key missing or deleted [OK]
Common Mistakes:
  • Thinking WITHSCORES is required for output (it's optional)
  • Assuming ZREVRANGE returns ascending order
  • Believing ZADD argument order was wrong (it was correct)
5. You want to implement a leaderboard that shows the top 3 users but excludes any user with a score below 50. Which Redis commands or approach should you use?
hard
A. Use ZRANGEBYSCORE leaderboard 50 +inf LIMIT 0 3
B. Use ZREVRANGE leaderboard 0 2 and filter scores < 50 in application
C. Use ZREVRANGEBYSCORE leaderboard +inf 50 LIMIT 0 3
D. Use ZRANGE leaderboard 0 2 WITHSCORES and exclude scores < 50

Solution

  1. Step 1: Understand filtering by score

    We want top 3 users with scores >= 50, sorted descending.
  2. Step 2: Choose correct Redis command

    ZREVRANGEBYSCORE leaderboard +inf 50 LIMIT 0 3 returns members with scores between +inf and 50 descending, limited to 3.
  3. Final Answer:

    Use ZREVRANGEBYSCORE leaderboard +inf 50 LIMIT 0 3 -> Option C
  4. Quick Check:

    Filter by score with ZREVRANGEBYSCORE [OK]
Hint: Use ZREVRANGEBYSCORE with score limits and LIMIT [OK]
Common Mistakes:
  • Using ZREVRANGE without score filtering
  • Using ZRANGEBYSCORE which sorts ascending, not descending
  • Filtering scores in app instead of Redis command
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