My Solution for Design Twitter with Score: 5/10
by halo2879
System requirements
Functional:
List functional requirements for the system (Ask the chat bot for hints if stuck.)...
- follow / unfollow
- home feed
- like/unlike tweets
- Not considering attachments for now
Non-Functional:
List non-functional requirements for the system...
- highly available to be able to interact with all the features.
- We can do with consistency not so high.
- high scale system.
Capacity estimation
Estimate the scale of the system you are going to design...
- 500 million daily active users.
- 2 tweets per day means 1 biilion total tweets per day.
- 100 views per tweet.
- max 140 characters per tweet
- average 50 characters per twe
API design
Define what APIs are expected from the system...
- POST /tweet
- PUT /tweet
- GET /feed/{optional_offset}
- POST /tweet/like/{id}
- POST /tweet/unlike/{id}
- POST /user/follow/{id}
- POST /user/unfollow/{id}
Database design
- mongoDb - store tweets.
shard document if user exceeds the max storage per doc.
- mongoDb - to also store newsfeed like a document
POSTGRES - for follow/following relationship
High-level design
- Tweet service -> requires -> ability to store tweets, provides with reliable ability to get tweets
- News feed service -> requires ability to be able to check the follower count of the tweet poster in order to be able to decide the flow for new tweet event.
- user service ( follow/unfollow operations )
- Push user events for building news feed.
Request flows
POST /tweet -> store mongodb -> push queue -> consumer reads -> sends to newsfeedsvc -> push new event to all followers newsfeed( in case of popular user just store it as one of the popular events ).
GET /newsfeed -> go to newsfeed svc -> query newfeed for the user + merge with popular tweets list -> respond.
Detailed component design
Newsfeed svc - need to store a stack of newsfeed to be shown to the user. Just use document store (mongoDB) here as well to push new tweets to the newsfeed.
Popular tweets -> store popular user's tweets in a cache like redis. query latest 5 posts of all the popular tweets and merge the result in the newsfeed.
Trade offs/Tech choices
Explain any trade offs you have made and why you made certain tech choices...
Failure scenarios/bottlenecks
Future improvements
What are some future improvements you would make? How would you mitigate the failure scenario(s) you described above?