Design Twitter: feed fanout and timeline system design
Introduce the Feed Service
A dedicated Feed Service reacts to every new post and decides how to fan it out. It classifies accounts, picks push or pull strategy, and proactively writes follower caches.
Request Flow
Post triggers Feed Service
After the API writes the tweet to the Database, it notifies the Feed Service asynchronously.
// After INSERT tweet:
feedService.notify({ tweetId, authorId })
// Feed Service receives event, begins fanoutFeed Service pre-populates caches
The Feed Service fetches @bob's follower list and writes the tweet ID into each follower's Cache timeline sorted set.
const followers = db.getFollowers(authorId);
for (const uid of followers) {
redis.lpush(`timeline:${uid}`, tweetId);
redis.ltrim(`timeline:${uid}`, 0, 800);
}
// @alice's cache ready BEFORE she opens the appWhy We Need This
Now @alice's timeline is pre-built before she opens the app. Cache hit rate climbs to 95%+. Timeline reads drop to a single O(1) cache fetch.
Key Insight
The Feed Service decouples post-writing from feed-assembly. It turns reads from O(N) DB queries into O(1) cache lookups. The cost is write amplification: 1 post x N followers = N cache writes.
Step 3 of 6
