Featured article for the topic Design Twitter · picked by the needcode team
Design Twitter: timelines, fan-out and the celebrity problem
Every Twitter design interview turns on one question: when someone posts, do you write the tweet into every follower's timeline straight away, or build each timeline when it's read? This article walks through both, then the hybrid most real systems use.
1. Requirements and scale#
Users post short tweets, follow others and read a home timeline. Reads outnumber writes by roughly 100 to 1, so the timeline read path is where the design lives or dies.
- 500M tweets a day, about 6k writes per second on average
- Home timeline reads at around 600k per second at peak
- A timeline shows the newest 800 tweets
2. Fan-out on write#
flowchart LR
Client --> LB[Load balancer] --> TS[Tweet service]
TS --> Q[(Fan-out queue)] --> W[Fan-out workers] --> C[(Timeline cache)]When a user tweets, workers push the tweet ID into each follower's cached timeline. Reads become a single cache lookup.
def fan_out(tweet_id, author_id):
for follower in followers(author_id):
redis.lpush(f"timeline:{follower}", tweet_id)
redis.ltrim(f"timeline:{follower}", 0, 799)
Interview tip. Say the celebrity problem out loud before the interviewer does: one account with 100M followers turns a single write into 100M.
3. The hybrid approach#
Fan out on write for normal accounts, and merge celebrity tweets in at read time. Pick the cutoff (say 10k followers) from fan-out queue lag, not a guess.
| Approach | Write cost | Read cost | Good for |
|---|---|---|---|
| Fan-out on write | O(followers) | O(1) | Most accounts |
| Fan-out on read | O(1) | O(following) | Celebrities |
| Hybrid | Bounded | Small merge | Real systems |
4. Storage#
Tweets live in a sharded store keyed by tweet ID (Snowflake IDs sort by time). The timeline cache only holds IDs, so a cache miss rebuilds from the follow graph and the tweet store.
Comments