System requirements


Functional:

users able to share tweets

users can subscribe to other users

  • tweets sent by these users will appear in the subscriber's feed list
  • time order
  • up to 1000

users can favorite tweets

  • there's a place to check all favorited tweets
  • up to 1000

tweets can include both media files and texts

users receive notifications when people they subscribe post tweets



Non-Functional:

support user worldwide

high scalability, reliability for a big scale

low latency





Capacity estimation

100 million users, 10% DAU -> 10 million

10 million tweets created per day -> 100 write qps, ~ 200 peak qps

Suppose on average users check tweets 10 times per day -> 1k read qps to retrieve tweets from users they subscribe.


one char 1 byte, ~300B, let's assume on average 100B, and 10% of tweets have media, with average 3MB -> 1GB text, 1TB media every day ->~400GB text, ~400TB media per year






API design


REST APIs


  • retrieve a list of tweets (read)

request {

long requestUserId

// for pagination

long anchorId

int limit


boolean isFavorite

}


response {

list tweets

long nextAnchorId

}


struct Tweet {

long id

String text

List mediaUrls

}

  • subscribe/unsubscribe to user (write)

request {

long requestUserId

long followingUserId


// enum action, can be follow or unfollow

Action action

}


response {

boolean successful

// error messages if not successful. Used by clients to deliver user-facing messages

Set errorMessages

}


  • post tweets (write)

request {

long requestUserId

String text

List mediaUrls

}


response {

boolean successful

// error messages if not successful. Used by clients to deliver user-facing messages. examples like reaching daily limit

Set errorMessages

long tweetId

}

  • favorite tweets (write)

request {

long requestUserId

long tweetId

}


response {

boolean successful

// error messages if not successful. Used by clients to deliver user-facing messages.

Set errorMessages

}




Database design





High-level design

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





Request flows

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






Detailed component design

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






Trade offs/Tech choices

Explain any trade offs you have made and why you made certain tech choices...






Failure scenarios/bottlenecks

Try to discuss as many failure scenarios/bottlenecks as possible.






Future improvements

What are some future improvements you would make? How would you mitigate the failure scenario(s) you described above?