Two-Tower Retrieval Models

How YouTube narrows millions of videos to a few hundred in milliseconds.

🔴 advanced5 min readrecsysretrieval
A two-tower model is how large apps like YouTube pick a few hundred candidates out of millions of items in milliseconds. One network turns the user and their history into a vector, and a second, separate network turns each item into a vector. A good match means the two vectors point the same way. Because the item side does not depend on the user, every item's vector can be computed in advance, so finding the best matches becomes a fast search through a pre-built index.

Two-Tower Architecture (YouTube DNN Pattern)

  USER CONTEXT FEATURES                                ITEM METADATA FEATURES
  (User ID, Age, Watch History, Device, Location)       (Video ID, Title Embeddings, Channel, Duration)
           │                                                     │
           ▼                                                     ▼
  [ USER TOWER (MLP + Embeddings) ]                     [ ITEM TOWER (MLP + Embeddings) ]
           │                                                     │
           ▼                                                     ▼
     User Vector u [1 × 128]                               Item Vector v [1 × 128]
           │                                                     │
           └──────────────────────────┬──────────────────────────┘
                                      ▼
                            Dot Product: Score(u, v) = u · v

Real-Time Production Serving Loop

  OFFLINE BATCH PHASE:
  Run Item Tower over 10,000,000 Catalog Items ──► Pre-compute Items Matrix V [10M × 128]
  Index V in Vector DB (HNSW / Qdrant)

  ONLINE REAL-TIME INFERENCE (< 10ms):
  User Requests Feed ──► Pass User Features through User Tower ──► Output Vector u [1 × 128]
  Execute Vector Search: Top 1000 = HNSW_Search(u, V) ──► Pass 1,000 Candidates to Ranking Stage!

Loss Formulation: In-Batch Sampled Softmax

Given a mini-batch of BB user-item interaction pairs {(u1,v1),(u2,v2),…,(uB,vB)}\{(u_1, v_1), (u_2, v_2), \dots, (u_B, v_B)\}:

L=−1B∑i=1Blog⁡exp⁡(ui⋅vi/τ)∑j=1Bexp⁡(ui⋅vj/τ)\mathcal{L} = -\frac{1}{B} \sum_{i=1}^B \log \frac{\exp(\mathbf{u}_i \cdot \mathbf{v}_i / \tau)}{\sum_{j=1}^B \exp(\mathbf{u}_i \cdot \mathbf{v}_j / \tau)}

Say this out loud

"Two-Tower models decouple user and item feature processing into independent neural networks, projecting both into a shared embedding space. Pre-computing item embeddings offline enables sub-10ms candidate retrieval over millions of items via ANN vector search (u · v). We train two-tower models using in-batch sampled softmax loss."

Follow-ups to expect

Check yourself

Question 1 of 3

Why does a Two-Tower Retrieval Model separate User feature processing from Item feature processing into two independent neural networks?

More in RecSys & Search

See all →
Collaborative Filtering5 minThe Cold Start Problem4 minTwo-Stage: Retrieval then Ranking5 min