advanced 2 min answer

Your social feed precomputes each user's timeline when someone they follow posts. An account with 40 million followers joins. What breaks and how do you fix it?

feedfanouthybridscale
Show the full answer Hide the answer

What the interviewer is testing

Whether you recognise that a strategy correct for the bulk of a distribution can be catastrophic in its tail, and whether you reach the hybrid.

What breaks

Write amplification. One post triggers 40 million writes into individual timeline caches. That cannot be absorbed by adding capacity — it is a single logical action producing tens of millions of physical writes, and if several such accounts post in the same minute the system is generating hundreds of millions of writes for a handful of user actions.

The queue backs up, ordinary users' timelines stop updating because the fan-out workers are saturated by one account, and latency for everyone degrades.

The fix: hybrid fan-out

Exclude high-follower accounts from fan-out. Their posts are stored once and merged at read time when a follower opens their timeline.

Ordinary accounts continue to fan out on write, so the overwhelming majority of reads remain a cheap fetch from a precomputed list. Celebrity posts are read from one place by many people, which is where the cost is lowest for that shape.

This is the approach Twitter publicly described, and it is a general principle: pay the cost where it is cheapest for each part of the distribution.

The complications to name

Two code paths with different latency and consistency characteristics, and a threshold deciding which applies — so accounts near the boundary behave inconsistently.

Merge ordering. The read-time merge must interleave celebrity posts correctly with the precomputed timeline, which requires a consistent ordering key across both sources.

Threshold selection. Too low and too many accounts are read-time, eroding the precomputation benefit. Too high and the fan-out cost remains. It should be derived from the actual follower distribution rather than chosen.

What a strong answer adds

The generalisation: this is materialised views with an escape hatch for the pathological tail, and the same shape recurs in search indexes, analytical rollups, caches with a bypass for very large objects, and notification systems.

The design question for any precomputation: what is the distribution of the fan-out? If uniform, one strategy works. If heavily skewed — and most real distributions are — a single strategy is wrong at one end.

Common weak answers

Scaling the fan-out workers, which does not address a single action producing 40 million writes. Switching entirely to read-time merge, which makes every ordinary timeline expensive.