A match ends and the rank changes
Illustrative exampleNumbers here are illustrative: round figures to reason with, not measurements.
The game server posts the result. The results service records it and the player’s new total in PostgreSQL, then sets the player’s entry on the season board to the new total. The board is a Redis sorted set, which keeps members in order as their values change at O(log N) per update, and names leaderboards as a use (Redis docs (opens in a new tab)).
The trap is the obvious query. ORDER BY points sorts 80 million rows per request, and a count of the players with more points is O(rank) even with an index. How this path stays right for prizes is the third deep dive.
- The game server calls
POST /internal/.v1/ results - The results service records the result and the new season total in one transaction.
- The player’s entry on the board is set to the new total, and their new rank shows within a second or two.
Diagram as text
Components: Game server (Service); Results service (Service); PostgreSQL (SQL DB); Ordered board (Redis) (Cache). Connections: Game server → Results service (POST result); Results service → PostgreSQL (result + total); PostgreSQL → Ordered board (Redis) (new total, O(log N)), asynchronous.
Read:Learn: RedisLearn: Indexes and search fundamentals
Keep reading+1 min
Why a database index isn’t enough
An index on points keeps the rows in order, but a B-tree doesn’t store how many rows sit before each entry, so finding a rank still walks every row above it. A sorted set, or any order-statistic structure, keeps those counts as it goes, which is exactly what a rank needs.
Check yourself
A player around rank 30 million asks for their rank. What does the board do?
Show answerHide answer