Twitter Feed Design

Difficulty: MEDIUMCategory: HLDFrequency: 1Asked On 2025-02-10

Problem Statement

Design a scalable Twitter-like system that allows users to post tweets, follow other users, and view their personalized feed (timeline). The feed should display tweets from people a user follows, ordered by time and/or relevance, and must scale to hundreds of millions of users and billions of tweets.


Functional Requirements


1.User Management
•Sign up, login, manage profile.
•Follow/Unfollow other users.
2.Tweet Management
•Post new tweets (text, image, video).
•Like, retweet, and reply.
•Delete tweets.
3.Feed Generation
•Show personalized timeline (tweets from followed users).
•Support home timeline (friends + trending content).
•Support user timeline (tweets from a specific user).
4.Search & Trends
•Search tweets by hashtags, keywords.
•Generate trending topics.


Non-Functional Requirements


•Scalability: Handle billions of reads/writes per day.
•Low Latency: Feed generation < 200ms for recent tweets.
•High Availability: System should remain available despite failures.
•Consistency vs Performance Trade-off: Tweets may appear slightly delayed in followers’ timelines (eventual consistency acceptable).
•Storage Efficiency: Handle large tweet volumes (text + media).


Design Considerations


•Tweet Storage: Distributed DB (Cassandra, DynamoDB, HBase).
•User Graph: Store follow relationships (adjacency list / graph DB).
•Feed Generation:
•Fan-out on write → Push new tweets to followers’ feeds (fast reads, heavy writes).
•Fan-out on read → Fetch tweets at read time (fast writes, heavy reads).
•Hybrid approach for balance.
•Caching: Redis / Memcached for hot tweets & timelines.
•Media Storage: CDN + Blob storage (S3, GCS).
•Search & Trends: ElasticSearch / Solr + batch jobs for trending hashtags.
•Queueing: Kafka for distributing tweets to follower timelines.


APIs / Interfaces


•POST /tweet → Create a new tweet.
•GET /timeline/home?userId=123 → Get personalized feed.
•GET /timeline/user/{userId} → Get a specific user’s tweets.
•POST /follow/{userId} → Follow another user.
•GET /trending → Get trending topics.


Solution

Comments