#53 Write-Heavy Top-K
Strava Leaderboard
Rank athletes on thousands of route segments, with new efforts arriving constantly and everyone wanting to know their position.
What is being asked
Design a leaderboard service for a fitness app: every recorded activity may contain efforts on many segments; each segment has a ranking by time, overall and by filters such as age group or this year.
Requirements
Functional
- Submit an effort for a segment; keep each athlete's best.
- Top N for a segment; an athlete's own rank and the athletes around them.
- Filtered boards: friends, age group, this year.
Non-functional
- Write-heavy at weekends, with millions of efforts.
- Rank lookups in milliseconds.
- Millions of segments, most rarely used.
Design
- An uploaded activity is matched against segments along its route, producing efforts.
- Each effort is stored durably, then the updater compares it with the athlete's best on that segment.
- If it is better, the athlete's score in the segment's sorted set is updated. A sorted set keeps members ordered by score and gives rank, top N and "neighbours of this member" in logarithmic time.
- The API reads from the sorted sets; the database remains the source of truth and can rebuild any board.
Key decisions and trade-offs
- Sorted sets fit the questions asked. A SQL
ORDER BYwith a count of faster rows becomes slow as boards grow; a sorted set answers rank directly. - Filtered boards multiply storage. Materialise the popular ones (overall, this year); compute rare ones, such as a friends board, on demand from each friend's best.
- Hot and cold segments. Keep active segments in memory and load an idle segment's board from the database on first request.
- Ties and corrections. Use a composite score, time then date, to order ties consistently. Removing a flagged effort means recomputing that athlete's best from stored efforts.
Follow-up questions
- How would you detect an effort recorded from a car?
- How do you shard when one segment has five million athletes?
- How would you show "you moved up 12 places this week"?
- What happens if the in-memory store loses data?