List functional requirements for the system (Ask the chat bot for hints if stuck.)...
List non-functional requirements for the system...
1.scalability: system should be able to handle a large number of users, tweets and interactions.
2.high availability: ensure system function is available under the pressure of high traffic volume.
3. stability: ensure service is accessible without frequent issues or down time under the pressure of high concurrency.
Estimate the scale of the system you are going to design...
User Base:
Traffic:
We can calculate the traffic based on the number.
QPS:
Data size :
It is clear that we need a distributed architecture.
Define what APIs are expected from the system...
For tweeting, the api for user to post tweets.
public Result postTweets(UserInfo user, TweetInfo tweet);
public Result postTweets(Long userId, String tweetsText, String location,DateTime date);
For following,
public Result follow(Long userId, Long followedUserId);
public Result unFollow(Long userId, Long followedUserId);
For favorites:
public Result favorites(Long userId, Long tweetId);
public Result Unfavorites(Long userId, Long tweetId);
For feeds:
public Result renderFeeds(Long userId,String location, int pageNo);
Defining the system data model early on will clarify how data will flow among different components of the system. Also you could draw an ER diagram using the diagramming tool to enhance your design...
The tables will include userInfo , tweets , follow relationship
I will use mysql to store userInfo ,tweets, and relationship.
And use Amazon S3 to store picture and videos.
UserInfo Table:
tweets Table:
Follower table:
The ER diagram is on the right;
You should identify enough components that are needed to solve the actual problem from end to end. Also remember to draw a block diagram using the diagramming tool to augment your design. If you are unfamiliar with the tool, you can simply describe your design to the chat bot and ask it to generate a starter diagram for you to modify...
There's some problems I need to take into account;
Usually, there're 2 common model to render the home feed. Pull and Push.
Push is when a user post a tweet, it is immediately pushed to the feeds of all their followers.
Features:
Pull is when a user refresh the homepage, then tweets are fetched in real time.
Features:
I prefer to the Pull Model here, and then I'll dive deep at the stage of trade-off.
Explain how the request flows from end to end in your high level design. Also you could draw a sequence diagram using the diagramming tool to enhance your explanation...
First , the loadbalancer receive request from client.
After process with algorithm like round-robin or Consistent Hashing, the requests are distributed to one of the servers.
If the traffic exceed the threshold of rate limiter, the request will be blocked.
The server process the request and store data into Mysql and redis at the same time.
Also put image and video into CDN;
When the read request come in, server retrieves files from CDN ,and query data from redis, if no exists, query from Mysql ,then put into redis.
Dig deeper into 2-3 components and explain in detail how they work. For example, how well does each component scale? Any relevant algorithm or data structure you like to use for a component? Also you could draw a diagram using the diagramming tool to enhance your design...
Explain any trade offs you have made and why you made certain tech choices...
I choose RDB like Mysql rather than NoSQL.
Although NoSQL provides schema flexibility , but it cannot support complex queries, Structured Data and transaction. Cause the business model of twitter does not change a lot , not like B2B business.
I choose Redis rather than memcache.
Cause redis support various data type and Horizontal Scaling. It is more suitable for large-scale system and high volume traffic.
Memcache is simple and efficient for basic key-value store, but does not have many advanced features.
Redis offers more robust solutions on scale and availability.
Try to discuss as many failure scenarios/bottlenecks as possible.
If there's a user who had followed a lot of people, the combination of home feed could take a long time.
So we can adopt a hybrid model.
Adopt hybrid model, set a threshold of followed people. If the threshold is exceeded, I will use push model to push the new tweets to the user. This can reduce latency and improve user experience.
If there's a celebrity, who is followed by numerous people. His tweet will become read hotspot.
What are some future improvements you would make? How would you mitigate the failure scenario(s) you described above?
To ensure the disaster recovery and availability of services, I would opt for a multi-region active-active strategy. This involves deploying service clusters and database clusters in multiple locations. Through automatic failover and load balancing, this strategy ensures that there are no single points of failure within any cluster.