The core problem Twitter’s feed system solves isn’t just displaying tweets, but doing so instantly for millions of users, even when those users follow hundreds or thousands of other accounts.
Imagine you’re a user, and you just tweeted something. You want your followers to see it now. If Twitter waited until each follower asked for your tweet (a "pull" model), and each follower followed 1000 people, that’s 1000 individual requests per follower every time any of those 1000 people tweet. This scales terribly.
Instead, Twitter uses a "fanout" or "push" model. When you tweet, that tweet is immediately sent out to a list of people who follow you. This pre-computation means when a user requests their feed, it’s largely a matter of retrieving already-delivered tweets, not orchestrating a massive lookup across all the people they follow.
Here’s a simplified view of the fanout process:
- Tweet Ingestion: A user posts a tweet. This is the initial event.
- Fanout Service: A dedicated service receives this tweet. Its job is to figure out who needs to receive this tweet.
- Follower Graph Lookup: The fanout service queries a system (like a graph database or a cache of follower lists) to get the list of user IDs who follow the tweeter.
- Fanout Delivery: For each follower ID, the tweet is "delivered." This delivery often means writing the tweet ID to a data structure associated with the follower, typically a Redis list or a similar in-memory store. This makes fetching the feed very fast: just read the last N tweet IDs from the follower’s list.
Let’s see this in action with some hypothetical configurations.
When a tweet comes in, say from user_A (ID 12345), it hits a fanout service. This service might have a configuration like:
{
"service_name": "tweet_fanout_v2",
"fanout_strategy": "fanout_to_followers",
"max_followers_to_fanout_to": 10000,
"delivery_mechanism": "redis_list",
"redis_host": "redis-fanout-01.prod.twitter.com",
"redis_list_key_template": "feed:{user_id}"
}
The service then fetches user_A’s followers. Let’s say user_B (ID 67890) follows user_A. The fanout service would then perform an operation like:
# On redis-fanout-01.prod.twitter.com
RPUSH feed:67890 <tweet_id_from_user_A>
This RPUSH operation adds the tweet ID to the right end of the Redis list named feed:67890. When user_B requests their feed, the client might perform:
# On redis-fanout-01.prod.twitter.com
LRANGE feed:67890 0 49 # Get the latest 50 tweet IDs
The client then takes these IDs, fetches the full tweet objects from a tweet storage service, and renders the timeline. This is incredibly fast because Redis is in-memory.
The challenge is the "millions of QPS" part. At peak, Twitter can receive hundreds of thousands of tweets per second. Each tweet needs to be fanned out. If an average user has 200 followers, and 10% of users are "celebrities" with 10,000+ followers, the fanout system needs to handle a massive write load. This is why multiple fanout services run, often sharded by user ID or tweet ID, and why Redis clusters are heavily optimized for write throughput.
What most people don’t realize is how aggressively Twitter optimizes for the "happy path" of a regular user and then has specialized handling for "celebrity" users. For users with millions of followers, fanning out every single tweet to every follower’s Redis list would overwhelm their individual feed storage and the fanout system itself. Instead, for these high-follower accounts, a different strategy is employed: their tweets are not fanned out immediately to all followers’ feeds. Instead, the fanout service might simply mark the tweet as being from a "celebrity." When a user requests their feed, the system then merges tweets from the people they follow (pulled from their pre-populated feed lists) with recent tweets from the celebrities they follow (fetched live or from a separate, smaller cache). This hybrid approach balances the write cost of fanout with the read performance of consuming a feed.
The next hurdle is handling the sheer scale of data and the latency requirements for real-time interactions, especially when dealing with the "hybrid" fanout for celebrities.