Scrivio Interview library
DesignIntermediate6 min read · updated 13 November 2026

A game has 80 million players with up to 500 friends each. Players open the friends leaderboard far more often than the global one, and today each view reads every friend's score from the database, taking 600 ms at p99.

How would you design the friends leaderboard so it opens in under 50 ms, and how do score changes reach it?

We assemble the friends board on read from a fast store: each player's current score per board sits in a key-value cache, a view fetches up to 500 friends' scores in one batched multi-get and sorts them in memory, which takes a few milliseconds. Precomputing a sorted board per player would cost up to 500 writes per score change, so read-time assembly is cheaper for this shape. The assembled board is cached per player for a few seconds, the friend list is cached and updated from friendship events, and a global percentile comes from a per-board score histogram.

Why this question is asked

Friends leaderboards are what players actually look at: being 40,000th in the world means little, being third among your friends is a reason to play another match. That makes this the most-read board in the game, and today it is the slowest. Interviewers ask this because it is a clean example of the fan-out choice, doing work when data is written or when it is read, and the right answer depends on the numbers. In this answer, we will see why the current query is slow, compare precomputing with assembling on read, build the read path, add caching for the board and the friend list, and answer the global percentile question with a histogram.

1Why Each View Takes 600 ms

Today a view reads every friend's score from the database. With up to 500 friends, that is up to 500 rows scattered across a large table, often across shards, each needing an index lookup and possibly a disk read. Even if most are fast, the slowest of 500 lookups decides the response time, so p99 is dominated by the occasional slow read. On top of that, the friend list itself is fetched from the social database first.

So the problem is not sorting; sorting 500 numbers takes microseconds. The problem is fetching 500 small values from a store that was built for durability, not for this access pattern.

2Precompute on Write or Assemble on Read?

There are two classic designs. Fan-out on write precomputes a sorted friends board for every player and, whenever a score changes, updates the boards of all that player's friends. Fan-out on read keeps one score per player and builds the friends board when someone opens it.

Let us put numbers on fan-out on write. Suppose 3,000 score changes per second at peak and an average of 150 friends, with some at 500. That is about 450,000 board updates per second, and every player's board must be stored: 80 million boards of up to 500 entries. Each score change also has to find all 500 boards that contain the player. Fan-out on read, by contrast, costs one multi-get of up to 500 keys per view, and nothing extra per score change.

Two shapes for a friends board
Precompute on writeAssemble on read
Work per score changeup to 500 board updates1 key update
Work per view1 read of a sorted board1 multi-get of up to 500 keys + sort
Storage80M boards x up to 500 entries80M scores per board
Freshnessinstant once fan-out completesinstant for scores, seconds with caching
Hot spotspopular players fan out widelynone beyond normal reads

Notice that precomputing is attractive when views vastly outnumber writes and lists are long. Here lists are capped at 500 and a multi-get of 500 keys from memory is cheap, so assembling on read wins on cost and simplicity.

3Building the Board on Read

Each player's current score per board lives in a key-value cache such as Redis, with keys like score:season-12:4411. A view fetches the friend list, issues one batched multi-get for all friends' scores, and sorts in memory.

Java
List<Long> friends = friendCache.friendsOf(playerId); // up to 500 ids, cached
List<String> keys = friends.stream().map(f -> "score:" + board + ":" + f).toList();
List<KeyValue<String, String>> values = redis.mget(keys.toArray(String[]::new)); // one batched call
List<Entry> board = zip(friends, values).stream()
.filter(e -> e.score() != null) // friends who never played this board
.sorted(comparing(Entry::score).reversed().thenComparing(Entry::achievedAt))
.toList();
// 500 keys: about 2 to 4 ms in Redis, sorting about 0.05 ms

In Redis Cluster those 500 keys live in different slots, so a plain MGET would fail with a cross-slot error. Cluster-aware clients such as Lettuce split a multi-key call by node and send the parts in parallel, so the cost is a few round trips in parallel, not 500. Notice also the tie-break by achievement time inside the sort, so two friends with equal scores always appear in the same order.

4Caching the Board and the Friend List

