35. Design a Leaderboard (Top-K)
35.8 How do you handle ties?
With 20 million teams built from 22 players, many teams have exactly the same points, especially early in the match. Two decisions:
1. Ordering: who's listed first? Redis orders equal scores by member name, which is arbitrary. Define a rule, for example earlier joining wins, and encode it into the score:
score = points × 2 × 10¹⁰ + (10¹⁰ − joined_at)
(Points are scaled by 2 to keep half-points from the 1.5× vice-captain as integers.) More points always wins; for equal points, the earlier join has a higher score. In the chapter's code, 121.0 points beats 120.5, and for two teams on 120.5, the one that joined at time 500 beats the one at 900. Keep the composite well below 2⁵³ so it's exact as a double-precision score: check the ranges for your real numbers.
2. Display rank: do equal points share a rank? Most leaderboards show competition ranking ("1, 2, 2, 4"): tied teams share a rank, and the next rank skips. In the chapter's code, scores 90, 85, 85, 70 rank 1, 2, 2, 4. Prizes for tied ranks are usually split according to the contest rules, decided by the final job, not the live board.
Memory code: "Break ties with a rule you can explain, and show shared ranks honestly."