What if you could see the top players update live without any slow sorting?
Why Leaderboard implementation in Redis? - Purpose & Use Cases
Imagine you want to keep track of the top players in a game by their scores. You try to do this by writing down each player's score on paper or in a simple list, then sorting it every time someone finishes playing.
This manual way is slow and confusing. Every time a new score comes in, you have to find the right place to put it, which takes a lot of time. Mistakes happen easily, and you might lose track of who is really on top.
Using a leaderboard in Redis lets you store scores in a way that automatically keeps them sorted. You can quickly add or update scores and instantly see the top players without sorting manually.
scores = [("Alice", 50), ("Bob", 70)] scores.sort(key=lambda x: x[1], reverse=True)
ZADD leaderboard 50 Alice ZADD leaderboard 70 Bob ZREVRANGE leaderboard 0 9 WITHSCORES
You can instantly see the top players and update scores in real time, making your game more fun and fair.
In a mobile game, players compete globally. The Redis leaderboard shows the top 10 players instantly, so everyone knows who is winning right now.
Manual score tracking is slow and error-prone.
Redis leaderboards keep scores sorted automatically.
Instantly update and retrieve top players with simple commands.
Practice
Solution
Step 1: Understand leaderboard requirements
A leaderboard needs to store members with scores and retrieve them sorted by score.Step 2: Identify Redis data structure
Redis Sorted Sets store members with scores and keep them sorted automatically.Final Answer:
Sorted Set -> Option BQuick Check:
Leaderboard = Sorted Set [OK]
- 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
Solution
Step 1: Recall ZADD syntax
ZADD command syntax is: ZADD key score member.Step 2: Match syntax to options
ZADD leaderboard 100 user1 uses ZADD leaderboard 100 user1 which matches the correct syntax.Final Answer:
ZADD leaderboard 100 user1 -> Option AQuick Check:
ZADD syntax = ZADD key score member [OK]
- Swapping score and member order
- Using ZINCRBY which increments score, not sets it
- Using ZREM which removes member instead of adding
ZADD leaderboard 50 user1 ZADD leaderboard 100 user2 ZADD leaderboard 75 user3 ZREVRANGE leaderboard 0 1 WITHSCORESWhat is the output of the last command?
Solution
Step 1: Add members with scores
user1=50, user2=100, user3=75 added to sorted set.Step 2: Use ZREVRANGE 0 1 WITHSCORES
This fetches top 2 members by highest score descending: user2(100), user3(75).Final Answer:
["user2", "100", "user3", "75"] -> Option AQuick Check:
Top 2 by score desc = user2, user3 [OK]
- Confusing ZREVRANGE with ZRANGE (ascending order)
- Ignoring WITHSCORES option output format
- Mixing member and score order in output
ZADD leaderboard 30 user1 ZADD leaderboard 60 user2 ZADD leaderboard 45 user3 ZREVRANGE leaderboard 0 2But the output is empty. What is the most likely cause?
Solution
Step 1: Check command correctness
ZADD commands have correct syntax and add members with scores.Step 2: Analyze empty output from ZREVRANGE
Empty output means the key 'leaderboard' likely does not exist or was deleted before ZREVRANGE.Final Answer:
The leaderboard key was deleted before ZREVRANGE -> Option DQuick Check:
Empty output = missing key [OK]
- Thinking WITHSCORES is required for output (it's optional)
- Assuming ZREVRANGE returns ascending order
- Believing ZADD argument order was wrong (it was correct)
Solution
Step 1: Understand filtering by score
We want top 3 users with scores >= 50, sorted descending.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.Final Answer:
Use ZREVRANGEBYSCORE leaderboard +inf 50 LIMIT 0 3 -> Option CQuick Check:
Filter by score with ZREVRANGEBYSCORE [OK]
- Using ZREVRANGE without score filtering
- Using ZRANGEBYSCORE which sorts ascending, not descending
- Filtering scores in app instead of Redis command
Ready to prove it?
Pass the test and this pattern moves unknown
