Library / Case Studies
Productionizing ML with Workflows at Twitter
Cortex automates machine learning pipelines at Twitter with Airflow.
By Twitter engineers · Twitter · Added 2026-10-12
All content belongs to Twitter engineers / Twitter. We only link to it and give credit. Report a problem
Learn this case study
About 37 min left. Your work is saved as you go.
What you are about to study
Twitter explains a real system. You should know the basics: caching, queues and replication.
Ideas you will meet
- Distributed Systems
- Consensus, time, failure and the fundamentals of scale-out.
- Machine Learning Systems
- Recommendations, ranking, feature stores and serving models at scale.
What we already know about this
Our own lessons on these ideas. Read them here before you start, so it makes sense.
Real-World Systems · 13 minHow a Twitter-style service worksPosting, timelines, search, trends and counters at huge scale: the architecture of a short-message social network.Read here
The problem
A service where people post short messages, follow others, and read a timeline of recent posts. The numbers are what make it hard: hundreds of millions of users, huge read traffic, and a few accounts followed by tens of millions.
Core operations
- Post a message (with media, mentions, hashtags).
- Follow and unfollow.
- Read the home timeline (people I follow), and a user timeline (one person's posts).
- Search posts and see trends.
- Likes, replies, reposts and notifications.
Estimates
Say 300 million monthly users, 100 million daily active, each reading their timeline 10 times a day and posting a few times a week.
- Timeline reads: 1 billion a day, about 12,000 a second on average and several times that at peaks.
- Posts: about 50 million a day, a few hundred per second on average.
- Reads outnumber writes by roughly 20 to 1, so we optimise for reading.
High-level architecture
100%Figure SDA-RWS-16-1 · Short-message service architecture · © forcsguy.com
Posting
- The tweet service validates the post and obtains a unique, time-sortable id (Snowflake-style, see the ID generator lesson).
- It stores the tweet in a sharded tweet store, partitioned by tweet or user id. Media goes to object storage and is served through a CDN.
- It publishes a "tweet created" event to a stream, so the rest of the work happens asynchronously: fan-out, search indexing, notifications, trends and analytics.
Home timeline: fan-out
The home timeline is the hot path. Two strategies (from the news feed lesson):
- Fan-out on write: when a user posts, workers look up their followers and push the tweet id into each follower's cached timeline list (for example Redis, keeping only the latest few hundred ids). Reading a timeline is then one quick lookup.
- Fan-out on read: build the timeline on demand by merging recent tweets of everyone you follow. Cheap to post, expensive to read.
The hybrid used at scale: fan out on write for most accounts, but skip accounts with huge follower counts. Their tweets are fetched at read time and merged into the cached list. This avoids one post creating tens of millions of writes.
Reading the timeline
- Fetch the user's cached list of tweet ids.
- Merge in recent tweets from followed popular accounts, plus promoted content.
- Hydrate ids into tweets, authors and counts using caches (tweet cache, user cache, counters).
- Rank (recency, engagement, relationship, relevance) and return a page with a cursor.
- Filter muted, blocked and deleted items at read time.
User timeline
A user's own posts are a simple range query: tweets for one author in time order. They are stored by author id and cached, and paginated with a cursor.
The social graph
Who follows whom is a huge graph with fast lookups in both directions (followers of A, accounts followed by B). It is stored in a specialised, sharded store, with caches, and updated asynchronously when users follow or unfollow.
Search
Posts are indexed in an inverted index (term to list of tweet ids), usually built on a Lucene-style engine. The indexer consumes the event stream, so new tweets become searchable within seconds. The index is sharded by time (recent tweets are the most searched and kept in memory) and replicated. Queries combine text relevance, recency and engagement.
Trends
Trending topics are not the most common words, but the ones that are unusually frequent right now. A stream processor counts hashtags and phrases in sliding windows (say 5 minutes), compares them to their normal rate, filters spam and sensitive terms, and publishes the result per region.
100%Figure SDA-RWS-16-2 · Search indexing and push notifications from the same event · © forcsguy.com
Counters and notifications
Likes, reposts and view counts are updated very frequently. Use sharded counters and approximate, eventually consistent values, batching updates, since an exact real-time number is rarely required. Notifications are produced from the event stream and delivered through the notification system (see that lesson).
Scale and reliability
- Cache everything hot: timelines, tweets, users, counters. The tweet store is the source of truth, not the read path.
- Shard stores by id, and replicate across zones and regions.
- Rate limit posting, following and API calls to fight spam and abuse.
- Degrade gracefully: if ranking is slow, return a chronological timeline; if search lags, show fewer results.
- Moderation and abuse systems run on the same event stream.
- Real systems evolve, and details in public talks differ from year to year; the principles above (async fan-out, hybrid push and pull, caching, sharding) are the durable part.
Real-world reading
Related reading
- Hello Interview: System Design in a HurryHello InterviewCourses
- Hello Interview: System Design WalkthroughsEvan King & Stefan Mai · Hello InterviewYouTube
- Consistent Hashing and Random Trees (paper)Karger et al. · MITPapers
- The System Design PrimerDonne Martin · donnemartinGitHub
Show 6 moreShow fewer
- How Discord Stores Trillions of MessagesBo Ingram · DiscordBlogs
- How Discord Stores Billions of MessagesDiscordCase Studies
- YouTube ArchitectureHigh ScalabilityCase Studies
- TAO: The power of the graphMeta Engineering · MetaCase Studies
- Dropbox Tech BlogDropbox engineers · DropboxBlogs
- Productionizing ML with Workflows at TwitterTwitter engineers · TwitterCase Studies
Go deeper on this
A path through videos, articles, other case studies and code on the same ideas as Productionizing ML with Workflows at Twitter.
Warm upGet the vocabulary first.
Books · Free from the author
Concurrent and Distributed Systems (Cambridge lecture notes)
Eight lectures on time, broadcast, replication, consensus and transactions, with notes, slides and video. Free from the author under CC BY-SA.
University of CambridgeIntermediate
Books · Free from the author
Computer Networks: A Systems Approach
How networks are built and why: protocols, routing, congestion control and the internet's architecture. The authors publish it free online under CC BY 4.0.
Systems ApproachIntermediate
WatchHow others solve the same problem, on video.
Videos belong to their creators and play from YouTube. More on YouTube (opens in a new tab)
ReadEngineers explain it in their own words.
Blogs
Amazon Science
Technical deep-dives and perspectives from our scientists who are pushing boundaries across multiple scientific disciplines while solving real-world challenges. Explore how our teams combine research excellence, world...
AmazonIntermediate
Papers
Atom Smashing using Machine Learning at CERN
A real-world engineering write-up from CERN on distributed Machine Learning.
CERNIntermediate
See it in another companyThe same ideas, a different system.
Case Studies · Read here
Aroma: Using ML for Code Recommendation at Facebook
Thousands of engineers write the code to create our apps, which serve billions of people worldwide. This is no trivial task—our services have grown so diverse and complex that the codebase contains…
FacebookIntermediate
Case Studies · Read here
Concourse: Generating Personalized Content Notifications in Near-Real-Time at LinkedIn
Social media are computer-mediated platforms that facilitate creation and sharing of information, ideas, career interests, and other forms of expression via virtual communities and networks. In the last few years, not...
LinkedInIntermediate
Read the codeSee how it looks when it is real.
GitHub
Engineering Blogs
A huge directory of company engineering blogs to learn from real production systems.
kilimchoiIntermediate
GitHub
Awesome Scalability
Curated reading list of articles on the architecture of scalable, reliable systems.
binhnguyennusIntermediate
Keep learning
Loading community…