Skip to content
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

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
1client → lb · GET /timeline/homeA user pulls to refresh.1 / 7

The numbers, at a glance

For every tweet……there are about 20 timeline reads
1 tweet ≈ 5,000/s20 reads ≈ 100,000/s
Timeline inserts at peak15,000 tweets a second
Pulling huge accounts saves most insertsInserts left after the hybrid
The few accounts with over 1 million followers make most of the fan-out.

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.

Pull on read
Too slow
GET timeline
query 200 authors
sort, take 20
+ No work at post time.− 30 index reads per page.
Push on write
Breaks on huge accounts
tweet
push to every follower
read 20 IDs
+ Reads are one memory read.− 22.5M inserts/s at peak.
Hybrid
Our pick
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.

Stage 1 · ≈ 1,000 reads/sMake it work
Client
App server
SQL database
One server, one database, timeline built by a query.
Stage 2 · ≈ 50,000 reads/sPrecompute timelines
Client
Load balancer
App × NTweet stream + fan-out
Tweet cacheTweet storeTimeline cacheFollow graph
Push tweet IDs into cached timelines; cache the tweets.
Stage 3 · ≈ 300,000 reads/sShards and celebrities
Client
Load balancer
App × NHybrid fan-out
Tweet cache16 tweet shardsTimeline cacheFollow graph
16 tweet shards; pull accounts over 1M followers on read.
Stage 4 · 300,000 reads/s + searchSearch
Client
Load balancer
App × NHybrid fan-outSearch indexer
Tweet cache16 tweet shardsTimeline cacheFollow graphSearch index
An index fed by the tweet stream, split by time.
Was this walkthrough useful?Your vote tells us what to improve next.