Why do large recommenders and search engines split prediction into candidate generation (retrieval) followed by ranking? Suppose a catalogue has 10 million items and a heavy ranking model takes 50 µs per item; how does scoring the whole catalogue compare with using a two-tower model and approximate nearest neighbour (ANN) search to retrieve 1,000 candidates first?
ML system design, Retrieval and ranking
Level L3 Intermediate. Length Medium. Nature Mat Mathematical.
What's being asked
Whether you can quantify the latency bottleneck of scoring a massive catalogue with an expressive model, and explain why a two-tower architecture enables fast retrieval while needing a second-stage ranker.
Key idea
Scoring 10 million items sequentially at 50 µs each takes , far above interactive latency budgets of tens of milliseconds. A two-tower model factorises the score as a dot product between a user (or query) encoder and an item encoder , so item embeddings can be precomputed offline and queried with approximate nearest neighbour (ANN) search in milliseconds, leaving only candidates for the heavy ranker ().
Steps
- Compute the brute-force cost: scoring items at per item takes sequentially, which is impossible for a real-time request.
- Split the funnel into retrieval and ranking: first narrow items down to plausible candidates using a lightweight retrieval stage, then run the heavy ranker only on those items, which takes (and even less when batched or parallelised across cores).
- Explain how a two-tower model enables precomputation: the user tower (for embedding dimension ) depends only on user and context features , and the item tower depends only on item features , with similarity score . Because does not depend on , all item vectors are computed offline and indexed in an ANN (approximate nearest neighbour) structure.
- Trace the online retrieval step: at request time, the service runs only one forward pass of and performs a sub-linear ANN lookup (typically ) to fetch the top items, cutting total scoring latency from to well under .
- Explain what the two-tower model cannot capture: because the user and item towers interact only through a single dot product at the end, the retriever cannot model fine-grained user–item feature crosses (such as exact query-to-title token matches, whether the user has recently bought that brand, or cross-attention over user history).
- Explain why the heavy ranker exists: the second-stage ranker can use rich features that combine the user and the item, plus fresh session information, because it only scores the candidates. It orders them for the final page and can balance goals such as clicks and purchases.
Common mistakes
- Assuming a two-tower model can directly take user–item cross features; any feature combining user and item breaks the factorisation and prevents precomputing .
- Confusing recall with ranking precision: the retrieval stage only needs high recall (not missing good items among the it returns) so that the true relevant items reach the ranker, whereas exact ordering happens in the ranking stage.
- Ignoring the offline index refresh lag: newly added items cannot be retrieved until their item embeddings are computed and inserted into the ANN index.
How long your answer should be
About 3 minutes; two latency numbers, two-tower factorisation, and the cross-feature trade-off.
Read this
Previous in ML system design: Why must training features be joined “as of” each label’s timestamp, and how does a point-in-time join work? Using a small user timeline, why do naive joins inflate offline metrics and then fail online, and what is one serving-time alternative?
Next in ML system design: How much memory does an approximate nearest neighbour (ANN) index of 100 million 256-dimensional item embeddings require in fp32 (32-bit floating point), in fp16 (16-bit floating point), and with product quantisation (PQ) using 32 sub-quantisers of 1 byte each? How does PQ achieve this compression, and what are the trade-offs?