Ranking in real time
A leaderboard must answer two questions fast for millions of players: what is my rank, and who are the top N. A relational ORDER BY over a huge table is too slow when updates pour in constantly.
The sorted set fit
Redis offers a sorted set, a collection where each member has a numeric score and is kept ordered by that score. This maps perfectly onto a leaderboard.
- Update a score with an add command in logarithmic time.
- Read the top N with a range command directly off the ordered structure.
- Get a player's rank with a single rank lookup, no scan required.
Because the set stays sorted on every write, rank queries do not require recomputation.
Operational concerns
- For global scale you can shard by league or region, then merge tops.
- A periodic snapshot persists the set to durable storage for recovery.
- Time based boards, like weekly, use a separate set per period and expire old ones.
Key idea
A Redis sorted set keeps members ordered by score, giving fast top N and per player rank queries that a plain table cannot match.