TExcelTAD
Home › Redis › Redis Leaderboards

Redis Leaderboards

Games, fitness apps, sales dashboards and coding sites all show rankings: the top 10, your position, the players just above and below you. In SQL, "what is my rank among 5 million players?" means counting rows on every request. A Redis sorted set keeps members ordered as scores change, so every one of these questions is a single O(log N) command.

The basic leaderboard

Scores go in with ZADD (set) or ZINCRBY (add points). The top N comes from ZRANGE ... REV:

redis · try it yourself
ZADD lb:game 3200 mai 2950 an 4100 binh 2950 chi 3800 dung
ZINCRBY lb:game 500 an
ZRANGE lb:game 0 2 REV WITHSCORES
ZCARD lb:game
redis> ZADD lb:game 3200 mai 2950 an 4100 binh 2950 chi 3800 dung
(integer) 5
redis> ZINCRBY lb:game 500 an
"3450"
redis> ZRANGE lb:game 0 2 REV WITHSCORES
1) "binh"
2) "4100"
3) "dung"
4) "3800"
5) "an"
6) "3450"
redis> ZCARD lb:game
(integer) 5

A player's rank and neighbours

ZREVRANK gives the 0-based rank from the top, and WITHSCORE (Redis 7.2+) returns the score in the same call. To show the players around someone, fetch a rank range centred on them:

redis · try it yourself
ZADD lb:game 3200 mai 3450 an 4100 binh 2950 chi 3800 dung 2700 em 3000 giang
ZREVRANK lb:game mai
ZREVRANK lb:game mai WITHSCORE
ZRANGE lb:game 2 4 REV WITHSCORES
ZSCORE lb:game nobody
ZREVRANK lb:game nobody
redis> ZADD lb:game 3200 mai 3450 an 4100 binh 2950 chi 3800 dung 2700 em 3000 giang
(integer) 7
redis> ZREVRANK lb:game mai
(integer) 3
redis> ZREVRANK lb:game mai WITHSCORE
1) (integer) 3
2) "3200"
redis> ZRANGE lb:game 2 4 REV WITHSCORES
1) "an"
2) "3450"
3) "mai"
4) "3200"
5) "giang"
6) "3000"
redis> ZSCORE lb:game nobody
(nil)
redis> ZREVRANK lb:game nobody
(nil)

Mai is at rank 3 (fourth place), so the neighbour window is ranks 2 to 4. Unknown players return (nil), which your code should handle.

Ties

Equal scores are ordered by member name, and REV reverses that too. If ties should be broken by who got there first, encode time into the score. One trick: the score is points plus a fraction that gets smaller the later the score was reached:

python · try it yourself
MAX_TS = 10_000_000_000          # a timestamp far in the future

def score(points, reached_at):
    return points + (MAX_TS - reached_at) / MAX_TS

entries = [("mai", 3000, 1760000300), ("an", 3000, 1760000100), ("chi", 3100, 1760000500)]
ranked = sorted(entries, key=lambda e: score(e[1], e[2]), reverse=True)
for rank, (name, points, ts) in enumerate(ranked, start=1):
    print(rank, name, points, f"{score(points, ts):.10f}")
1 chi 3100 3100.8239999500
2 an 3000 3000.8239999900
3 mai 3000 3000.8239999700

An reached 3000 points before Mai, so An ranks higher. A double holds about 15–16 significant digits, so keep the points part small enough to leave room for the fraction.

Time-based leaderboards

"Top players this week" is just another key. Write to the daily key and combine days with ZUNIONSTORE, which adds the scores of the same member:

