Requirements
Functional Requirements:
- Allow users to tweet messages up to 140 characters.
- Enable users to follow other users.
- Allow users to like tweets from other users.
- Display tweets from followed users in the home feed.
- Show top K popular tweets in the home feed based on likes and followers.
Non-Functional Requirements:
- highly available for users to see posts
- Able to scale among regions
- Durable data
Capacity Estimation
Estimate the scale of the system. Consider daily active users, read/write ratio, storage requirements, bandwidth, and any relevant QPS calculations...
API Design
REST APIs
POST /tweet
-- messages
-- user_id
-- topic
-- timestamp
POST /follow/{user_id}
POST /like/{tweet_id}
GET /tweet/{user_id} # get tweet from a user
GET /toptweets/{k} # get top k tweets
High-Level Design
The request starts from the client and goes to API gateway, then it goes to tweet service (for APIs like post a tweet, add likes to a tweet, get all tweets from a user) or user service (for APIs about the user, like follow a user)
Database Design
MongoDB or other documented based database
Tweet table:
tweet id (primary key)
user id
topic
message
timestamp
likes amount
user likes the tweet []
User table
user id (primary key)
followers []
Detailed Component Design
- CDN can be added to store static files like pictures, profile photo, videos and etc.
- Redis can be added to store top-k tweets
- With high scalability, both tweet service and user service can be horizontally partitioned with replicas; Add sharding to database with shard id as tweet id for tweet table, shard is as user id for user table.
- Add message queue like Kafka to handle concurrent or multiple requests,