Design Twitter
Difficulty: MEDIUMCategory: HLDFrequency: 1Asked On 2025-01-18
Problem Statement
Design a microblogging social media platform (like Twitter) where users can post tweets, follow others, and view a personalized feed. The system should handle high write and read throughput, ensure low-latency feed retrieval, and scale to millions of users.
Functional Requirements (FR)
User Management
•Register/login users.
•Follow/unfollow other users.
•Maintain user profiles (name, handle, bio, profile picture).
Tweet Management
•Post, edit, and delete tweets (text, images, videos).
•Like, retweet, and reply to tweets.
•Support hashtags, mentions, and media attachments.
Timeline / Feed
•Generate a home feed with tweets from followed users.
•Support pagination and infinite scrolling.
•Optionally, show trending tweets.
Search & Discovery
•Search tweets by keyword, hashtag, or user.
•Explore trending topics.
1.Notifications
•Notify users about likes, retweets, mentions, or new followers.
2.Analytics (Optional)
•Track tweet engagement, impressions, and user activity.
Non-Functional Requirements (NFR)
•Scalability: Handle millions of tweets per day and millions of active users.
•Low Latency: Feed retrieval should be near real-time.
•High Availability: System should remain available even under high load.
•Consistency: Ensure tweets and likes are not lost and feed ordering is correct.
•Fault Tolerance: Handle failures of individual services gracefully.
Suggested Components / System Architecture
Client Layer
•Web and mobile apps that allow posting, viewing, and interacting with tweets.
API Gateway
•Entry point for client requests; handles authentication, rate-limiting, and routing.
User Service
•Manages user profiles, followers/following relationships.
Tweet Service
•Handles tweet creation, deletion, updates, likes, retweets, and replies.
Feed Service
•Generates home feeds for users:
•Push model: precompute feeds for fast retrieval.
•Pull model: generate feed on request using recent tweets from followed users.
Search Service
•Index tweets for keyword/hashtag search.
•Support trending topics computation.
Notification Service
•Sends alerts for likes, retweets, mentions, and follows.
Database Layer
•Relational DB: Store user profiles, relationships, and metadata.
•NoSQL DB: Store tweets, likes, retweets, and feed data.
•Cache (Redis/Memcached): For fast access to feed, trending topics, and user sessions.
Message Queue / Event Bus
•For asynchronous operations such as fan-out tweets, notifications, and analytics.
Data Flow (High-Level)
1.User posts a tweet → Tweet Service stores it in DB → triggers Feed Service to update followers’ feeds.
2.User fetches home feed → Feed Service retrieves tweets from cache/DB → returns sorted feed.
3.User likes/retweets → updates stored counters asynchronously via Message Queue.
4.Search queries → Search Service indexes and retrieves matching tweets.
5.Notifications → delivered via Notification Service asynchronously.
Key Design Considerations
•Fan-out vs Fan-in: Precompute feeds (push) or generate on-the-fly (pull) based on scale.
•Sharding & Partitioning: Distribute tweets and users across multiple databases.
•Caching: Use Redis for fast feed access and trending computation.
•Rate Limiting: Prevent abuse of posting, liking, or following.
•Consistency: Eventual consistency is acceptable for feed updates; strong consistency for likes and retweets counters.
•Media Storage: Use CDN/S3 for storing images/videos attached to tweets.