Assuming we already have a user log in system that handles authentication and authorization for us
Estimate the scale of the system you are going to design...
to reduce load on GIFs we can rely on a bank of GIFs to reduce duplication of GIFs that need to be stored and instead store a link to the GIF used by tweet
we can also cache the most popular media and most popular tweets for faster retrieval
Define what APIs are expected from the system...
Assuming headers will contain information about which user is making the request
POST /tweet/ - allows users to post a tweet, can take in a media attachment
returns 200 upon successful write of tweet to DB
returns error if write is not successful
automatically retries on error, using an exponential retry system before declaring total failure after 3 retries
POST /retweet/ - allows users to retweet another users post
returns 200 upon successfully writing link to users tweet in user's DB post entry
returns error if write is not successful, use exponential retry system for 3 retries before declaring total failure
GET /newsfeed/ - returns a users newsfeed, fetching tweets from the people they follow that have been made in the last 3 days and then running them through a prioritization algorithm to sort them based on engagement
returns 200 and newsfeed
returns 404 on error, using an exponential retry system
POST /response/ - allows users to respond to a tweet
re
DELETE /deleteTweet/ - allow user to delete tweet
return 200 if authorized and delete successful
return error if otherwise
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...
User DB
userID - primary key
account creation date - datetime
tweets - list of tweetIDs associated with an account
follows - list of userIDs user follows
followers - list of userIDs following user
Tweet DB
tweetID - primarykey
text of tweet
mediaID - foreign key of media contained in tweet
author - foreign key of userID
creation timestamp
Media DB
media ID - primary key
media
tweetIDs - list of tweets associated with media
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...
user accesses application via a web UI or an app, logging in and getting authenticated and being issued a token with a TTL to allow them to continue interacting with the application without having to refresh
all user requests will pass through an API gateway to allow for built in rate limiting and authorization services
all requests will hit a load balancer that will route the request to the right service and ensure an even level of load across all hosts
the service will pull the needed information from the database and return it, or return an error if the database read was unsuccessful
the service will write to the database if the request is a POST
Service break down:
User service will manage:
Tweet service will manage:
Media service will manage:
all databases will have back up, read only copies that will be updated using a gossip protocol
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...
user logs in -> gets authenticated through API gateway, receives some sort of auth token so we don't have to re-authenticate in the future -> request for newsfeed automatically generated, passes through load balancer and returns the top 20 or so tweets and then places the rest in a cache for faster access as the user scrolls
user posts a tweet -> tweet is written to tweet database
user retweets a tweet -> link to original tweet added to DB containing list of tweet ID's for a user
user follows another user -> user gets written to their followers list in the db
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...
Try to discuss as many failure scenarios/bottlenecks as possible.
What are some future improvements you would make? How would you mitigate the failure scenario(s) you described above?