Twitter (X) System Design
The For You timeline: candidates, ranking and filters from X's own code
By Ankur NayakReviewed
How X builds the For You timeline, based on the code it open-sourced in 2023: Home Mixer gathers candidate posts from the Earlybird search index (in-network) and graph and embedding services (out-of-network), hydrates about 6,000 features, scores them with a neural network that predicts engagement, then filters and mixes in ads and recommendations.
Open in Studio· edit a copy- Ranking features
- ~6,000
- In-network share
- ~50%
- Realtime index window
- ~7 days
- UTEG memory
- 24–48 hours
Requirements
Functional
- Post, read, edit and delete posts
- Show a Following timeline: posts from accounts you follow, newest first
- Show a For You timeline mixing followed accounts with recommended posts
- Search posts in real time
- Filter content for legal compliance, safety and quality
Non-functional
- Build a ranked timeline on every request, quickly
- Make new posts searchable and recommendable within moments
- Rank with thousands of features per candidate
- Generate unique, time-ordered IDs without coordination
In depth
A timeline built on every request
"Design Twitter" is one of the most common system design questions, and it is usually answered with fan-out: push each new post into every follower's timeline. That describes the Following tab, a reverse-chronological list of posts from accounts you follow. The For You tab is a different problem: from millions of recent posts, pick the few dozen you are most likely to find worth reading, every time you open the app.
In 2023 Twitter (now X) open-sourced much of the code behind For You in two repositories, the-algorithm and the-algorithm-ml. Their READMEs describe the pipeline component by component, and this page is built from them. They don't cover storage scale or failure handling, so those parts are kept brief and marked as inferred where they appear.
Home Mixer
The For You timeline is built by Home Mixer, "the main service used to construct and serve Twitter's Home Timelines": For You, Following and Lists. It is built on Product Mixer, X's Scala framework for building feeds, where candidate pipelines fetch content, scoring pipelines rank it and mixer pipelines combine heterogeneous results.
Home Mixer's README lists the stages:
- Candidate generation: fetch posts from several candidate sources.
- Feature hydration: fetch the roughly 6,000 features needed for ranking.
- Scoring and ranking with a machine-learning model.
- Filters and heuristics: author diversity, a balance of in-network and out-of-network posts, feedback fatigue, removing posts you've already seen, and visibility filtering.
- Mixing: integrate posts with non-post content such as ads, who-to-follow modules and prompts.
Where candidates come from
In-network candidates, posts from people you follow, come mainly from Earlybird, X's real-time search system built on Apache Lucene. The repository's README says this source provides about 50% of For You candidates. Earlybird is a sharded, replicated inverted index: a realtime cluster indexes public posts from about the last seven days, a protected cluster covers protected posts, and an archive cluster indexes everything older. Ingesters read posts and user updates from Kafka, a feature-update service feeds it fresh engagement counts, and root servers merge results from the partitions.
Out-of-network candidates come from several places. UTEG, the User Tweet Entity Graph, is built on the GraphJet framework and keeps an in-memory graph of user–post engagements from the past 24–48 hours. Traversing it finds posts liked by people you engage with: collaborative filtering, the source of "X liked" posts. tweet-mixer coordinates fetching other out-of-network candidates from underlying compute services, and the follow-recommendation service suggests accounts and their posts.
Ranking
The heavy ranker is a neural network: the the-algorithm-ml README describes a parallel MaskNet that outputs a set of probabilities, one per kind of engagement: a like, a repost, a reply, a click into the author's profile, watching half a video, the author engaging with your reply, negative feedback, a report and more. The final score is a weighted sum of those probabilities.
The weights published as of April 5, 2023 show what the product valued: a like weighed 0.5, a repost 1.0, a reply 13.5, a reply that the author engages with 75, negative feedback −74 and a report −369. The README notes the weights are adjusted periodically and may differ from production. Models are served by Navi, X's high-performance model-serving system written in Rust.
After ranking, visibility filters remove content for legal compliance, product quality and user trust, and heuristics enforce diversity before ads and modules are mixed in.
Posting
On the write side, Tweetypie is the core service that reads and writes post data; clients reach it through GraphQL to fetch, create, delete and edit posts. It stores posts in Manhattan, X's distributed database, and reads through the Twemcache cache layer. New posts and user actions flow through Kafka to the services that index them, such as Earlybird and UTEG.
Every post needs a unique ID. In 2010 Twitter built Snowflake to generate them without coordination: a 64-bit ID made of a 41-bit millisecond timestamp, a 10-bit machine ID and a 12-bit sequence number. IDs sort roughly by time, and each process had to produce at least 10,000 per second with about 2 ms response time. The original release is retired; X runs a rewrite. See ID generation.
The trade-offs
X's For You design ranks on read: it rebuilds the timeline from fresh candidates on each request, which uses the latest engagement signals at the cost of heavy computation per request. It separates candidate generation from ranking, so the expensive model only sees a few candidates, but a post that no source retrieves can never be shown. It ranks with weighted engagement predictions, which lets product teams change priorities without retraining, while making those weights a set of value judgements. And it keeps only recent engagement in memory for fast graph traversals, with no long-term memory in that component.
Compare with the fan-out approach in the news feed design and Facebook's cache-first social graph.
What to take into an interview
- Separate the Following timeline (fan-out, chronological) from For You (rank on read)
- Generate candidates cheaply from several sources, then rank a small set with an expensive model
- Use a real-time inverted index for recent posts from people you follow
- Use an in-memory engagement graph for fresh out-of-network recommendations
- Predict several engagement types and combine them with tunable weights
- Filter for safety and diversity after ranking, then mix in ads and modules
Components
X App
Client. Requests timelines and posts through X's GraphQL API.
Tweetypie
Service. The core service that handles reading and writing post data: fetch, create, delete and edit. Clients reach it through GraphQL; it doesn't store user objects.
Manhattan
+ TwemcacheNoSQL database. Tweetypie reads posts from Manhattan (X's distributed database) or the Twemcache cache layer, and writes new posts to Manhattan.
Building block: Cache
Snowflake IDs
Service. Twitter's 2010 ID service: 64-bit IDs from a 41-bit millisecond timestamp, a 10-bit machine ID and a 12-bit sequence, generated without coordination. The original release is retired; X runs a rewrite.
Building block: Unique ID Generation
Event Streams
KafkaStream. Posts, user modifications and a real-time stream of user actions flow through Kafka topics to the services that index them.
Building block: Message Queue & Event Log
Earlybird
LuceneSearch index. Real-time search built on Apache Lucene: a sharded, replicated inverted index. A realtime cluster indexes public posts from about the last 7 days, and an archive cluster everything older. The main source of in-network candidates, about half of For You.
Building block: Inverted Index, Database Sharding
UTEG
GraphJetGraph database. An in-memory graph of user–post engagements from the past 24–48 hours, fed by Kafka. Traversals find out-of-network posts liked by people you engage with: collaborative filtering.
Out-of-Network
tweet-mixer, FRSService. tweet-mixer coordinates fetching out-of-network candidates from underlying compute services; the follow-recommendation service suggests accounts and their posts.
Home Mixer
Product MixerService. The main service that builds and serves the Home timelines (For You, Following, Lists). Built on Product Mixer, X's framework for building feeds from candidate, scoring and mixer pipelines.
Why: A pipeline framework lets teams add candidate sources, features and filters without rewriting the timeline service.
Candidate Pipelines
Service. Fetch posts from Earlybird, UTEG, tweet-mixer and the follow-recommendation service.
Feature Hydration
Service. Fetch the ~6,000 features needed to rank each candidate.
Scoring
MaskNetML model. Predicts the probability of each engagement (like, repost, reply, profile click, negative feedback, report…) and combines them in a weighted sum.
Filters & Heuristics
Service. Author diversity, in-network vs out-of-network balance, feedback fatigue, removing previously seen posts, and visibility filtering.
Mixing
Service. Blends posts with non-post content: ads, who-to-follow modules and prompts.
Heavy Ranker
Navi (Rust serving)ML model. The neural network that ranks candidate posts, one of the main signals for selecting timeline posts. X's model serving system, Navi, is written in Rust.
Visibility Filters
Service. Filters content to support legal compliance, improve product quality and increase user trust.
Request flows
Build a For You timeline
- You open the app; Home Mixer starts building your For You timeline.
- Earlybird returns recent posts from accounts you follow: about half of the candidates.
- UTEG traverses recent engagements to find posts liked by people you interact with.
- tweet-mixer and follow recommendations add more out-of-network candidates.
- Each candidate, with ~6,000 features, is scored by the heavy ranker.
- Visibility filters remove content that shouldn't be shown; heuristics diversify authors.
- The ranked timeline, mixed with ads and who-to-follow, comes back to the app.
Post and become discoverable
- You post; the app calls Tweetypie through GraphQL.
- Tweetypie writes the post to Manhattan.
- The new post is published to Kafka topics.
- Earlybird's ingesters index it in the realtime cluster, so followers' timelines and search can find it.
- The app shows your post as published.
Failure scenarios
What if: an Earlybird partition is slow?
Earlybird is sharded and replicated, and its roots merge responses from partitions. How X handles a slow partition isn't documented; a common approach is to route to another replica or return partial results rather than stall the timeline.
User impact: Possibly fewer in-network candidates for a moment.
- Replicated partitions
- Partial results from the root (inferred)
What if: an out-of-network source fails?
For You blends several independent candidate pipelines. If one fails, the timeline can still be built from the others; this is inferred from the pipeline design, not documented.
User impact: A timeline with fewer recommendations, mostly from accounts you follow.
- Independent candidate pipelines (inferred)
Trade-offs
Rank on read
Build the For You timeline on each request from fresh candidates instead of precomputing it.
- Pros
- Uses the latest engagement signals
- Mixes in-network and recommended posts freely
- Cons
- Heavy compute per request: thousands of features per candidate
Candidate generation then ranking
Narrow millions of posts to a candidate set with cheap sources, then apply an expensive model.
- Pros
- Expensive ranking only runs on a small set
- New sources can be added independently
- Cons
- A post missed by every candidate source can never be ranked
Weighted engagement prediction
Predict several engagement probabilities and combine them with tunable weights.
- Pros
- Product teams can change priorities without retraining
- Negative feedback can be weighted heavily
- Cons
- Weights encode value judgements that need constant tuning
In-memory graph for recent engagement
Keep only the last 24–48 hours of user–post engagements in UTEG.
- Pros
- Fast traversals for fresh recommendations
- Cons
- No long-term memory; older signals must come from elsewhere
Frequently asked questions
How does the Twitter (X) For You algorithm work?
Home Mixer gathers candidate posts from the Earlybird search index (posts from accounts you follow, about half), from UTEG's graph of recent engagements and from other out-of-network sources. It hydrates about 6,000 features, scores each post with a neural network, filters and diversifies the results and mixes in ads and suggestions.
What does the heavy ranker predict?
Probabilities of engagements such as a like, a repost, a reply, a profile click, the author engaging with your reply, negative feedback or a report. As of April 2023 they were combined in a weighted sum where, for example, a reply the author engages with weighed 75 and a report −369.
What is a Snowflake ID?
Twitter's 2010 scheme for unique IDs without coordination: 64 bits made of a 41-bit millisecond timestamp, a 10-bit machine ID and a 12-bit sequence number, so IDs sort roughly by time. Each process had to produce at least 10,000 IDs per second.
What is Earlybird?
Earlybird is X's real-time search system, built on Apache Lucene. A realtime cluster indexes public posts from about the last seven days and an archive cluster holds older posts. It powers search and supplies in-network candidates to the Home timelines.
Building blocks used
Sources
- the-algorithm (README) · X (GitHub)
- Home Mixer README · X (GitHub)
- Earlybird search README · X (GitHub)
- User Tweet Entity Graph README · X (GitHub)
- Heavy ranker (recap) README · X (GitHub)
- Tweetypie server README · X (GitHub)
- Snowflake (2010 release) · Twitter (GitHub)