← Lessons

quiz vs the machine

Silver1080

System Design

The Twitter Timeline at Scale

Why Twitter precomputes your home timeline instead of querying it live.

4 min read · intro · beat Silver to climb

The problem

Your home timeline is the merge of tweets from everyone you follow, newest first. Computing this on every load means scanning thousands of authors — far too slow at read time when reads vastly outnumber writes.

Fan-out on write

Twitter flips the cost to write time. When you tweet, the system fans out the tweet id into the precomputed timeline cache of each follower. Reading a timeline then becomes a single cheap lookup.

  • Fan-out on write pushes each tweet into follower inboxes
  • Fan-out on read merges author timelines only when you open the app
  • Most users get fan-out on write for fast reads

The celebrity problem

A user with millions of followers would trigger millions of writes per tweet. So Twitter uses a hybrid: celebrity tweets are fetched at read time and merged into your precomputed list, avoiding write amplification.

The lesson is that read and write costs trade off, and you place work where traffic is cheapest.

Key idea

Precompute timelines on write for the common case, but fall back to read time merging for celebrities to dodge write amplification.

Check yourself

Answer to earn rating on the learn ladder.

1. Why does Twitter prefer fan-out on write for most users?

2. How does the hybrid model handle celebrity accounts?