System Design Problem

Design a Timeline and Tweet Service (Twitter)

Commonly Asked By:TwitterMetaLinkedInUber

  • Post a tweet: Text (280 chars), images, videos, links
  • User timeline: View a user's own tweets (profile page)
  • Home timeline (feed): Aggregated tweets from followed users, ranked
  • Follow / Unfollow users
  • Like, Retweet, Reply, Quote Tweet
  • Search tweets by keyword, hashtag, user
  • Trending topics: Real-time trending hashtags and topics
  • Notifications: Mentions, likes, retweets, new followers
Loading...

Hybrid Fan-Out Strategy (Same as News Feed: Critical for Twitter)

Twitter's core architectural challenge:

User TypeFollowersStrategyReason
Normal (99.9%)< 10KFan-out on WritePre-compute timeline at write time; reads are instant
Celebrity (0.1%)> 10KFan-out on ReadWriting to 50M timelines is too slow

Implementation

  1. User A tweets → Tweet Service publishes to Kafka
  2. Fan-out Service consumes event, checks follower count
  3. If < 10K followers → fetch follower list → write tweet_id to each follower's Redis timeline
  4. If ≥ 10K followers → skip fan-out; store tweet in author's timeline only
  5. When User B reads home timeline:
    • Fetch pre-computed timeline from Redis (fan-out-on-write results)
    • Separately fetch latest tweets from any celebrities User B follows (fan-out-on-read)
    • Merge and rank