Design Twitter: feed fanout and timeline system design
Add a Cache Layer
A Cache (Redis) stores pre-assembled feed slices per user. On a hit, we skip the DB entirely. But cache misses still fall back to N queries.
Request Flow
Cache hit β 2ms read
@alice requests her timeline β Cache returns pre-assembled IDs in ~2ms. No DB query needed.
GET /timeline β LRANGE timeline:alice 0 19 β [ tweetId1, tweetId2, ... ] // 2ms
Cache miss β N DB queries
When the cache is cold (new user, eviction), the system falls back to N DB lookups and then populates the cache for next time.
// Cache miss: β cache empty β SELECT x500 follows from DB β SET timeline:alice (assembled) β next read: cache hit
Why We Need This
Cache helps, but only reactively β it fills on the first read. Nothing populates the cache proactively when a new post arrives. We need a Feed Service to manage that.
Key Insight
Cache cut 70% of timeline DB reads. But cache misses are still expensive, and the cache is only populated lazily (on first miss). We need a fanout service to push new posts into follower caches at write time.
Step 2 of 6