Players open their friends board repeatedly within a session, so we cache the assembled board per player for a few seconds, for example 10. When the player's own score changes, we drop that cache entry so they always see their own new score immediately. A friend's change may appear a few seconds late, which nobody notices.

The friend list is cached too, so a view never touches the social database. The cache is updated from friend-added and friend-removed events published by the social service, with a long expiry as a safety net if an event is missed.

Step throughOpening the friends board
ClientBoard APIBoard cacheFriend cacheScore store
GET friends board
cached board for player?
miss
friend ids
up to 500 ids
batched multi-get of scores
scores
sort, keep 10 s
board, about 10 ms

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

A cache hit skips everything after the first lookup.

5A Global Percentile Without an Exact Rank

Players also like 'you are in the top 3 percent'. An exact global rank among 80 million players needs a sorted structure of all of them, but a percentile does not. We keep a histogram per board: the number of players in each score bucket, for example 1,000 buckets, updated from the score stream (one bucket down, one bucket up on each change). The percentile is the count of players in higher buckets divided by the total.

Java
long[] buckets = histogram(board); // buckets[i] = players with score in bucket i
int mine = bucketOf(18250); // e.g. bucket 912
long above = 0, total = 0;
for (int i = 0; i < buckets.length; i++) { total += buckets[i]; if (i > mine) above += buckets[i]; }
double topPercent = 100.0 * above / total; // 2.7 -> 'top 3 percent'

This answers in constant time, about 1,000 additions, regardless of player count, and the error is at most the size of one bucket, which is invisible when rounded to whole percent. Notice that we could precompute the cumulative counts every few seconds so that even the loop disappears.

6Follow-up questions

When would precomputing friends boards be the better choice?

When friend lists are long but views are extremely frequent and reads must be a single lookup, or when the board is expensive to assemble, for example ranking across many attributes. Social feeds precompute for normal users for this reason. Even then, systems usually skip fan-out for very popular accounts and merge them in at read time.

What latency budget gets us under 50 ms?

Roughly 2 ms for the friend list from cache, 2 to 4 ms for the batched multi-get, under 1 ms for sorting and serialising, plus network and service overhead of 10 to 20 ms. That leaves comfortable margin, and a board cache hit is a few milliseconds total.

How do we keep the score cache consistent with the record of truth?

The score service writes the durable record first and then updates the cache key, and a periodic job compares a sample of keys with the database. If the cache is lost, it is refilled from the database, because it holds only current scores, not history.

7Conclusion

In this answer, we saw that the friends board was slow because each view fetched up to 500 scores from a durable database. We compared precomputing per player, which costs up to 500 writes per score change, with assembling on read, and chose a batched multi-get from a key-value cache and an in-memory sort that take a few milliseconds. We cached the assembled board for a few seconds, invalidated it on the player's own change, cached friend lists from friendship events, and answered global percentiles from a per-board histogram.

What to say in the interviewAbout 90 seconds aloud

Open with

The friends board is slow because each view reads up to 500 scores from the database, and the slowest of 500 lookups sets the p99. Sorting 500 numbers is trivial; fetching them is the cost.

Then, if they want more

I would compare the two shapes. Precomputing a sorted friends board for every player means each score change fans out to every friend's board, up to 500 writes per update, hundreds of thousands per second at peak, and 80 million stored boards. Assembling on read means one batched multi-get of up to 500 keys per view.

With lists capped at 500, assembling on read is clearly cheaper. So I keep each player's current score per board in Redis, fetch the friend list from a cache, do one multi-get, which a cluster-aware client splits by node, and sort in memory with a tie-break on achievement time. That is a few milliseconds.

I cache the assembled board per player for around ten seconds and drop it when the player's own score changes, so they always see their own update while friends may lag a few seconds. The friend list is cached and kept current from friend-added and friend-removed events, so a view never touches the social database.

For 'top 3 percent' globally, I keep a score histogram per board, updated from the score stream, which gives a percentile in constant time without an exact rank.

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 "How would you design the friends leaderboard so it opens in under 50 ms, and how do...": https://scrivio-interview-library.netlify.app/architecture/system-design-problems/e5a30318bf/