List functional requirements for the system (Ask the chat bot for hints if stuck.)...
Tweet Management
Follow System
Search
System Requirements
List non-functional requirements for the system...
Estimate the scale of the system you are going to design...
Define what APIs are expected from the system...
Get a Tweet
GET twitter.com/api/1/tweets/:id
Request Header: [
Authorization : Bearer <JWT Token>
]
Response : 200 OK
Respond Body : [Tweet instance]
Get Tweets by user
GET twitter.com/api/1/users/:id/tweets/
Request Header: [
Authorization : Bearer <JWT Token>
]
Response : 200 OK
Respond Body : [a list of tweets]
Delete a tweet
DELETE twitter.com/api/1/tweets/:id
Request Header: [
Authorization : Bearer <JWT Token>
]
Response : 200 OK
Post a Tweet
POST twitter.com/api/1/tweets
Request Header: [
Authorization : Bearer <JWT Token>
]
Request Body: [A tweet]
Response: 201 Created
Update a tweet
PUT twitter.com/api/1/tweets
Request Header: [
Authorization : Bearer <JWT Token>
]
Request Body: [A tweet]
Response: 200 OK
Retweet a tweet
POST twitter.com/api/1/users/:id/tweets/:source-tweet-id
Request Header: [
Authorization : Bearer <JWT Token>
]
Response: 200 OK
Unlike/Like API
PATCH twitter.com/api/1/tweets/:id/like
Request Header: [
Authorization : Bearer <JWT Token>
]
Request Body: [a tweet identifier, like/unlike]
Search Trends
GET twitter.com/api/1/trending/topics
Request Header: [
Authorization : Bearer <JWT Token>
]
Response: 200 OK
Response Body: [a list of Trending tweets]
Get User API
GET twitter.com/api/1/users/:id/
Request Header: [
Authorization : Bearer <JWT Token>
]
Response: 200 OK
Response Body: [an instance of user]
Get User by username
GET twitter.com/api/1/users/by/username/:name
Request Header: [
Authorization : Bearer <JWT Token>
]
Response: 200 OK
Response Body: [a list of users]
Get following
GET twitter.com/api/1/users/:id/following/
Request Header: [
Authorization : Bearer <JWT Token>
]
Response: 200 OK
Response Body: [a list of users]
Get followers
GET twitter.com/api/1/users/:id/followers/
Request Header: [
Authorization : Bearer <JWT Token>
]
Response: 200 OK
Response Body: [a list of users]
Follow a user
GET twitter.com/api/1/users/:source-user-id/following/
Request Header: [
Authorization : Bearer <JWT Token>
]
Response: 200 OK
UnFollow a user
GET twitter.com/api/1/users/:source-user-id/following/:target-user-id
Request Header: [
Authorization : Bearer <JWT Token>
]
Response: 200 OK
Timeline API
PATCH twitter.com/api/1/users/:id/timeline?cursor={date}®ion={region}
Request Header: [
Authorization : Bearer <JWT Token>
]
Response : 200 OK
Response Body : [a list of 1000 tweets in reverse_chronological order]
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...
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...
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...
Tweeting
Twitter stream processing finds top trending topics, keywords, top k tweets around the globe. Its bucketised to regional (country, city). The real-time stream processing engine computes this incrementally
Sliding Window
Track frequency in last n minutes, hours
Surge Detection
Compare current frequency vs historical baseline
Top K
Count Min-Sketch - keep track of most frequent hash tags with low memory footprint, avoid botnet activity or spam, demote such active hashtags or hashtags used in a private group.
Count Min-Sketch (CMS)
Instead of keeping all the hashtags or trending topics in to cache or database to compute the popularity of topics, we use a count min-sketch which gives and approx estimate of popular topics. This is computed for each hashtag and the max hash count is populated onto a min heap kept in cache. The CMS is efficient data structure that it takes very less memory,
if we have 2 hashtags "#ukraine" and #tariff" and depth = 5, we compute 5 hashes for these #ukraine and #tariff, and take the min value of these 5 hashes. The min value is compared in the Min heap, if the min value in min heap is smaller that count from CMS then replace the min heap. This takes less memory and efficient. Only space used is Min heap of K size
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...
Handle Hot Tweets
We use Twitter-Snowflake ID as tweetId, this is sortable by time and allows range queries. A tweetId consists of
timestamp(48 bits), datacenterId(4 bits), MachineId (8 bits) sequenceNo(4 bits)
The first 48 bits is the timestamp of the machine that received the tweet, the datacenter id in which the machine belongs, MAC address or machine id assigned, a running counter in the machine. This ensures the tweet id is unique and used for sharding. i.e. tweetId % N database nodes gives us the location where to store the data.
A users timeline is sharded across thousands of machine using userid i.e shard_id = hash(userId) % N shards
Hot hashtags
What happens when a hashtag goes viral, for instance a hashtag #worldcup goes viral, this introduces millions of tweets, retweets, likes etc. We use a separate inverse index for hashtag and attach the tweets to the hashtag.
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?