Redis Leaderboards
A leaderboard ranks players, students, or sellers by score. Games, quiz apps, and sales dashboards all show top ten lists that update every second. Redis Sorted Sets keep members in score order at all times, so ranking needs no sorting work in your code.
The Race Podium Analogy
Think of a race where runners step onto a podium as soon as they finish. The podium always shows first, second, and third place in order. A Sorted Set acts as that podium. You change a score, and Redis moves the member to the right spot instantly.
Sorted Set "game:board" ┌──────┬────────┬───────┐ │ Rank │ Player │ Score │ ├──────┼────────┼───────┤ │ 1 │ meena │ 2200 │ │ 2 │ asha │ 1500 │ │ 3 │ ravi │ 1320 │ │ 4 │ zoya │ 980 │ └──────┴────────┴───────┘
Adding Players and Scores
127.0.0.1:6379> ZADD game:board 1500 "asha" 1320 "ravi" 2200 "meena" 980 "zoya" (integer) 4
ZADD inserts new members and replaces the score of existing ones.
Raising a Score
127.0.0.1:6379> ZINCRBY game:board 300 "ravi" "1620"
ZINCRBY adds to the current score in one atomic step. Two game servers can award points to the same player at once without losing either update.
Showing the Top Players
127.0.0.1:6379> ZRANGE game:board 0 2 REV WITHSCORES 1) "meena" 2) "2200" 3) "ravi" 4) "1620" 5) "asha" 6) "1500"
The REV option lists from the highest score down. Positions 0 to 2 return the top three.
Finding One Player's Rank and Score
127.0.0.1:6379> ZREVRANK game:board "asha" (integer) 2 127.0.0.1:6379> ZSCORE game:board "asha" "1500"
Ranks start at 0, so the app adds 1 before showing "Rank 3" to the player.
Showing Neighbors Around a Player
Players enjoy seeing who sits just above and below them.
Step 1: rank = ZREVRANK game:board "asha" → 2
Step 2: ZRANGE game:board (rank-2) (rank+2) REV WITHSCORES
(clip the start at 0)
Result: Two players above Asha, Asha, two players below her.
Counting Players in a Score Range
127.0.0.1:6379> ZCOUNT game:board 1000 2000 (integer) 2 127.0.0.1:6379> ZCARD game:board (integer) 4
ZCOUNT counts members inside a score range. ZCARD counts all members.
Handling Ties
Members with the same score sort by name in alphabetical order. For a "first to reach the score wins" rule, build the score from the points and the finish time, such as points multiplied by 10 billion minus the timestamp.
Weekly and Monthly Boards
Key name pattern: board:alltime board:2026-W24 board:2026-06 Each score event updates all three keys. Set EXPIRE on weekly keys so old boards clean themselves up.
Combine boards later with ZUNIONSTORE when you need a total across several periods.
Removing Players
127.0.0.1:6379> ZREM game:board "zoya" (integer) 1
Practical Example – Quiz App
A quiz app with 2 million players. Player answers correctly: ZINCRBY quiz:board 10 "player:77" Home screen top 10: ZRANGE quiz:board 0 9 REV WITHSCORES "Your rank" badge: ZREVRANK quiz:board "player:77" Each command finishes in well under a millisecond.
Key Points
- A Sorted Set keeps members ordered by score automatically.
- ZADD and ZINCRBY update scores atomically.
- ZRANGE with REV lists top players, and ZREVRANK finds one player's position.
- Separate keys per period make weekly and monthly boards simple.
- Ties fall back to alphabetical order unless you encode time into the score.
