Case study: Twitter home timeline
Serve home timelines at hundreds of thousands of reads per second with hybrid fan-out: in-memory timelines for most authors, read-time merge for celebrities.
30 min9 steps
By the end you can
- Derive the fan-out write rate and timeline memory from published Twitter numbers
- Design the hybrid read path that merges precomputed timelines with celebrity tweets
- Explain how time-ordered IDs make merging and pagination cheap
- Predict how cache loss, slow fan-out and traffic spikes affect reads, writes and freshness
Requirements
Home timeline
- Post tweets; follow graph; cursor pagination
Bounded reads
- Fresh timelines; celebrity fan-out must not delay everyone
Estimates from published numbers
Timeline reads/s
300k
2012 published workload
Tweets/s
5k
~12k peak; ~400M/day
Fan-out inserts/s
1M
5k / 200 followers
Timeline RAM
~3 TB
150M / 800 IDs / 8 B / 3
Celebrity tweet
30M inserts
30M followers; motivates pull
API
POST /2/tweets
- 201 + tweet ID
GET /2/timeline/home?pagination_token=
- Merge/hydrate tweets; last seen ID becomes cursor
Data model
Source tweets
- Snowflake: 41-bit ms, 10-bit worker, 12-bit sequence; follow graph
Derived timelines
- ~800 IDs/active user; three replicas; rebuild from tweets + follows
High-level design: hybrid fan-out
Order the home timeline read.
- 1Wait for both
- 2Start two lookups in parallel: the reader's precomputed timeline and recent tweets by followed celebrities
- 3Merge the two lists by tweet ID (time order)
- 4Hydrate the top IDs into full tweets from the tweet cache
- 5Return the page and the next pagination token
Deep dive: choosing the threshold
Push below threshold
- F followers / F inserts/tweet; cheap timeline reads
Pull celebrities
- Pay on follower reads; avoid millions of burst writes and unused timelines
Alternatives
Pure push
- Cheap reads; celebrity write storm
Pure pull
- Cheap posts; hundreds of shard reads
Failure scenarios you can run
Inject
Timeline cache flush
Observe
Fall through to replicated timeline store
Result
Brief latency rise; no recomputation outage
Inject
Fan-out slows 6x
Observe
Queue lag rises hundreds of milliseconds
Result
Posts/reads succeed; new tweets appear late
Inject
Global event: 3x
Observe
Bounded reads stay fast
Result
Watch celebrity merge load on tweet store
Wrap up
Watch
- Fan-out lag, timeline hits, celebrity query load
Explain
- Threshold, inactive users, replicated IDs, Snowflake cursors
Then score each stage from 1 to 4.
Answer this step to continue.