Leaderboards with Sorted Sets

7 questions found

Why are Redis sorted sets a natural fit for building a leaderboard feature?

Beginner
A sorted set automatically keeps its members ordered by their associated score, meaning as you add or update a player's score, Redis automatically maintains the correct ranking order internally, letting you instantly retrieve the top players or a specific player's rank without needing to manually sort anything yourself.
ZADD leaderboard 1500 'player1'
ZADD leaderboard 2300 'player2'
ZREVRANGE leaderboard 0 9 WITHSCORES
Real-world example A mobile game maintains a global leaderboard using a sorted set, automatically keeping thousands of player scores properly ranked as they play and earn points throughout the day.

Common follow-ups: What happens if two players end up with the exact same score?;How do you update a player's score without needing to remove and re-add them?

Sets & Sorted Sets in Redis;Data Types

How do you retrieve the top ten highest scoring players from a Redis leaderboard?

Beginner
You use the ZREVRANGE command, specifying the sorted set key and a range from zero to nine, which returns the ten members with the highest scores in descending order, since ZREVRANGE returns results from highest to lowest score by default.
ZREVRANGE leaderboard 0 9 WITHSCORES
Real-world example A gaming leaderboard screen displays the current top ten players by calling ZREVRANGE, instantly retrieving both the player names and their scores in the correct descending order.

Common follow-ups: What is the difference between ZRANGE and ZREVRANGE?;How would you retrieve the bottom ten lowest scoring players instead?

Sets & Sorted Sets in Redis;Redis CLI & Basic Commands

How do you find a specific player's current rank on a leaderboard, along with the players immediately above and below them?

Intermediate
You use the ZREVRANK command to find a specific player's zero based rank position in descending score order, and then use ZREVRANGE around that rank position to retrieve the players immediately surrounding them, giving you a personalized view showing a player exactly where they stand relative to nearby competitors.
ZREVRANK leaderboard 'player5' -- returns rank, e.g. 42
ZREVRANGE leaderboard 40 44 WITHSCORES -- players around that rank
Real-world example A game shows a player their current global rank along with the two players just above and below them, calculated using ZREVRANK combined with a small ZREVRANGE query around their specific position.

Common follow-ups: What does ZREVRANK return if the specified player is not on the leaderboard at all?;How efficient is this lookup on a leaderboard with millions of players?

Sets & Sorted Sets in Redis;Redis Performance Tuning & Benchmarking

How do you atomically increase a player's score on a leaderboard, such as after they earn additional points during gameplay?

Intermediate
You use the ZINCRBY command, specifying the leaderboard key, the amount to add, and the player's name, which atomically increases that player's existing score by the specified amount, automatically re-adjusting their position in the ranking as needed without any risk of a race condition from multiple simultaneous score updates.
ZINCRBY leaderboard 50 'player1'
Real-world example A game safely increases a player's score by fifty points immediately after they complete a level, using ZINCRBY to atomically update their score even if many players are earning points at the exact same moment.

Common follow-ups: What happens if you call ZINCRBY on a player who is not yet on the leaderboard?;Can ZINCRBY be used with a negative value to decrease a score?

Sets & Sorted Sets in Redis;Redis CLI & Basic Commands

How would you design a leaderboard system that resets periodically, such as weekly, while still preserving historical rankings for past periods?

Advanced
You would create a new sorted set key for each period, such as including the week number in the key name, letting the current week's leaderboard start fresh while previous weeks' completed leaderboards remain fully intact and queryable, and you could set an expiration on very old period leaderboards to automatically clean them up after they are no longer needed.
ZADD leaderboard:week37 100 'player1'
ZADD leaderboard:week38 50 'player1' -- fresh start for the new week
EXPIRE leaderboard:week30 2592000 -- clean up very old weeks
Real-world example A competitive mobile game maintains a separate sorted set for each week's leaderboard, letting players compete fresh every week while still allowing the game to display historical results from previous weeks upon request.

Common follow-ups: How long should historical leaderboard data typically be retained?;How do you efficiently calculate an all time leaderboard alongside these periodic ones?

Expiration & Eviction;Sets & Sorted Sets in Redis

How would you implement a leaderboard that supports multiple different ranking categories, such as by region or by game mode, efficiently in Redis?

Advanced
You would create a separate sorted set for each distinct combination of category you need to rank by, such as leaderboard:region:us or leaderboard:mode:ranked, updating a player's score in every relevant category specific sorted set whenever they earn points, letting each category be queried independently and efficiently without any complex filtering logic.
ZADD leaderboard:global 1000 'player1'
ZADD leaderboard:region:us 1000 'player1'
ZADD leaderboard:mode:ranked 1000 'player1'
Real-world example A competitive game maintains separate leaderboards for global rankings, regional rankings, and specific game mode rankings, updating a player's score across all the relevant sorted sets simultaneously whenever they complete a match.

Common follow-ups: What is the overhead of maintaining several sorted sets simultaneously for the same player?;How do you keep all these separate leaderboards consistent with each other?

Sets & Sorted Sets in Redis;Redis Memory Optimization

How do you retrieve all players within a specific score range on a leaderboard, such as everyone between a thousand and two thousand points?

Intermediate
You use the ZRANGEBYSCORE command, specifying the leaderboard key along with a minimum and maximum score, which returns all members whose score falls within that specified range, letting you build features like showing players in a similar skill bracket to a specific user.
ZRANGEBYSCORE leaderboard 1000 2000 WITHSCORES
Real-world example A matchmaking system finds all players within a similar skill range to a specific user by querying a score range on the leaderboard, helping to pair players with comparable skill levels for a fair match.

Common follow-ups: Can you limit the number of results returned by ZRANGEBYSCORE for very large ranges?;How does ZRANGEBYSCORE handle players with scores exactly at the boundary values?

Sets & Sorted Sets in Redis;Data Types