All system design challenges
#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.

Asked at Strava

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

Activity uploadSegment matcherEffort streamLeaderboard updaterEfforts DBRedis sorted sets: oneper segment and filterLeaderboard API
Boxes are services; rounded shapes are data stores and queues.
  1. An uploaded activity is matched against segments along its route, producing efforts.
  2. Each effort is stored durably, then the updater compares it with the athlete's best on that segment.
  3. 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.
  4. 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 BY with 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?
Log in to tick off the challenges you have worked through. Log in