How do you find a specific player's current rank on a leaderboard, along with the players immediately above and below them?
Short answer
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…
Advertisement
AModel answer
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.
Example
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.
Likely 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?
Related topics