Scrivio Interview library
DesignFoundations6 min read · updated 13 November 2026

A game leaderboard ranks players by score so they can see the top players and their own position. Scores come from matches that must be trusted, boards may be global, regional or per season, and ranks should update within seconds of a match ending.

Design the API and data flow of a leaderboard service from the end of a match to an updated rank, including where the record of truth lives and how scores are validated.

Only the game server submits results, with POST /scores carrying the match id, player id, score and board ids, signed, and the match id makes the call idempotent. Scores are validated against the match and checked for plausibility, written to a durable scores table or event log that is the record of truth, and then applied to an in-memory ranking structure such as a Redis sorted set that can always be rebuilt. Reads serve the top list from a cache refreshed every second or two and a player's rank with neighbours, ties break by earliest achievement, and board definitions are data.

Why this question is asked

A leaderboard looks like a sorted list, and the sorting is the easy part. The hard parts are deciding which scores to trust, deciding where the real data lives, and serving millions of players who all look at the same top 100 at once. Interviewers ask it because it combines API design, data ownership, caching and a little anti-cheat thinking in a small problem. In this answer, we will set the requirements and numbers, design the write path from match end to rank, validate scores, separate the record of truth from the ranking structure, design the read API, and handle ties and board types.

1Requirements and Rough Numbers

Players finish matches, and within seconds they should see their new rank on one or more boards: global, regional, or for the current season. Suppose 10 million daily players and peaks of 2,000 match results per second, each updating up to three boards. That is 6,000 board updates per second, which is modest. Reads are heavier: every player who opens the game sees the top 100 and their own rank, perhaps 50,000 reads per second at peak.

So writes need correctness more than speed, and reads need a cache more than a bigger database. That shapes the rest of the design.

2The Write Path: Only the Game Server Submits

If the client could submit its own score, the first player who opens the network inspector would post a score of a billion. So only the game server, which ran the match, submits results. It signs each request with a key the leaderboard service trusts, and it includes the match id.

Java
POST /scores
X-Signature: hmac-sha256=... // signed by the game server
{
"matchId": "m-88213",
"playerId": 4411,
"score": 18250,
"boards": ["global", "eu", "season-12"]
}
// first call: 202 Accepted
// same matchId and playerId again: 200, already recorded, nothing changes
Step throughFrom match end to updated rank
Game serverScore APIDatabaseRanking store
POST /scores (signed, matchId)
verify signature, validate
insert score event, unique matchId+player
committed
ZADD board GT player score
202 Accepted

6 messages in order. Play them, or step through them one at a time.

The durable write happens before the ranking structure changes.

Notice why the match id matters. Game servers retry when a response times out, and without a key we would count the same match twice. A unique constraint on (match_id, player_id) in the scores store makes the submission idempotent: a retry finds the existing row and returns success without changing anything.

3Validating Scores Before Ranking

A signed request proves who sent the score, not that the score is sensible. So before ranking, we check that the match exists, that it ended, and that this player was in it, using the match service's records. Then we check plausibility: a 3-minute match in which the best players score around 20,000 should not produce 900,000.

Implausible scores are not rejected silently and not ranked at once. They are held for review, stored with a flag, so a real exceptional performance can still be confirmed later, while an exploit does not reach the top of the global board for everyone to see.

4The Record of Truth and the Ranking Structure

The record of truth is a durable store. Either a scores table holding each player's best score per board, with the time it was achieved and the match it came from, or an append-only log of score events from which best scores are derived. The ranking structure is separate and derived: a Redis sorted set per board, where each member is a player and its score decides its position, gives rank lookups in logarithmic time.

Java
-- record of truth (PostgreSQL)
CREATE TABLE board_scores (
board_id text, player_id bigint, best_score int NOT NULL,
achieved_at timestamptz NOT NULL, match_id text NOT NULL,
PRIMARY KEY (board_id, player_id)
);
# derived ranking (Redis)
ZADD board:season-12 GT 18250 4411 # GT: only replaces a lower score
ZREVRANK board:season-12 4411 # (integer) 1532 -> rank 1,533
ZREVRANGE board:season-12 0 99 WITHSCORES # top 100

