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 500 s, far above interactive latency budgets of tens of milliseconds. A two-tower model factorises the score as a dot product ⟨u(x),v(y)⟩ between a user (or query) encoder u(x) and an item encoder v(y), so item embeddings v(y) can be precomputed offline and queried with approximate nearest neighbour (ANN) search in milliseconds, leaving only 1,000 candidates for the heavy ranker (50 ms).

Steps

  1. Compute the brute-force cost: scoring N=107 items at trank=50 μs=5×10−5 s per item takes 107×5×10−5 s=500 s sequentially, which is impossible for a real-time request.
  2. Split the funnel into retrieval and ranking: first narrow 107 items down to k=1,000 plausible candidates using a lightweight retrieval stage, then run the heavy ranker only on those 1,000 items, which takes 1,000×50 μs=50 ms (and even less when batched or parallelised across cores).
  3. Explain how a two-tower model enables precomputation: the user tower u(x)∈Rd (for embedding dimension d) depends only on user and context features x, and the item tower v(y)∈Rd depends only on item features y, with similarity score s(x,y)=u(x)⊤v(y). Because v(y) does not depend on x, all 107 item vectors are computed offline and indexed in an ANN (approximate nearest neighbour) structure.
  4. Trace the online retrieval step: at request time, the service runs only one forward pass of u(x) and performs a sub-linear ANN lookup (typically 5–20 ms) to fetch the top 1,000 items, cutting total scoring latency from 500 s to well under 100 ms.
  5. 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).
  6. 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 1,000 candidates. It orders them for the final page and can balance goals such as clicks and purchases.

Common mistakes

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?