redis · try it yourself
ZINCRBY lb:day:2026-10-15 120 mai
ZINCRBY lb:day:2026-10-15 80 an
ZINCRBY lb:day:2026-10-16 60 mai
ZINCRBY lb:day:2026-10-16 150 chi
ZINCRBY lb:day:2026-10-17 90 an
ZUNIONSTORE lb:week:42 3 lb:day:2026-10-15 lb:day:2026-10-16 lb:day:2026-10-17
ZRANGE lb:week:42 0 -1 REV WITHSCORES
EXPIRE lb:week:42 3600
redis> ZINCRBY lb:day:2026-10-15 120 mai
"120"
redis> ZINCRBY lb:day:2026-10-15 80 an
"80"
redis> ZINCRBY lb:day:2026-10-16 60 mai
"60"
redis> ZINCRBY lb:day:2026-10-16 150 chi
"150"
redis> ZINCRBY lb:day:2026-10-17 90 an
"90"
redis> ZUNIONSTORE lb:week:42 3 lb:day:2026-10-15 lb:day:2026-10-16 lb:day:2026-10-17
(integer) 3
redis> ZRANGE lb:week:42 0 -1 REV WITHSCORES
1) "mai"
2) "180"
3) "an"
4) "170"
5) "chi"
6) "150"
redis> EXPIRE lb:week:42 3600
(integer) 1

Give daily keys a TTL (for example 40 days) and the computed weekly key a short one, recomputing it when it expires. AGGREGATE MAX instead of the default SUM gives a "best single day" ranking.

Showing names and avatars

A sorted set member should be a small, stable ID, not a display name that may change. Keep the details in hashes and fetch them for the page you show:

redis · try it yourself
ZADD lb:game 4100 user:3 3800 user:5 3450 user:2
HSET user:3 name Binh country VN
HSET user:5 name Dung country VN
HSET user:2 name An country FR
ZRANGE lb:game 0 1 REV
HMGET user:3 name country
HMGET user:5 name country
redis> ZADD lb:game 4100 user:3 3800 user:5 3450 user:2
(integer) 3
redis> HSET user:3 name Binh country VN
(integer) 2
redis> HSET user:5 name Dung country VN
(integer) 2
redis> HSET user:2 name An country FR
(integer) 2
redis> ZRANGE lb:game 0 1 REV
1) "user:3"
2) "user:5"
redis> HMGET user:3 name country
1) "Binh"
2) "VN"
redis> HMGET user:5 name country
1) "Dung"
2) "VN"

In application code, send the ZRANGE and the HMGETs in one pipeline, so the page costs two round trips at most.

Score percentiles

ZCOUNT answers "how many players have a score of at least X?", which gives you statements like "you're in the top 20 %":

redis · try it yourself
ZADD lb:exam 55 s1 62 s2 70 s3 74 s4 81 s5 85 s6 88 s7 90 s8 93 s9 97 s10
ZCOUNT lb:exam 85 +inf
ZCARD lb:exam
ZREVRANK lb:exam s7
redis> ZADD lb:exam 55 s1 62 s2 70 s3 74 s4 81 s5 85 s6 88 s7 90 s8 93 s9 97 s10
(integer) 10
redis> ZCOUNT lb:exam 85 +inf
(integer) 5
redis> ZCARD lb:exam
(integer) 10
redis> ZREVRANK lb:exam s7
(integer) 3

Five of the ten students scored 85 or more. Student s7 (88 points) has a reverse rank of 3, so three students scored higher and s7 is in fourth place: the top 40 %.

Test yourself

Which command returns the top 10 players of lb with their scores?

  1. ZRANGE lb 0 9 WITHSCORES
  2. ZRANGE lb 0 9 REV WITHSCORES
  3. ZRANGE lb 0 10 REV
  4. ZREVRANK lb 10

Why store user IDs instead of display names as sorted-set members?

  1. Names are not allowed
  2. IDs are stable and small; names can change and would break the ranking
  3. IDs sort faster
  4. Sorted sets only accept numbers

How do you build a weekly leaderboard from seven daily sorted sets?

  1. ZINTERSTORE with AGGREGATE MIN
  2. ZUNIONSTORE, which sums the members' scores
  3. SUNIONSTORE
  4. Copy them with RENAME