Two-Tower Retrieval Models
How YouTube narrows millions of videos to a few hundred in milliseconds.
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 user-item interaction pairs :
- Positive Pair: (User clicked Item ).
- Negative Pairs: Items () clicked by OTHER users in the mini-batch (In-Batch Negatives).
- : Temperature scaling hyperparameter (typically ).
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
- What is Sampling Bias in In-Batch Negatives? Popular items appear more frequently in mini-batches, causing them to be sampled as negative items more often. Correct using Log Q Correction: .
- What is Mixed Negative Sampling? Combining In-Batch Negatives with explicit Random Uniform Negatives to ensure the model learns both popular item discrimination and hard tail item retrieval.
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?