Design Twitter: feed fanout and timeline system design
Push Model: Fanout-on-Write
For regular users, the Feed Service fans out every post to all follower caches at write time. Timeline reads become a single O(1) Cache fetch β 5ms vs 250ms.
Request Flow
Post fans out to all follower caches
The Feed Service writes @bob's tweet to Alice's, Carol's, and Dave's Cache timelines. The cost: 1 post = N cache writes.
// 1 post β N writes:
tweetId = 'tweet_001'
followers = [alice, carol, dave, ...]
redis.lpush('timeline:alice', tweetId)
redis.lpush('timeline:carol', tweetId)
redis.lpush('timeline:dave', tweetId)
// 500 followers = 500 writes per postTimeline read is a single cache lookup
@alice requests her timeline β single LRANGE on her Cache β 5ms. No DB, no fanout at read time.
GET /timeline β LRANGE timeline:alice 0 19 β [tweet_001, tweet_099, ...] // ~5ms // Hydrate content: MGET tweet:tweet_001 tweet:tweet_099 β full feed assembled
Why We Need This
Write amplification is the cost of fast reads. For users with 500 followers, every post triggers 500 Redis writes. For a celebrity with 50M followers, this becomes catastrophic β hence the hybrid model.
Key Insight
Push trades write cost for read speed. It works brilliantly for regular users. For celebrity accounts it creates write storms that can saturate the entire cache tier.
Step 5 of 6
