Twitter’s News Feed Dilemma: Reactive Or Proactive?

Ankit Sahay
4 min readSep 21, 2023

--

Out of many, Twitter has 2 major operations:

  1. Post Tweet: A user can publish a new message to their followers (~5k requests per second on average and ~12k requests at peak)
  2. Home Timeline or News Feed: A user can view tweets posted by the people they follow on their news feed (300k requests per second)

(Above numbers are from around 2017, so they would definetely be up today in 2023)

Handling 12k writes per second is fairly easy. However, Twitter’s scaling challenge is not primarily due to tweet volume, but due to fan-out — each user follows many people, and each user is followed by many people.

Now that we have basics out of the way, let’s talk about the problem at hand. When a user logs in to Twitter, they see some tweets on their News Feed. Those tweets are from people that the user’s follow. Taking an example from above diagram, if I login to Twitter — I would see 2 tweets from Kohli, 1 from Shah Rukh Khan and 1 from Messi on my News Feed. So the question is, how is the News Feed created for me (and millions of other users)? That is our problem statement.

Approach 1

When someone tweets anything, simply insert the new tweet into a global collection of tweets (any database). When a user requests their home timeline or news feed, look up all the people they follow, find all the tweets for each of those users, and merge them (sorted by time). Again going back to the example, say Kohli, Messi and Shah Rukh khan have each tweeted 1 tweet which gets inserted into a database. When I login — first the system fetches all the people I follow (SRK, Messi, Kohli, Friend 1 and Friend 2). Then the system queries all the tweets from these 5 people and arrange them by time and shows on my timeline.

This approach was indeed used by Twitter when it launched. But the systems struggled to keep up with the load of News feed queries. So, Twitter moved to the next approach.

Approach 2

Maintain a pre-calculated News Feed as a cache for each user. When a user posts a tweet, look up all the people who follow that user, and insert the new tweet into each of their news feed caches. Example — A news feed is pre-calculated proactively for every user. As I am follwed by 5 users (follower 1,2,3,4 and 5), when I tweet something, the pre-calculated news feed for those 5 followers of mine are updated in their cache. Now when any of the user logs in the request to read the news feed is then cheap, because its result has been computed ahead of time.

Let’s Dissect Both Approaches

The reason why 2nd approach works better because the average rate of published tweets is almost two orders of magnitude lower than the rate of news feed reads (write is 12k/sec while read is 300k/sec), and so in this case it’s preferable to do more work at write time and less at read time.

However, the downside of approach 2 is that posting a tweet now requires a lot of extra work. On average, a user has 75 followers, so on average a tweet is delivered to about 75 followers, so 4.6k tweets per second become (4.6k*75) 345k writes per second to the news feed caches.

But this average hides the fact that the number of followers per user varies wildly, and some users have over 30 million followers. This means that a single tweet may result in over 30 million writes to news feed caches! Doing this in a timely manner — Twitter tries to deliver tweets to followers within five seconds — is a significant challenge.

A Final Twist to the implementation

Now that approach 2 is robustly implemented, Twitter is reportedly moving to a hybrid of both approaches. Most users’ tweets continue to be fanned out to news feed caches 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 this fan-out. Tweets from any celebrities that a user may follow are fetched separately and merged with that user’s news feed when it is read, like in approach 1. This hybrid approach is able to deliver consistently good performance.

Hope you liked this article. For more such engineering problems, join my whatsapp channel by clicking here

--

--

Responses (1)