SD-6 · Back to the write-up
Design a Microblog Timeline
Push each tweet’s ID into followers’ cached timelines, pull huge accounts at read time, and fill in the text from a tweet cache.
Follow a request
Post a tweet
Fan a tweet out
Read the timeline
A huge account tweets
A timeline is missing
Clients
Edge
Services
Data
ClientApp or browser
Load balancerRate limits
App serversStateless
Tweet streamNew tweets
Fan-out workersPush IDs
Search indexRecent tweets
Tweet cacheTweets by ID
Tweet store16 shards
Timeline cache800 IDs a user
Follow graphFollowers
The numbers, at a glance
1 tweet ≈ 5,000/s20 reads ≈ 100,000/s
3M/sHybrid
22.5M/sPush all
13% of the inserts22.5M/s if we pushed everyone
A timeline load, in order
On a timeline cache miss, the server rebuilds the list from the graph and the store first.
Client
Load balancer
App server
Timeline cache
Tweet cache
Tweet store
1 GET /timeline/home
2 route
3 20 IDs + 4 recent lists
4 IDs, merged by time
5 MGET 20 tweets
6 19 hits
7 GET 1 miss
8 tweet
9 200 page
10 20 tweets + next_max_id
RequestResponseAsync, does not block
When to build the home timeline
The decision this question is really about.
GET timeline
query 200 authors
sort, take 20
+ No work at post time.− 30 index reads per page.
tweet
push to every follower
read 20 IDs
+ Reads are one memory read.− 22.5M inserts/s at peak.
tweet
push if under 1M followerselse recent list
read IDs + merge
+ Fast reads, 3M inserts/s.− Merge a few lists on read.
How it grows
Add a box only when a number forces you to. Highlighted boxes are new at each stage.
Client
App server
SQL database
Client
Load balancer
App × NTweet stream + fan-out
Tweet cacheTweet storeTimeline cacheFollow graph
Client
Load balancer
App × NHybrid fan-out
Tweet cache16 tweet shardsTimeline cacheFollow graph
Client
Load balancer
App × NHybrid fan-outSearch indexer
Tweet cache16 tweet shardsTimeline cacheFollow graphSearch index
Was this walkthrough useful?Your vote tells us what to improve next.