Two-Tower Model for Recommendation Service having a Very Large Catalog | ||
| ||
| The Two-Tower model (also called a dual-encoder) is the workhorse of large-scale candidate generation (retrieval). It encodes users and items into the same embedding space using two separate neural networks — a user tower and an item tower — and measures affinity by a simple similarity (dot product or cosine) between the two output vectors. The decisive property: because the towers are independent and only meet at the final similarity, item embeddings can be precomputed offline and indexed for approximate nearest-neighbor (ANN) search. At request time you encode just the user, then retrieve the top-K items from millions or billions in milliseconds. This is what makes retrieval at internet scale feasible.
How It Works (The Math)Each tower is a neural network mapping raw features to a
Affinity is their inner product (optionally normalized):
Training as in-batch softmaxThe model is trained as a retrieval/classification problem: given a user, the positive item should score higher than all others. Using in-batch negatives, every other item in the mini-batch serves as a negative for a given user, which is extremely efficient:
Sampling-bias correctionIn-batch negatives are drawn by popularity, biasing the softmax toward popular items. The standard fix (Yi et al., 2019) subtracts a logQ correction using the estimated sampling probability
This logQ-corrected in-batch softmax is the canonical recipe for production two-tower retrieval.
Requirements
Simplified Architecture
Reference Architecture
Reference Architecture (with Technology Stack)The same flow on concrete tooling — towers trained on GPU with TensorFlow/PyTorch, batch item-tower inference via Spark/Beam, a Milvus/ScaNN vector index, and the user tower served live on Triton.
Signals & FeaturesThe user tower consumes interaction history (often pooled or sequence-encoded), demographics, and context (device, time, location). The item tower consumes content features (text/image embeddings, category, creator, age) alongside the item ID. Feeding content features into the item tower is precisely what lets the model embed brand-new items sensibly before they have any interactions — its cold-start advantage over ID-only matrix factorization.
Training & Serving ConsiderationsThe architecture deliberately keeps the towers decoupled so item embeddings are query-independent and cacheable. This is also its main limitation: because users and items only interact via a single dot product, the model cannot learn fine-grained cross-features — that work is deferred to the downstream ranking model. The serving loop has two cadences: frequent user-tower inference at request time, and periodic batch re-embedding of the catalog plus ANN index rebuild. Embedding “staleness” between rebuilds is a real operational concern for fast-moving catalogs.
EvaluationRetrieval quality is measured with recall@k (did the relevant item appear in the retrieved top-K?) and the contribution to downstream end-to-end metrics after ranking. Because retrieval is the funnel’s top, its job is high recall, not precise ordering — over-optimizing retrieval precision is a common mistake. Monitor index freshness and the share of impressions sourced from the two-tower retriever.
Strengths & WeaknessesStrengths: massive-scale retrieval via precomputed item embeddings + ANN; handles item cold-start through content features; efficient in-batch-negative training; the de facto standard for the candidate-generation stage. Weaknesses: the decoupled dot product cannot model user–item cross-features, capping accuracy (hence a separate ranking stage); sampling/popularity bias must be corrected; index staleness between rebuilds; tuning negatives and the logQ correction is nontrivial.
When to Use ItUse the two-tower model whenever you must retrieve a small relevant candidate set from a very large catalog under tight latency — the first stage of nearly every modern feed and search-ranking system (YouTube, Google Play, large e-commerce). Pair it with a heavier ranking model (Wide & Deep, DeepFM, or a sequence/cross model) that re-scores the retrieved candidates with full feature interactions. | ||
| | ||
| |