Total users: 10 millions
DAU: 2 millions
Tweets per day:3
Read/Write ratio: 20:1
Average tweet size:
Daily Tweets: 3 * 2millions = 6 millions
Daily Requests: 6 millions * 21 = 126 millions
QPS: 126 millions / 24 / 60 = 1458 query/second
Daily storage (text): 6 million tweets * 280 bytes/tweet = 1.68 GB
Daily storage (images): 6 million tweets * 20% * 200 KB/image = 240 GB
Daily storage (videos): 6 million tweets * 5% * 1 MB/video = 300 GB
Total daily storage: 1.68 GB + 240 GB + 300 GB = ~541.68 GB
Yearly storage: 541.68 GB/day * 365 days = ~197.5 TB
Average request size (estimate):
Weighted average request size: (0.75 * 300 bytes) + (0.2 * 200.3 KB) + (0.05 * 1 MB) = ~90.3 KB
Peak bandwidth: 5833 RPS * 90.3 KB/request = ~526 MB/s
Users
GET /users/{user_id}: Retrieve a user's profile.POST /users: Create a new user account.PUT /users/{user_id}: Update a user's profile.DELETE /users/{user_id}: Delete a user account.Tweets
POST /tweets: Create a new tweet.GET /tweets/{tweet_id}: Retrieve a tweet.DELETE /tweets/{tweet_id}: Delete a tweet (only by the author).GET /users/{user_id}/tweets: Retrieve tweets posted by a specific user.GET /me/tweets: Retrieve tweets forUsers:
user_id (INT, primary key, auto-incrementing)username (VARCHAR, unique, not null)email (VARCHAR, unique, not null)password_hash (VARCHAR, not null)bio (TEXT)profile_picture_url (VARCHAR)created_at (TIMESTAMP)updated_at (TIMESTAMP)Tweets:
tweet_id (INT, primary key, auto-incrementing)user_id (INT, foreign key referencing Users)content (TEXT, not null)media_urls (JSON - to store an array of image/video URLs)created_at (TIMESTAMP)Relationships:
follower_id (INT, foreign key referencing Users)following_id (INT, foreign key referencing Users)created_at (TIMESTAMP)follower_id and following_id)Clients:
Load Balancers:
API Gateway:
Microservices:
Data Stores:
Message Queue (e.g., Kafka):
Content Delivery Network (CDN):
Client Initiates Request: A user composes a new tweet with text and an optional image in the mobile app and presses the "Tweet" button.
Load Balancer Distributes: The mobile app sends the tweet request to the Load Balancer. The Load Balancer distributes the incoming request to one of the available API Gateway instances.
API Gateway Authentication and Routing: The API Gateway authenticates the user's request using their authentication token or credentials. Once authenticated, the API Gateway routes the request to the Tweet Service.
Tweet Service Processes Request: The Tweet Service receives the request and processes the tweet data. It stores the tweet content (text and image URL) in the NoSQL database (Cassandra) and relevant metadata (user ID, timestamp, etc.) in the relational database (MySQL).
Asynchronous Processing with Message Queue: The Tweet Service sends a message to the Message Queue (Kafka) to notify the system about the new tweet. This message is placed on a topic like "New Tweets."
Feed Service Updates Feeds: The Feed Service, subscribed to the "New Tweets" topic, receives the message about the new tweet. It retrieves the tweet data from the database and updates the feeds of the users who follow the author of the tweet. It may also update the cached feed data in Redis.
Notification Service (Optional): The Notification Service might also consume the message from the queue to send out notifications to followers, informing them about the new tweet.
Client Receives Response: Once the Tweet Service has successfully stored the tweet, it sends a success response back to the client via the API Gateway. The user sees their tweet posted on their profile and their followers' feeds.
Tweet Service
Functionality:
Scalability:
Data Structures and Algorithms:
Relational Database (MySQL) vs. NoSQL Database (for all data)
Message Queue (Kafka) vs. Synchronous Communication
1. Traffic Overload
Database Failures
Database failures can disrupt the availability and consistency of your service. Here's a breakdown of common failure scenarios:
Replication:
Sharding:
Enhanced Feed Personalization:
Real-time Features: