Skip to content
mlmentorship

Two-tower retrieval

Encode queries and items with separate networks into a shared embedding space; retrieve by approximate nearest neighbors. The default architecture for industrial recommenders and search.

Published · 5 min read ·Role-specific ·Intermediate

Visual quick review

Visual first · depth when needed

Trace which half of two-tower inference is paid offline for the whole catalog and which half is paid once per request, explaining why retrieval can search millions of items without running the item tower millions of times.

Preparing the visual…

Summary

A two-tower model encodes the query and item with separate neural networks into a shared embedding space. A dot product or cosine scores each pair. Contrastive or sampled-softmax training pushes positive pairs above negatives.

Two-tower (a.k.a. dual-encoder) is the dominant architecture for the retrieval stage of large-scale ranking systems: web search, e-commerce search, YouTube recommendations, ad targeting, dense passage retrieval for RAG, semantic search.

The structural advantage: item embeddings can be precomputed once per item and indexed. At query time you only run the query tower (cheap) and do an approximate-nearest-neighbor lookup (sub-linear in catalog size). Cross-encoders (where query and item are concatenated into a single network) cannot be precomputed and are 100–10000× too slow for retrieval at scale.

Architecture

query  → query_tower  → q ∈ R^d
item   → item_tower   → i ∈ R^d
score  = q · i  (or cosine)
  • Towers: typically transformers, MLPs, or a mix. Towers usually do not share weights (different input modalities or feature sets).
  • Embedding dim : 64–512 in production. Higher is more expressive; lower is faster to index and more cache-friendly.
  • Output normalization: L2-normalize so dot product equals cosine; lets the index use Inner Product mode (see embedding spaces).

Learning objective: trace which tower runs during a catalog refresh and which tower runs on every request, so you can explain why two-tower retrieval scales.

Learning objective

Precompute the catalog side; run only the query side per request.

Offline item encoding feeds the index used by online query retrieval The upper offline lane sends every catalog item through the item tower and writes the resulting item vectors to an approximate nearest-neighbor index during a batch refresh. The lower online lane sends one incoming query through the query tower to produce vector q. The index compares q with its stored item vectors and returns top-K candidate IDs. Text labels and a dashed batch-refresh path distinguish the two lifecycles without relying on color. 1 · OFFLINE OR WHEN THE CATALOG / MODEL CHANGES ALL CATALOG ITEM FEATURES ITEM TOWER run in batch ITEM VECTORS i₁, i₂, …, iₙ batch refresh writes stored vectors 2 · ONLINE FOR EACH REQUEST ONE QUERY + context QUERY TOWER run once QUERY VECTOR q ∈ Rᵈ ANN INDEX q · stored i TOP-K IDS to ranker No item-tower call occurs on this request path.
Read it this way: follow the dashed refresh path once for the catalog: encode every item, then store its vector in the ANN index. For each request, follow only the solid lower path: encode one query, search the stored vectors, and send top-K IDs to the ranker. The speedup comes from moving item-tower work out of the request path. Original schematic checked against Yi et al., the TensorFlow Recommenders retrieval guide, and Google Cloud's two-tower serving architecture.

Training

Standard losses:

In-batch sampled softmax

For a batch of positive (query, item) pairs, treat the other items in the batch as negatives. Loss per query:

Cheap, parallelizes well, but biases toward popular items (popular items appear as negatives more often).

Importance-corrected sampled softmax

Correct the in-batch sampling bias by subtracting from each negative’s logit. Standard in YouTube’s two-tower (Yi et al., 2019).

Hard negative mining

Sample hard negatives (high-scoring but incorrect items) explicitly. More expensive but improves quality, especially after the model is past the easy-negatives stage.

Two-stage architecture

In production systems, two-tower is almost always the retrieval stage, followed by a cross-encoder ranker:

  1. Retrieval (recall-oriented): two-tower returns top-K (e.g., 1000) candidates from millions of items in <10 ms via ANN.
  2. Ranking (precision-oriented): cross-encoder or feature-rich tree model ranks the K candidates with full feature interactions.

Tradeoffs vs. cross-encoder

PropertyTwo-towerCross-encoder
Latency at scalesub-linear (ANN)linear in catalog
Qualitylower (no query-item interactions)higher
Memoryone vector per itemnone (recomputed per query)
Use caseretrievalreranking

Common pitfalls

  • Using two-tower for ranking when accuracy matters. Lacks fine-grained feature interactions.
  • Ignoring negative sampling bias. In-batch sampled softmax favors popular items; always combine with importance correction or popularity de-biasing.
  • Forgetting to refresh item embeddings. When the item tower changes (new training run), all item embeddings must be re-encoded and re-indexed. Plan for periodic offline re-embedding.
  • Comparing dot vs. cosine inconsistently. Pick one (usually L2-normalized + dot) and use it everywhere.
  • Embedding spaces. Vector representations and indexing.
  • RAG overview. Retrieval-augmented generation uses two-tower for the retrieval step.