The reason for the split is recovery. Redis is fast but its replication is asynchronous, so a failover can lose recent updates. Because every score is committed to the database first, we can always rebuild a sorted set by reading the table and running ZADD for each row. The ranking structure is a cache with a shape, not the place where scores live.

5The Read API and Caching

Two calls cover most screens. GET /boards/{id}/top?limit=100 returns the top players, and GET /boards/{id}/players/{pid}/rank returns the player's rank plus, say, five players above and below, which is what makes a board motivating.

The top list is identical for every viewer, so we cache it for one or two seconds, in the service's memory or a CDN. With 50,000 reads per second, a one-second cache turns them into one Redis call per second per board. The personal rank cannot be shared, but ZREVRANK followed by a ZREVRANGE around that rank is two fast Redis calls.

6Ties and Board Definitions

If two players both score 18,250, who ranks higher? Without a rule, the order flips between requests and players notice. We break ties by earliest achievement: whoever got there first ranks higher. In a sorted set we can encode this into the score itself, for example score times 10,000,000 plus (9,999,999 minus the seconds since the season started), which stays well within the range a Redis score represents exactly.

Finally, boards differ in period, region and reset time: a weekly regional board resets every Monday at 00:00 local time, a season board at the season's end. We keep these as board definitions in data, a row per board with its filter and schedule, so adding a new board is configuration rather than a deployment.

7Follow-up questions

Why not compute ranks with a SQL query instead of Redis?

SELECT count(*) FROM board_scores WHERE best_score > ? gives the rank, and with an index it works for small boards. For millions of players it scans a large part of the index on every lookup, and thousands of lookups per second add up. A sorted set answers rank queries in logarithmic time from memory.

What happens when a player is banned for cheating?

We mark their scores as removed in the record of truth, which is the auditable step, and run ZREM on every board's sorted set. Everyone below them moves up by one, and the cached top list refreshes within a second or two.

Should we keep the best score or the latest score?

It depends on the board. Most competitive boards rank by best score in the period, which is why we use ZADD with GT. Boards like a rating ladder rank by current rating, where ZADD without GT replaces the value each time.

8Conclusion

In this answer, we designed a leaderboard where only signed game servers submit results, idempotent by match id, and where scores are validated against the match and checked for plausibility before ranking. The durable scores table or event log is the record of truth, and a Redis sorted set per board is a derived structure we can always rebuild. Reads serve a top list cached for a second or two and a personal rank with neighbours, ties break by earliest achievement, and board definitions live in data.

What to say in the interviewAbout 90 seconds aloud

Open with

I would never let the client submit scores; only the game server that ran the match calls POST /scores with the match id, player id, score and board ids, and it signs the request.

Then, if they want more

A unique key on match id and player makes retries idempotent. Before ranking, I validate that the match exists, that the player was in it, and that the score is plausible for the match length; outliers are stored but held for review instead of going straight onto the board.

The record of truth is durable: a scores table with each player's best score per board, the time it was achieved and the match id, or an append-only score log.

The ranking itself lives in a Redis sorted set per board, updated with ZADD GT after the database commit, so if Redis fails I can always rebuild it from the table.

For reads, I have GET top with a limit, cached for a second or two because everybody reads the same thing, and GET a player's rank with a few neighbours above and below using ZREVRANK and ZREVRANGE.

Ties break by earliest achievement, which I can encode into the sorted set score, and board definitions such as region, period and reset time are rows of data, so a new weekly board is configuration, not code.

About this answer

Last updated
13 November 2026
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the official documentation for the version you use. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "Design the API and data flow of a leaderboard service from the end of a match to an...": https://scrivio-interview-library.netlify.app/architecture/system-design-problems/5ee441cbb1/