Interview conceptSystem Design

Leaderboards And Real-Time Ranking

Asked of: Software Engineer

Last updated

Landscape editorial architecture infographic showing client → API → durable store + event log → worker → Redis ZSET shards → merger → read API, with callouts for query patterns, tie-breaking, sharding tradeoffs and reconciliation.

What's being tested

Interviewers are probing whether you can design a low-latency ranked data system under frequent updates, large fanout reads, and correctness constraints. Strong answers combine data structures like heaps, balanced trees, and skip lists with distributed-system choices around sharding, caching, consistency, and failure recovery. Meta cares because ranking-like systems appear in feeds, games, payments, creator dashboards, messaging metadata, and abuse systems where users expect fast, fresh, and explainable ordering. The interviewer is not looking for one magic database; they want to see you identify query patterns, pick the right ranking representation, and reason through scale, ties, time windows, and real-time updates.

Core knowledge

  • Leaderboard query patterns determine the design. Top-NN queries, “rank of user,” “users around me,” percentile buckets, and time-windowed rankings need different indexes. A system optimized only for GET top 100 may fail badly on GET rank(user_id) at scale.

  • Sorted sets are the canonical single-node structure. Redis ZSET uses a hash table plus skip list, supporting ZADD, ZREVRANGE, and ZREVRANK in roughly O(logN)O(\log N) updates and O(logN+K)O(\log N + K) range reads. This is excellent up to millions of members per shard.

  • Tie-breaking must be explicit and stable. Common ordering is (score DESC, updated_at ASC, user_id ASC) or (amount DESC, user_id ASC). If two users have equal scores and the API does not define ties, pagination becomes inconsistent and users may see rank flicker.

  • Write path usually separates durable events from serving state. A score update can be written to a durable store like MySQL, Postgres, Cassandra, or DynamoDB, then applied to a serving index such as Redis. For real-time systems, the serving index is optimized for reads, not treated as the only source of truth.

  • Read path should match latency goals. For a game leaderboard with p99 < 100ms, serve top-NN and neighbor queries from memory-backed indexes. Durable stores are better for history, reconciliation, and rebuilding, not for every rank query.

  • Sharding rankings is hard because rank is global. Sharding by user_id gives balanced writes but makes global top-NN require merging per-shard top lists. Sharding by score range helps range queries but creates hot shards near the top. A common approach is per-shard top-KK plus a merger service for global results.

  • Top-NN can be maintained cheaply with a min-heap. For streaming “maintain top 100 payers,” keep a hash map payer_id -> total and a heap ordered by (total, tie_breaker). Updates are O(logK)O(\log K) with lazy deletion or indexed heaps; full exact ranking still requires an ordered structure over all users.

  • Rank computation can be exact or approximate. Exact rank requires knowing how many users have greater score: rank(u)=1+#{v:scorev>scoreu}\text{rank}(u)=1+\#\{v: score_v > score_u\}. Approximate rank can use histograms, quantile sketches, or bucketed score ranges when exact neighbor ordering is unnecessary.

  • Time windows multiply storage requirements. “Daily,” “weekly,” and “all-time” leaderboards are often separate indexes: game_id:daily:2026-05-23, game_id:weekly:2026-W21, and game_id:alltime. Sliding windows are harder than calendar windows because old events must expire or be subtracted.

  • Idempotency matters for score updates. Retried client requests, duplicate transaction events, or replayed scheduled withdrawals can double-count unless each mutation has an idempotency key such as event_id. Store applied event IDs or make updates derived from authoritative totals instead of blindly incrementing.

  • Consistency tradeoffs should be named. Many leaderboards accept eventual consistency of a few seconds, but financial rankings or payment totals may require read-after-write correctness. State whether users need immediate rank visibility, monotonic score updates, or merely eventual convergence.

  • Operational recovery requires rebuildability. If Redis loses state or a shard is corrupted, you need to reconstruct rankings from durable score snapshots plus event logs. A good design includes periodic snapshots, replay from a known offset, and comparison jobs to detect divergence.

Worked example

For Design a real-time game leaderboard, a strong candidate first clarifies: “Are rankings per game, per region, or global? What queries are required: top 100, my rank, friends around me, or historical seasons? What are update and read rates, acceptable staleness, and tie rules?” Then they declare reasonable assumptions, for example: 100M players, 1M daily active players per game, score updates during matches, p99 read latency under 100ms, and eventual consistency under 2 seconds acceptable.

The answer can be organized around four pillars: API design, data model, serving architecture, and scaling/failure handling. APIs might include POST /scores, GET /leaderboards/{game_id}/top?limit=100, GET /leaderboards/{game_id}/users/{user_id}/rank, and GET /leaderboards/{game_id}/users/{user_id}/neighbors. The data model stores authoritative player scores in a durable table keyed by (game_id, season_id, user_id) and keeps a serving index in Redis ZSET keyed by (game_id, season_id, region).

For scaling, the candidate should discuss per-partition leaderboards and a fan-in aggregator: each shard maintains top-KK, then a merger computes global top-NN by k-way merge. For rank(user), exact global rank across shards is expensive unless each shard can answer “count scores greater than X,” so the candidate should either propose ordered indexes per shard or state an approximation. A key tradeoff to flag is freshness versus global exactness: exact rank on every write may add cross-shard coordination, while eventual global rank gives much lower latency and higher availability.

A strong close would be: “If I had more time, I’d cover anti-cheat score validation, season rollover, backfills from durable logs, cache warmup after failover, and observability such as update lag, rank-query p99, and shard hotness.”

A second angle

For Maintain top N payers, the same ranking concept appears, but the constraints are more algorithmic and correctness-heavy. Instead of designing a full user-facing leaderboard with neighbor queries, you can focus on maintaining an aggregate payer_id -> cumulative_amount and returning the top NN payers after each transaction. If NN is small, a hash map plus min-heap is better than globally sorting after every payment; if arbitrary ranks are needed, switch to a balanced tree or sorted-set representation. Payment events also raise stricter idempotency and correctness requirements than games: duplicate transaction processing or inconsistent tie-breaking can create visibly wrong financial results.

Common pitfalls

Pitfall: Treating the problem as “just use Redis” without matching data structure to query shape.

Redis ZSET is a strong component, not a complete design. If the interviewer asks for global rank, time windows, durability, or cross-region behavior, you need to explain how the sorted set is populated, partitioned, rebuilt, and reconciled.

Pitfall: Ignoring exactness and tie semantics.

A tempting answer is “sort by score and return the top users,” but real systems need deterministic ordering. Define whether equal scores share a rank, use dense ranking, or use unique positions; then carry that rule through storage, pagination, and API responses.

Pitfall: Over-indexing every possible leaderboard too early.

Maintaining all-time, daily, weekly, regional, friends-only, clan, and percentile leaderboards on every update can explode write amplification. A better answer starts with the core access patterns, precomputes the hot ones, and computes rare views asynchronously or from durable aggregates.

Connections

Interviewers may pivot from leaderboards into caching strategy, event-driven architecture, distributed counters, rate limiting, or pagination consistency. They may also ask for an in-memory version, where the focus shifts to heaps, balanced trees, hash maps, and complexity analysis rather than distributed storage.

Further reading

Featured in interview prep guides

Practice questions

Related concepts

Leaderboards And Real-Time Ranking — Tech Interview Concept | PracHub