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.)...


  1. follow / unfollow
  2. home feed
  3. like/unlike tweets
  4. Not considering attachments for now


Non-Functional:

List non-functional requirements for the system...


  1. highly available to be able to interact with all the features.
  2. We can do with consistency not so high.
  3. high scale system.





Capacity estimation

Estimate the scale of the system you are going to design...

  1. 500 million daily active users.
  2. 2 tweets per day means 1 biilion total tweets per day.
  3. 100 views per tweet.
  4. max 140 characters per tweet
  5. average 50 characters per twe




API design

Define what APIs are expected from the system...

  1. POST /tweet
  2. PUT /tweet
  3. GET /feed/{optional_offset}
  4. POST /tweet/like/{id}
  5. POST /tweet/unlike/{id}
  6. POST /user/follow/{id}
  7. POST /user/unfollow/{id}



Database design


  1. mongoDb - store tweets.

shard document if user exceeds the max storage per doc.


  1. mongoDb - to also store newsfeed like a document


POSTGRES - for follow/following relationship



High-level design



  1. Tweet service -> requires -> ability to store tweets, provides with reliable ability to get tweets
  2. 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.
  3. user service ( follow/unfollow operations )
  4. 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?