0
0
Redisquery~10 mins

Leaderboard implementation in Redis - Step-by-Step Execution

Choose your learning style9 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.