System design · Fan-out and the celebrity problem
How to design Twitter's home timeline
Precompute each user's home timeline by fanning out every tweet to followers' cached timelines on write, but skip that for accounts with millions of followers and merge their tweets in at read time. At 200 million daily users that is about 115,000 timeline reads and 460,000 timeline inserts a second.
Updated · 5 min read
A teaching design inspired by Twitter's published engineering, not Twitter's real architecture. Read the source.
The short version
- Reads outnumber posts about 50 to 1, so precompute each user's timeline as a list of tweet IDs in Redis.
- Fan-out on write is the default: pay once per tweet so thousands of reads are cheap.
- A 50-million-follower account breaks fan-out on write. Skip it for such accounts and merge their tweets at read time.
- A queue drains fan-out, so bursts become backlog instead of errors. Eventual consistency makes this possible.
- Cap timelines at 800 IDs, skip inactive users, and paginate with a cursor.
Run it: click the fixes, watch the numbers
Scroll the diagram to see every component at a readable size.
- p99 latency
- 614ms
- Errors
- 39.0%
- Cost
- $1,101/mo
The database's 3 read replicas can serve 3,000 reads/s but are receiving 4,950. 39% of requests fail. A cache in front of it would absorb most of those reads.
Embed this simulation in your blog, docs or course
Free to embed. Paste this HTML anywhere that accepts an iframe.
Clarifying questions to ask first
- Ranked or chronological? (Assume newest first; ranking, ads, search and notifications are out of scope.)
- How many users, and how active? (Assume 200 million daily users, each posting about one tweet and loading the timeline about 50 times a day.)
- How many followers per account? (Assume 200 on average, with a few accounts in the tens of millions.)
- How fresh must the timeline be? (Assume a new tweet may take a few seconds to appear.)
Numbers that shape the design
| Quantity | Assumption | Result |
|---|---|---|
| Daily active users | 200 million | - |
| Tweets posted | 1 tweet per user per day | ≈ 2,300 tweets/s average |
| Timeline reads | 50 timeline loads per user per day | ≈ 115,000 reads/s average |
| Average fan-out | 200 followers per tweet | ≈ 460,000 timeline inserts/s |
| Timeline cache | 800 tweet IDs × 8 bytes per active user | ≈ 1.3 TB of memory |
Reads must be fast (a few hundred milliseconds end to end), and fan-out multiplies every post by the follower count.
API
POST /api/tweets { "text": "..." } → 201 { "id": "1850..." }
POST /api/follows { "followee_id": 42 } → 204
GET /api/timeline?cursor=… → 200 { "tweets": [...], "next_cursor": "…" }Paginate with a cursor (the last tweet ID seen), not a page number. New tweets arrive constantly, and offsets would skip or repeat them.
Data model
- tweets: tweet_id (time-sortable, Snowflake-style), author_id, text, created_at, in a wide-column or sharded relational store, sharded by tweet_id.
- follows: follower_id, followee_id, indexed both ways. Readers need who I follow; fan-out workers need who follows me.
- home timelines: per user, the most recent tweet IDs from accounts they follow, in Redis. IDs only, never full tweets.
Fan-out on write vs fan-out on read
| Fan-out on write (push) | Fan-out on read (pull) | |
|---|---|---|
| On post | Insert the tweet ID into every follower's cached timeline | Write the tweet once |
| On read | Read one precomputed list - fast | Fetch recent tweets from every followee and merge - slow |
| Cost | Writes multiply by follower count | Reads multiply by followee count |
| Breaks when | An account has millions of followers | Users follow thousands of accounts, or reads spike |
Reads dominate, so fan-out on write is the right default.
The celebrity problem
When an account with 50 million followers posts, one tweet becomes 50 million timeline inserts. Fan-out workers fall behind, everyone else's tweets queue behind the celebrity's, and freshness collapses.
The read side has its own version: millions refresh at once. In the simulation above, a 3× read spike overwhelms the API tier even though the timeline cache protects the database. Fix one bottleneck and it moves.
The hybrid design
Twitter described this in its 2013 "Timelines at Scale" talk: home timelines are precomputed lists of tweet IDs in Redis, built by fan-out on write, while tweets from very high-follower accounts are merged in at read time.
- Normal accounts fan out on write through a queue drained by workers.
- High-follower accounts skip fan-out; their recent tweets are merged in at read time.
- Inactive users get no maintained timeline; rebuild it on their next visit.
- Cap each cached timeline (say 800 IDs) and fetch older history from storage.
Reading a timeline
Read the user's ID list from the cache, merge in recent tweets from the high-follower accounts they follow, then hydrate the IDs with one batch lookup, mostly served from a tweet cache. Every step on the hot path is an in-memory read.
Common traps
- Copying full tweets into every cached timeline. Content and edits get duplicated across followers; store IDs and hydrate.
- Indexing follows in one direction only. Fan-out workers need an author's followers too.
- Fetching each tweet separately after reading the IDs. Hydrate the page in one batch.
- Keeping every historical ID in Redis. The estimate assumes a cap of 800; enforce it.
Follow-up questions interviewers ask
- "How do you delete a tweet already fanned out?" Mark it deleted in the tweet store. Hydration skips it, so stale IDs never show.
- "What happens on unfollow?" Remove that account's IDs from the timeline in the background. A few seconds of staleness is fine.
- "Where is the high-follower line?" Where fanning out costs more than merging at read time. Tune it from measurements.
- "What if a Redis shard holding timelines is lost?" Tweets and follows are still in storage, so affected timelines are rebuilt on next read. Slower, but nothing is lost.
Frequently asked questions
What is the celebrity problem in system design?
+
With fan-out on write, an account with millions of followers turns one post into millions of writes, delaying everyone else's posts. The fix is a hybrid: fan out for most accounts and merge high-follower accounts' posts at read time.
Is fan-out on write or fan-out on read better?
+
Fan-out on write makes reads fast at the cost of expensive writes, which suits read-heavy feeds. Fan-out on read makes writes cheap and reads slow. Large feeds use a hybrid keyed on follower count.
How do you paginate a news feed?
+
With a cursor, usually the ID of the last item returned. New items arrive continuously, so offset pagination would skip or repeat items.
Does a news feed need strong consistency?
+
No. A new post can take a few seconds to reach followers, and accepting that is what lets timelines be precomputed asynchronously.