Urgent.News

What's breaking now, across thousands of outlets.

Tech

How Twitter Scales Its Home Timeline: Fan-Out on Read vs Fan-Out on Write

Imagine opening Twitter (now X). Your home timeline shows tweets from hundreds or thousands of accounts you follow, sorted by time. Seems simple, right? But think about the scale: Millions of users accessing their timelines. Millions of tweets being published. Every user follows a different set of accounts. Some accounts have millions of followers. How do you generate everyone's timeline…

Twitter faces the challenge of efficiently generating home timelines for millions of users, each following a unique set of accounts with varying numbers of followers. The solution involves two main strategies: Fan-Out on Read and Fan-Out on Write.

The Fan-Out on Read approach, also known as the pull model, generates timelines each time a user requests them. When a user opens their timeline, the system fetches the list of accounts they follow, retrieves recent tweets from those accounts, merges and sorts them by timestamp, and returns the timeline. This method is advantageous because publishing tweets is relatively cheap, and updates to individual timelines aren't required when someone publishes a tweet.

However, it has disadvantages such as high read traffic, which can strain the system, and expensive merging and sorting of tweets during each request.

Conversely, the Fan-Out on Write approach, or push model, prepares timelines when tweets are published. When an account publishes a tweet, a fan-out worker finds all the follower accounts and pushes the tweet ID into each follower's home timeline cache. When a follower opens Twitter, they simply fetch their precomputed timeline.

This method is beneficial for read-heavy applications because reading timelines becomes extremely fast, as most of the computation happens during publishing. However, it is expensive for publishing, particularly for accounts with millions of followers, as a single tweet can trigger millions of writes, creating substantial write amplification.

Twitter addresses this challenge by implementing a hybrid approach. Normal accounts, who don't have millions of followers, use the Fan-Out on Write strategy. Precomputed timelines are generated for each follower's timeline when a tweet is published. Celebrity accounts, with millions of followers, utilize the Fan-Out on Read strategy.

Instead of pushing tweets into millions of follower timelines, these accounts' tweets are fetched only when followers request their timelines. When a user opens their timeline, the system fetches their precomputed timeline and merges it with recent tweets from celebrity accounts they follow. This hybrid approach balances the costs of reading and writing, optimizing for different user workloads and traffic patterns.

Written by urgent.news from Dev.to's reporting — not their text. Machine-written — may contain errors; check the original before relying on it.

Read the original at dev.to →

More in Tech

Apple Store Subscription Sign-Up Controversy Exposed

In a series of YouTube videos shared over the past two weeks, electronics technician and consumer rights advocate Louis Rossman has exposed a controversy in which some Apple Store employees have allegedly signed up customers for auto-renewing subscription trials for Apple One and other services without their informed consent.

More from Monday 9 November →