Design a real-time leaderboard that ranks millions of players by score, serving top-N and a player's own rank with very low latency under heavy read load.
Design a leaderboard service for a game or app with millions of players. Players' scores update frequently, and the system must answer two queries fast: the global top-N players, and a given player's current rank. Both are read-heavy and latency-sensitive : leaderboards are viewed constantly : and rank queries are expensive to compute from raw score records, so the ranking structure must live in a fast store built for ordered data rather than being computed from the database on every read.
The durable record of scores lives in a database, but serving ranks and top-N directly from it (sorting millions of rows per query) is infeasible at read volume. Instead, an in-memory ordered structure (the kind a fast cache/sorted-set store provides) maintains the live ranking and answers rank/top-N queries, while the database remains the system of record for scores.
Design the architecture emphasizing the fast ranking store fronting the durable score database. Then document the API (submit score, get rank, get top-N), the storage model, and the trade-offs of keeping the ranking store and the database consistent.