Advertisement
Intermediate Redis, Leaderboards with Sorted Sets

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

Short answer

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.

Save Redis mock interview
Advertisement
AModel answer
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.
Example

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.
Likely 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?
Related topics