Design a News Feed (Twitter Timeline), stage 4 of 9: break it
Thirty million followers
Twitter's target was to deliver to a million followers in about 3.5 seconds. Thirty million is a different problem.
System so far· 9 parts
Select a component to see what it is responsible for and which state it owns.
- 1Authors → Write API: Post
- 2Write API → Post store: Store post
- 3Write API → Fan-out queue: Fan-out job
- 4Fan-out workers → Fan-out queue: Take jobs
- 5Fan-out workers → Social graph: Active followers
- 6Fan-out workers → Timeline cache: Push ID, trim to 800
- 7Readers → Timeline service: GET home timeline
- 8Timeline service → Timeline cache: Page of IDs
- 9Timeline service → Post store: Hydrate; large accounts' recent posts
- Request / response
- Asynchronous
What you need to know
Averages hide the units of work that hurt. A system that's fine for a 200-follower account can stall on a 30-million-follower one, because that single post becomes a single enormous job, competing with every other delivery.
Work it out
Fan-out delivers about 1 million timelines in 3.5 seconds. About how many seconds for one post to 31 million followers?Pull has the opposite profile: posting is one write, and readers pay a merge. For an account with millions of followers, the merge is cheap: its recent posts are the same for every reader and sit in cache.
So the strategy can be chosen per account: push for the many accounts with ordinary audiences, pull for the few very large ones.
Check
A reader follows 3 accounts above the threshold. What extra work does their timeline read do?