Recommendation systems
A funnel: cheaply retrieve hundreds of candidates from millions (two-tower embeddings + ANN), rank them with a heavy model on rich features, then re-rank for diversity and business rules.
Analogy: A bookstore: the clerk grabs 30 books from the right shelves in seconds, then thinks hard about which 5 suit you, and finally makes sure they aren't all by one author.
How it works
- A two-tower model embeds users and items in one space, so candidate retrieval becomes a nearest-neighbor lookup.
- The ranker, a GBDT or deep model, predicts clicks, watch time or purchases from user, item and context features.
- Cold start: new items have no interactions yet, so lean on content features and popularity.
- Feedback loops: you only learn from what you showed, which amplifies popularity bias, so reserve exploration.
- Offline NDCG and recall@k guide work, but only an online A/B test on CTR, watch time or retention decides.
Check yourself
Answer to mark it learned; your answers aren't scored.
Your video app retrains its ranker on clicks. Offline NDCG@10 rises 3%, but the A/B test shows retention flat: the model now favors clickbait abandoned after 10 seconds. What do you change?
Go deeper
Full explanation
A recommender picks the few items a user will most value from a catalog too large to score exhaustively, so it runs as a multi-stage funnel (Covington et al., YouTube, 2016):
- Candidate generation (millions → hundreds, milliseconds): several cheap retrievers in parallel. A two-tower model embeds users and items into one space and an ANN indexes: HNSW, IVF, PQ lookup finds the nearest items; add collaborative filtering, trending, 'more from this creator'.
- Ranking (hundreds → dozens): a GBDT or deep model scores each candidate with rich features (user history, item stats, context, crosses) to predict clicks, watch time or purchases.
- Re-ranking: diversity, freshness, dedup, business and safety rules.
Hard parts: cold start (no interactions yet: use content features and popularity), feedback loops (you only learn from what you showed, which amplifies popularity bias: reserve exploration), and metrics (NDCG and recall@k offline, but only an online A/B test on CTR, watch time or retention decides).
LLMs help with item understanding, cold-start embeddings and explanations; rarely as the ranker at scale.
Code Two-tower retrieval, then ranking and re-ranking
import faiss
# Offline: the item tower embeds the whole catalog; rebuild the index on refresh
item_vecs = item_tower(catalog_features).astype("float32") # (5M, 64)
faiss.normalize_L2(item_vecs)
index = faiss.IndexHNSWFlat(64, 32, faiss.METRIC_INNER_PRODUCT)
index.add(item_vecs)
def recommend(user, k: int = 20):
# Stage 1: user tower + ANN -> ~500 candidates in a few ms
u = user_tower(user_features(user)).astype("float32").reshape(1, -1)
faiss.normalize_L2(u)
_, ids = index.search(u, 500)
seen = seen_items(user)
cands = [i for i in ids[0] if i not in seen]
# Stage 2: heavy ranker (e.g. LightGBM or a DNN) only on the candidates
scores = ranker.predict(rank_features(user, cands))
ranked = [c for _, c in sorted(zip(scores, cands), reverse=True)]
# Stage 3: diversity and business rules
return rerank_diverse(ranked, max_per_creator=2)[:k]Real example
A video app with 10M items retrains its ranker on clicks. Offline NDCG@10 rises 3%, yet the A/B test shows retention flat: the model now favors clickbait abandoned after 10 seconds. The team switches the ranking objective to expected watch time, as YouTube did, and caps re-ranking at 2 items per creator. For new uploads with zero views, an LLM writes a description that is embedded into the item space, so they enter the candidate pool on day one.
Trade-offs +3 · −4
+ You gain
- The funnel makes millions of items tractable within a ~100–200 ms page budget.
- Each stage optimizes one thing: recall, then precision, then policy.
- Directly drives core business metrics: engagement, retention, revenue.
− You pay
- Feedback loops: the model learns only from what it showed, reinforcing popularity.
- Cold start for new users and new items needs separate strategies.
- Offline metrics often fail to predict online wins; you need A/B testing infrastructure.
- Many models, features and indexes to keep consistent: heavy operational load.
Use it when
Large catalogs where personalization moves engagement or revenue, and you have enough repeat interaction data to learn from.
Avoid it when
Small catalogs or low traffic: popularity lists, simple rules or item-to-item co-occurrence beat a deep funnel you can't even evaluate.
Numbers to know 3
- Covington et al. (YouTube, 2016): candidate generation narrows millions of videos to hundreds; ranking scores those to show dozens.
- Netflix (Gomez-Uribe & Hunt, 2015): recommendations influence ~80% of hours streamed; the other ~20% comes from search.
- Netflix Prize: $1M for a 10% RMSE gain (awarded 2009); the winning ensemble was never fully deployed, as the engineering cost outweighed the gain (Netflix Tech Blog, 2012).
Common pitfalls 4
- Optimizing CTR alone trains a clickbait machine; optimize watch time, purchases or retention.
- Random train/test splits leak the future; split by time.
- Shipping on an offline NDCG gain without an online A/B test.
- No exploration: new items never get impressions, so they never get data.
Interview questions 2
Answer out loud first, then reveal.
Why a multi-stage architecture instead of one great model?Show answer
Compute. Scoring 10 million items with a rich model per request can't fit a 200 ms budget. So I split by cost: candidate generation uses models whose item side is precomputed, like two-tower embeddings with ANN, co-occurrence and trending, to get a few hundred high-recall candidates in milliseconds. The ranker can then afford hundreds of features on just those, and re-ranking applies diversity and policy. The trade-off: the ranker can't recover what retrieval missed, so I monitor recall per source.
How do you handle cold start and popularity bias?Show answer
For new items I lean on content: text, images and LLM-written descriptions embedded into the same space, so they're retrievable on day one, plus a small exploration slot of impressions. For new users, contextual popularity and onboarding signals, switching to personalized as interactions accumulate. Popularity bias is a feedback loop, since we only learn from what we showed. I counter it with exploration traffic, logged propensities for off-policy correction, and diversity constraints in re-ranking.