500 M DAU
Each user, send 2 tweets per day on average: 1B tweets per day.
Each tweets has 140 bytes, with meta data, so 500 bytes.
Storage: 1 B * 500 b -> 500 GB per day. (storage cost for 2 years: 500GB* 365 * 2-> 400 TB)
Database storage required: 500 TB
It better to use NoSQL database, the typical capacity of Relational database is around 100 TB.
Document based DB: MangoDB or DynamoDB
QPS: 500 M / day (10^5 seconds) -> 500 * 10 ^ 6 / (10^5) -> 5000 QPS on average
If the latency of 1 API call to pull tweets is 500 ms per core: need 10000 cores
10000 core / 8 core per instance -> 2000 machine instances
Each user:
Each user, send 2 tweets per day on average: 1B tweets per day.
Bandwidth:
Bottlenecks:
It is worth note that: millions of user might be viewing same content concurrently.
Tweet (document NoSQL database):
User (can be stored in SQL database):
Follower Table (can be stored in SQL database):
Timelines data (Cache or NoSQL database)
Scenario 1 user post tweet:
User -> LB -> tweet writer service -> DB
-> cache
Scenario 2 user visit a specific user's timeline
User -> LB -> tweet timeline service -> DB or cache
Need very low latency (< 200ms), cache is required.
Naive approach: query database with specific UserId, and fetch tweets in reversed chronological order (DB read is slower)
Fast approach: query cache. The timeline data of a hot user is stored in cache when the user post a new tweet
When to update cache: user post new tweet, fan out on write. The user write tweet event is going to update the cache of its own timeline tweet list.
Scenario 3 user visit Home timeline
User -> LB -> tweet timeline service-> Database
-> Cache
Naive approach (Pull Model):
Query all the following userIds of a user, then query all the new tweets of those userIds, then merge all these tweets based on some TopN algorithm, then return to the user. (DB read and in memory compute is too slow)
Improvement (Push Model):
User cache. Store the home timeline of a certain of user into the cache, so the user do need to fetch data and do compute and aggregation on the fly.
When to update cache: user post new tweet, fan out of write. The user write tweet event is going to update the cache of all its followers' home timeline tweet list.
Pros:
Read O(1)
Cons:
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...
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...
Bottlenecks:
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?
Scalability and Availability: