Skip to main content

Welcome to a Grasp lesson

Create a free account to keep studying and create your own study path.

Lesson 81

Two-Stage Recommender System Architecture

Recommended resources

Welcome back! In our last lesson, we mastered how to optimize large, complex models using techniques like knowledge distillation and quantization to make them efficient enough for production. This is a crucial skill, as the powerful models used in modern AI systems are often too slow and expensive to deploy directly.

Today, we will apply this system-level thinking to one of the most impactful and ubiquitous ML applications: recommendation systems. You will learn how to design the high-level architecture of a two-stage recommendation system (candidate generation and ranking). This architectural pattern is the backbone of recommendation engines at nearly every top tech company, including Google (YouTube), Meta (Instagram), and Netflix. Understanding it is non-negotiable for an elite engineering role focused on scalable ML systems.

We will deconstruct this complex system into five interrelated concepts:

  1. The Two-Stage Paradigm: Why Not Just Rank Everything?
  2. Candidate Generation: Finding Potential Needles in a Haystack
  3. The Ranking Stage: Finding the Sharpest Needle
  4. Serving at Scale: The Online/Offline Dance
  5. Advanced Topics: Cold Starts, Bias, and Multi-Objective Ranking

1. The Two-Stage Paradigm: Why Not Just Rank Everything? (Easy)

  • Technical Explanation:
    Imagine a platform like YouTube, with billions of videos. For every user visiting the homepage, it's computationally impossible to evaluate every single video with a complex model to find the perfect recommendations. The latency would be minutes, not milliseconds.

    To solve this, modern recommendation systems use a two-stage "funnel" architecture:

    1. Candidate Generation (or Retrieval): This first stage acts as a coarse filter. Its job is to rapidly sift through the entire corpus of billions of items and select a few hundred plausible candidates (e.g., 500 videos) that are broadly relevant to the user. This stage prioritizes high recall (don't miss any potentially good items) and low latency.
    2. Ranking: This second stage takes the small set of candidates and uses a powerful, feature-rich, and computationally expensive model to score and rank them precisely. This stage prioritizes high precision (the top items on the list should be extremely relevant).

    This division of labor allows the system to deliver highly personalized recommendations from a massive catalog within strict latency budgets.

*This diagram from the original YouTube recommendation paper illustrates the funnel. Millions of items from the corpus are filtered by the Candidate Generation stage into hundreds, which are then meticulously scored and sorted by the Ranking stage to produce the final list of dozens shown to the user.*
  • Metaphor for a 5-Year-Old:
    You're looking for a new toy in the world's biggest toy store.

    • Ranking Everything (The Slow Way): You could look at every single toy in the entire store, one by one, to decide which one you want most. You'd be there all day!
    • Two-Stage System (The Fast Way):
      1. Candidate Generation: You first run to the "Lego" section. You've instantly ignored all the dolls and board games, narrowing millions of toys down to just a few hundred Lego sets.
      2. Ranking: Now, you carefully look at the box art, piece count, and theme of each of the Lego sets in that aisle to pick the absolute best one.
  • Anchor Resource:
    The original Google paper on YouTube's recommendation system is a foundational text. It clearly outlines the motivation for their two-stage design.

Deep Neural Networks for YouTube Recommendations

To understand the core architecture, let's start with the paper that introduced this influential design.

Read Section 2, 'SYSTEM OVERVIEW'. It's very short but perfectly describes the roles of the candidate generation and ranking networks and why this separation is critical for scale.



2. Candidate Generation: Finding Potential Needles in a Haystack (Medium)

  • Technical Explanation:
    The goal of this stage is to learn low-dimensional vector representations (embeddings) for both users and items. The core idea is that if we map users and items to a shared embedding space, we can find relevant items for a user by finding item embeddings that are "close" to that user's embedding. The dominant architecture for this today is the Two-Tower Model.

    A two-tower model consists of:

    • Query Tower: A neural network that takes user features (e.g., watch history IDs, search query tokens, demographics) and outputs a single user embedding vector, .
    • Item Tower: A separate neural network that takes item features (e.g., video ID, category) and outputs an item embedding vector, .

    The model is trained on historical user-item interactions (e.g., user U watched video V). For a positive pair (U, V), the training objective is to maximize the similarity between their embeddings, typically measured by the dot product: . For negative pairs (items the user did not interact with), the objective is to minimize this similarity.

  • Metaphor for a 5-Year-Old:
    Imagine you have a magic map. The Item Tower is a machine that takes every food in the world and puts a magic sticker on the map for it (e.g., apples are here, pizza is there). The Query Tower is another machine that talks to you about what you like to eat and then puts a "You Are Here" sticker on the map for you. To find food you'll like, you just look for all the food stickers that are right next to your "You Are Here" sticker.

  • Python Snippet (Simplified Two-Tower Model in PyTorch):
    This code shows the architectural skeleton. In a real system, the inputs would be more complex (e.g., bags of embeddings for watch history).

    import torch
    import torch.nn as nn
    
    class TwoTowerModel(nn.Module):
        def __init__(self, user_feature_dim, item_feature_dim, embedding_dim):
            super().__init__()
            
            # Query/User Tower
            self.user_tower = nn.Sequential(
                nn.Linear(user_feature_dim, 128),
                nn.ReLU(),
                nn.Linear(128, embedding_dim)
            )
            
            # Item Tower
            self.item_tower = nn.Sequential(
                nn.Linear(item_feature_dim, 128),
                nn.ReLU(),
                nn.Linear(128, embedding_dim)
            )
    
        def forward(self, user_features, item_features):
            # Generate embeddings
            user_embedding = self.user_tower(user_features)
            item_embedding = self.item_tower(item_features)
            
            # Normalize embeddings (good practice for cosine similarity)
            user_embedding = nn.functional.normalize(user_embedding, p=2, dim=1)
            item_embedding = nn.functional.normalize(item_embedding, p=2, dim=1)
            
            # Calculate dot product similarity for a batch
            # In a real training loop, you'd compare this to labels using a loss
            # like contrastive loss or binary cross-entropy on the logits.
            scores = torch.sum(user_embedding * item_embedding, dim=1)
            
            return scores
    
    # --- Example Usage ---
    EMBEDDING_DIM = 64
    model = TwoTowerModel(user_feature_dim=32, item_feature_dim=32, embedding_dim=EMBEDDING_DIM)
    
    # Fake batch of data: 4 users and 4 corresponding items they interacted with
    users = torch.randn(4, 32)
    items = torch.randn(4, 32)
    
    similarity_scores = model(users, items)
    print("Similarity scores for positive user-item pairs:")
    print(similarity_scores)
    
  • Anchor Resource:
    To see this explained in the context of an interview, watch this mock interview segment where a candidate details the two-tower architecture.

Instagram ML Question - Design a Ranking Model (Full Mock Interview with Senior Meta ML Engineer)

The candidate in this mock interview gives a very clear explanation of using a Two-Tower network to learn embeddings.

Watch the segment from 00:33:21 to 00:37:12. Pay close attention to how the candidate describes the two independent towers, the use of dot product and sigmoid for prediction, and binary cross-entropy for the training loss. This is a very standard and effective explanation.



3. The Ranking Stage: Finding the Sharpest Needle (Medium)

  • Technical Explanation:
    Once candidate generation provides a list of ~500 items, the ranker's job is to score them with high precision. The key advantage here is that we can now afford to use a much richer feature set. The ranking model is typically a large, multi-layer feed-forward neural network (a.k.a. a Deep Neural Network or DNN).

    Crucial Distinctions of the Ranking Stage:

    • Rich Features: Unlike the candidate generator which might only use user/item IDs, the ranker uses hundreds of features:
      • Query Features: Time of day, device, user language.
      • Item Features: Content features (text, image embeddings), freshness ("Example Age" in the YouTube paper), popularity statistics.
      • User-Item Interaction Features: How many times has this user watched videos from this channel? When was the last time? These are extremely powerful signals.
    • Complex Objective Function: Simply predicting click-through rate (CTR) is naive. It promotes "clickbait." A better objective is to predict expected engagement. For YouTube, this is often expected watch time. This is cleverly implemented using a weighted logistic regression, where positive examples (clicks) are weighted by the actual watch time. The model's output then approximates expected watch time.
    • Model Architecture: The input layer takes all these diverse features. Categorical features are embedded, continuous features are normalized (and often powered, e.g., ) to give the network more expressive power. These are then fed through several hidden layers (e.g., 1024 -> 512 -> 256 ReLU units).
*This diagram shows a typical ranking model architecture. Note the variety of input features (embeddings for categorical, normalized values for continuous) being fed into a series of ReLU layers. This complex model is feasible because it only scores a few hundred items per request.*
  • Metaphor for a 5-Year-Old:
    You have 10 final contestants for a talent show (the candidates). Now, a panel of three expert judges (the ranking model) comes out. They don't just ask "Can you sing?" (a single feature). They score each contestant on vocal quality, stage presence, costume, song choice, and audience reaction (many features). They combine all these scores to decide who is the absolute best performer (the top-ranked item).

  • Anchor Resource:
    The YouTube paper provides an excellent, in-depth look at their ranking model.

Deep Neural Networks for YouTube Recommendations

Let's return to the YouTube paper to understand the specifics of the ranking model.

Read Section 4, 'RANKING', focusing on sub-sections 4.1 ('Feature Representation') and 4.2 ('Modeling Expected Watch Time'). Pay close attention to the types of features used and the clever weighted logistic regression technique for predicting watch time.



4. Serving at Scale: The Online/Offline Dance (Hard)

  • Technical Explanation:
    How do these two stages come together in a live production environment? It's a combination of offline batch processing and online real-time inference.

    • Offline System (Batch Jobs):

      1. The two-tower model and the ranking DNN are trained on massive datasets of user logs.
      2. The trained item tower is used to pre-compute embeddings for every single item in the corpus (millions/billions).
      3. These item embeddings are loaded into a specialized, high-performance index for fast lookups. This is not a standard database; it's an Approximate Nearest Neighbor (ANN) index (e.g., using libraries like ScaNN or FAISS). ANN allows you to find the "closest" vectors to a query vector in sub-linear time, making it possible to search millions of items in milliseconds.
    • Online System (Per User Request):

      1. A user request hits the server.
      2. The system fetches the user's features (e.g., recent watches, profile info) from a feature store.
      3. These features are fed into the query tower in real-time to generate a fresh user embedding.
      4. This user embedding is used as a query to the ANN index. The index returns the IDs of the top ~500 items whose embeddings are closest to the user's embedding. This completes the Candidate Generation stage.
      5. For these 500 candidate IDs, the system performs a batch lookup to fetch their full feature sets from a feature store.
      6. The ranking model scores all 500 candidates using their rich features.
      7. The final list is sorted by score. A post-processing step might apply business logic (e.g., filter watched items, ensure diversity).
      8. The final, ranked list is returned to the user.
  • Metaphor for a 5-Year-Old:
    You're a librarian.

    • Offline: Overnight, you read every book in the library and create a master "topic" catalog (the ANN index) that lists which books are about space, which are about dinosaurs, etc.
    • Online: A child comes to you and says, "I love dinosaurs!" (the user query). You generate an idea of what they want ("dinosaurs", the user embedding). You instantly flip to the "dinosaurs" section in your master catalog (querying the ANN index) and pull out all 20 dinosaur books (candidates). Then, you quickly read the back covers of those 20 books (ranking) to recommend the one with the coolest pictures for that specific child.
  • Anchor Resource:
    This article provides a fantastic overview of the end-to-end process, including the ANN search step.

One-Stop Guide for Production Recommendation Systems

This guide clearly explains the overall architectural paradigm, including the role of ANN.

First, read the section 'Approximate nearest neighbor search' to understand its critical role in making candidate generation fast. Then, read 'Architectural Paradigm of Real-time Recsys' to see how the offline and online components work together. The diagram in that section is an excellent summary of the entire flow.



5. Advanced Topics: Cold Starts, Bias, and Multi-Objective Ranking (Harder)

  • Technical Explanation:
    A basic two-stage system is great, but elite systems must handle several complex challenges:

    1. The Cold Start Problem: How do you recommend to a new user (no interaction history) or recommend a brand-new item (no one has interacted with it yet)?
      • New User: Fall back on demographic features, geographic location, or simply show a globally popular set of items to gather initial signals.
      • New Item: Use its content features (text, images) to create an initial embedding. The item tower can be trained to handle this.
    2. Position Bias: Users naturally click on items at the top of a list more often, simply because they see them first. This pollutes your training data, making the model think top-ranked items are more relevant than they are.
      • Mitigation: A common technique is to include the item's position on the page as a feature during training. The model learns to associate a high position with a higher click probability. Then, during inference, this feature is removed or set to a default (e.g., position 1 for all candidates), forcing the model to rely only on the true relevance features.
    3. Multi-Objective Optimization: A business rarely has a single goal. You might want to optimize for watch time, and likes, and shares.
      • Solution: The ranking model can be designed with multiple "heads," where each head predicts a different objective. The final score is then a weighted combination of these predictions: Final Score = w1 * P(like) + w2 * P(share) + w3 * E(watch_time). The weights (w1, w2, w3) are tuned based on business priorities.
  • Anchor Resource:
    This video on YouTube's more advanced ranking system directly addresses position bias and multi-objective ranking.

Machine Learning System Design (YouTube Recommendation System)

Let's explore some of the more advanced challenges. This video details how YouTube's system evolves to handle them.

Please watch these key segments: (00:04:07 - 00:06:57): Explains multi-objective ranking using Mixture-of-Experts and multiple prediction heads for engagement vs. satisfaction. (00:06:57 - 00:08:07): Shows how these multiple predictions are combined into a single score for the final ranking. (00:08:07 - 00:11:02): This is a fantastic explanation of the position bias problem and how they solve it by including position as a feature during training but not during inference.

Test your understanding! (System Design Interview Question)

You are tasked with designing the recommendation system for the home feed of a short-form video app like TikTok or Instagram Reels. The corpus contains billions of videos, and you need to generate a personalized, "infinite" feed for each user in real-time.

  1. Outline the high-level, two-stage architecture you would use. Name the two main stages and describe their primary responsibilities.
  2. Describe the model architecture you would propose for the candidate generation stage. What are its inputs and outputs? How is it trained?
  3. For the ranking stage, what are three distinct categories of features you would use that you couldn't afford to use in the first stage? What would be a good primary objective for the ranker to optimize for, and why is it better than just predicting clicks?
  4. How would you address the "new user" cold start problem?
Show answer

1. High-Level Architecture:
I would implement a standard two-stage funnel architecture to balance latency and relevance at scale.

  • Stage 1: Candidate Generation (Retrieval): This stage's responsibility is to rapidly filter billions of videos down to a manageable set of ~500-1000 candidates for each user request. It prioritizes speed and high recall.
  • Stage 2: Ranking: This stage's responsibility is to take the candidates and use a powerful, precise model to score and rank them, producing the final feed presented to the user. It prioritizes precision and engagement.

2. Candidate Generation Model:
I would propose a Two-Tower Model.

  • Architecture: It would have a "User Tower" and a "Video Tower". Both would be deep neural networks.
  • Inputs:
    • User Tower: A vector representation of the user's interaction history (e.g., an averaged embedding of the last 50 videos they watched/liked), along with basic user profile features (e.g., country, language).
    • Video Tower: The ID of the video being embedded.
  • Outputs: Both towers output a fixed-size embedding vector (e.g., 128 dimensions).
  • Training: The model is trained on (user, video) interaction pairs. The objective is to maximize the dot product similarity of the output embeddings for positive pairs (videos the user watched) and minimize it for negative pairs (randomly sampled videos the user did not watch). Offline, the video tower is used to pre-compute embeddings for all videos, which are stored in an ANN index for fast online retrieval.

3. Ranking Stage Features and Objective:
In the ranking stage, I can use a much richer feature set since I'm only scoring a few hundred items. Three categories would be:

  1. Real-time Contextual Features: Time of day, day of week, device type. User preferences can change significantly based on context (e.g., watching educational content during the day, entertainment at night).
  2. Fine-grained Item Features: The actual content embeddings from the video's audio, video frames, and transcript. Also, recent popularity trends (e.g., "velocity" of likes in the last hour).
  3. Rich User-Item Interaction Features: How long did this user watch videos from this creator in the past? Have they ever explicitly muted this creator or this sound? These are highly predictive.

A good primary objective would be Predicted Watch Time as a Percentage of Video Duration. This is better than clicks because:

  • It directly measures engagement and satisfaction. A click means nothing if the user leaves after 1 second.
  • It penalizes "clickbait" that doesn't deliver on its promise.
  • It normalizes for video length, preventing the model from simply favoring shorter or longer videos. It rewards holding a user's attention relative to the video's length.

4. New User Cold Start:
For a new user with no interaction history, the user tower cannot generate a meaningful embedding. I would handle this by:

  1. Asking for Interests: During signup, ask the user to select a few categories of interest (e.g., Comedy, Dance, Tech, Animals).
  2. Initial Candidate Generation: Use the embeddings of these interest categories as a starting point to query the ANN index for an initial set of diverse but relevant candidates.
  3. Default to Popularity: The initial ranking could heavily weigh global or regional popularity to show content that is broadly engaging.
  4. Rapidly Update: As soon as the user watches their first few videos, their user embedding can be generated and the feed can quickly become personalized.


Conclusion

You now have a robust blueprint for designing a modern, large-scale recommendation system. This two-stage architecture is a powerful and proven pattern for tackling the immense scale of today's platforms.

Key Takeaways:

  • Recommendation at scale is a two-stage problem: fast, high-recall candidate generation followed by a precise, feature-rich ranking stage.
  • Two-Tower models are the industry standard for learning user and item embeddings for candidate generation.
  • Ranking models are powerful DNNs that leverage hundreds of features and optimize for complex business objectives like expected watch time, not just clicks.
  • The entire system is orchestrated through an online/offline design, where item embeddings are pre-computed and stored in an ANN index for low-latency retrieval during a user request.
  • Elite systems must also thoughtfully address challenges like cold start, biases in the data, and balancing multiple objectives.

Next Lesson Preview

In this lesson, we discussed that candidate retrieval is powered by an Approximate Nearest Neighbor (ANN) index that can search millions of embeddings in milliseconds. But how do these indexes work? What are their internal data structures and trade-offs? In our final content lesson for this module, we will dive deep into this critical piece of infrastructure and learn how to design the architecture of a scalable vector database using Approximate Nearest Neighbor (ANN) indexing.