HARDSocial & Media

Design Twitter Social Network

Design a microblogging platform where users post tweets, follow others, like, retweet, and view a timeline feed.

Estimated Time: 45 minutes
Solution Overview

Twitter timeline generation: Fanout-on-write for active users, fanout-on-read for celebrities. Tweet storage in distributed database. Redis cache for hot timelines. Real-time updates via WebSockets or long polling.

Search

Elasticsearch with inverted index on tweet text, hashtags. Sharded by time.

Database

tweets

tweet_id, user_id, text, timestamp, likes, retweets

follows

follower_id, followee_id

timeline cache

user_id, tweet_ids[]

Trending

Count hashtag frequency over sliding window. Use approximate counting (Count-Min Sketch).

Tweet Creation
  • User posts tweet
  • Store in Tweets table
  • Fanout to followers timelines (async)
  • Index for search (Elasticsearch)
Timeline Generation

approach

Hybrid fanout

celebrities

On-demand merge (fanout-on-read)

regular users

Pre-compute timeline (fanout-on-write)