List functional requirements for the system (Ask the chat bot for hints if stuck.)...
registry
login
profile management
post twitter, length = 140
comment tweet
forward tweet
delete tweet
follow other user
trends
home feed + recommendation ranking
like tweet
user activity log reporting
Notifications can include: that user can decide which type of notificaiont they want
eventual consistency: A delay of a few seconds to a minute is often tolerable.
availability: 99.9% uptime, 8.76 hours of downtime per year. Not over 1 hour for each incident.
performance: write a tweet (post it) should ideally be within 200 milliseconds. This ensures a smooth and quick experience when users send tweets.
Opening a tweet should be very quick, targeting around 100 milliseconds to display the tweet content.
performance:Retrieving and serving the home feed with recommended tweets should ideally take less than 500 milliseconds. Pre-generating recommendations can help achieve this.
Estimate the scale of the system you are going to design..
Basic data:
DAU: 500Million
create tweet: 2 tweet every day = 1,000 Million tweets every day
read tweet: 100 /user / day = 100 * 500 M = 50,000 M views every day
years: 5
Maximum followers: 10,000
Maximum following: 5,000 (to prevent spammy behaviors)
Each tweet has a maximum length of 140 characters
QPS:
read:
50,000 M /24/3600 = 0.5 Million = 578K QPS
backend server: 115
sql db: ~1000
peak hour: backend server 230
sql db ~ 2000
write:
1000M / 24/ 3600 = 0.01 Million = 11K write per sec
peak hour: 2 times average, 22K write per sec
assuming backend server handle 5K QPS, need 5 backend server
Assuming DB handle 500QPS, need 50 db server, some db server is better than this, we can talk about this later section
Data storage:
data for storage = 1,000 M * 140Bytes * replica = 3 = 420,000 MB = 420 TB every day
5 years = 420TB * 365 * 5 = 766,000 T = 766 PB
Network bandwidth:
read: 578K * 140/2 = 40,000K = 40MB per sec
write 11K * 70 = 700K per sec
/registry
/login
/post,input: user_id, content
/delete, input: user_id, tweet_id
/like, input: user_id, tweet_id
/follow, input: follower_id, followee_id
/unfollow, input: follower_id, followee_id
/forward,input: user_id, tweet_id
/comment,input: user_id, tweet_id, content
/home_feed, input:user_id, return array[tweet_id]
/notification
/trends, return array[hashtag]
tweets
|tweet_id
|content|varchar
|cretead_at|
|status| enum['deleted', 'posted', ]
user
|user_id| int|
|description| varchar|
|State| varchar(2)|
|Country| varchar|
graph db
following_relation
follower
followee
follow_time
user_view_relation
user_id
tweet_id
status | enum['seen']
in memory db for recommendation:
user_id, array[tweet_id]
Load balancer
API gateway
Backend server for different endpoint
in memory cache for reading tweet
sql server for storing user info and tweet info
cache for storing recommendation result, will discuss tradeoff here
kakfa server
recommendation calculation engine, spark, flink, mapreduce etc
influxdb to store monitoring
grafana to show metrics
tweet related: user -> load balancer- > API Gateway -> backend server -> update db, also send data to kafka and computation engine will calculate recommendation result daily/hourly
notification: user <-> push/poll notification server
home_feed: user -> recommendation backend sever -> query from cache,
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...
/post, /delete, /forward):user_id % 1024). Async replication to followers' timelines via Kafka.status=deleted in DB) + cache invalidation./follow, /unfollow):/home_feed):tweet:{id}, Value: JSON with content, likes, retweets.user_id. TTL after 5 years (archived to S3).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?
/registry.