Problem Statement:
- User can publish a new message to their followers (broadcast)
- User can view tweets posted by the people they follow (inbox)
Method 1: Simple JOIN
- Insert directly into tweets table (on post)
- When a user requests their home timeline, get tweets from ones they follow and merge
Method 2: Fan-Out Broadcast
- Maintain a cache for each user’s home timeline—like a mailbox of tweets for each recipient user
- When user makes tweet, send posts into their followers' cache
Method 2 is better than Method 1 as no need to make many DB calls, but can be tougher for larger audiences.
Example: If a user has over 30 million followers. This means that a single tweet may result in over 30 million writes to home timelines!
Conclusion:
Twitter is moving to a hybrid of both approaches. Most users’ tweets continue to be fanned out to home timelines at the time when they are posted,
But a small number of users with a very large number of followers (i.e., celebrities) are excepted from thisfan-out