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

Open original source

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

    Figure SDA-RWS-16-1 · Short-message service architecture · © forcsguy.com

    Posting

    1. The tweet service validates the post and obtains a unique, time-sortable id (Snowflake-style, see the ID generator lesson).
    2. 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.
    3. 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

    1. Fetch the user's cached list of tweet ids.
    2. Merge in recent tweets from followed popular accounts, plus promoted content.
    3. Hydrate ids into tweets, authors and counts using caches (tweet cache, user cache, counters).
    4. Rank (recency, engagement, relationship, relevance) and return a page with a cursor.
    5. 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.

    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.

    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.

    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

    Open this lesson with its quiz and exercise

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.

  1. Warm upGet the vocabulary first.

  2. 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)

  3. ReadEngineers explain it in their own words.

  4. See it in another companyThe same ideas, a different system.

  5. Read the codeSee how it looks when it is real.

Keep learning

Loading community…