advanced 2 min answer

A contest platform must update leaderboards for millions of participants as scoring events arrive. What is the architecture, and what must not be attempted?

dream11leaderboardfanoutprecomputationranking
Show the full answer Hide the answer

What must not be attempted

Recomputing a full ranking on every scoring event. With millions of participants and frequent events, a sort per event is impossible, and it is the naive design.

Nor pushing every update to every viewer. One scoring event changing many participants' ranks, fanned out individually, is a write-amplification problem that no amount of capacity solves.

The architecture

  • Incremental score updates, applied per participant as events arrive. Cheap, and parallelises by participant.
  • Ranking computed periodically rather than per event — every few seconds — with the ranking treated as a materialised view that is deliberately slightly stale. Nobody can perceive a two-second-old rank, and the cost difference is enormous.
  • Approximate rank for the long tail, exact rank for the top. A participant in the top hundred needs an exact position; one ranked around two hundred thousand needs a percentile or a bucket. Serving both with the same precision is paying for accuracy nobody consumes.
  • Fan-out on read, not on write. Viewers poll or subscribe for their own position and the top slice, which is a bounded read rather than an unbounded write amplification.
  • Precomputed slices for the common views: the top N, the viewer's neighbourhood, their friends. Computed once per interval and served from cache.

The load shape that dominates the design

Traffic concentrates in the minutes around a scoring event and at the contest's conclusion, with a near-vertical ramp. Autoscaling cannot respond to it — instances take a minute or more to become useful and the deadline is shorter than that — so the answer is pre-provisioned capacity for the known peak plus shedding for the excess.

And the shedding priority is explicit: contest entry and the wallet debit are never shed; leaderboard views degrade to a cached snapshot with a visible timestamp first.

The consistency boundary

The score is eventually consistent and the wallet is not. Prize allocation at the contest's conclusion is the transition point, and it must read from the authoritative settled scores rather than from the live projection — because a prize awarded from a provisional ranking is a financial error, not a display bug.