Level 0: Foundation
★☆☆☆☆ Introductory⏱ 5 minGeneral audience. No mathematics required.The library and the librarian
A library with millions of books cannot have its best librarian read every book for every visitor. A quick catalogue search first narrows the shelves to a few hundred candidates; the librarian then reads those and recommends ten. If the catalogue search misses the right books, no librarian can recommend them; if it returns too many, the visitor waits. Designing an ML system is deciding how such stages split the work, and which stage to improve.
Recall of the true top 10: 0.440 · quality of the 10 shown: 0.9074 · latency 10.0 ms
At 100 candidates, what each change is worth:
Replacing a stage with a perfect one (an oracle ablation) shows the headroom: here retrieval, not the ranker, limits the system.
In Exercise 2, a perfect ranker would have raised quality from 0.907 to 0.928, but a perfect retrieval stage to 0.966: the system was limited by what it retrieved, not by how it ranked.
- 1. A two-stage recommender first…
- 2. If retrieval misses an item, the ranker…
- 3. System design starts from…
Level 1: Beginner
★★☆☆☆ Basic⏱ 20 minSecondary-school mathematics: arithmetic and elementary algebra.By hand
- Requirements: the business goal and its metric, latency budget (often p99), throughput (queries per second at peak), cost, freshness, and constraints such as privacy and policy.
- Multi-stage architecture: candidate generation (retrieval, often a two-tower embedding model with approximate nearest-neighbour search), filtering (eligibility, policy), ranking (a heavier model over hundreds of candidates), and re-ranking with business rules (diversity, freshness).
- Recall at K: the share of the truly best items among the K retrieved candidates; it caps what the ranker can achieve.
- Oracle ablation: replace one stage with a perfect one to measure the headroom it limits.
- Feedback loop: the system’s outputs decide which data are logged, so its training data depend on its past decisions; exploration counters this.
- Fallbacks: a cheaper answer when a stage times out or fails.
- Freshness: how quickly new data reach features, indexes and models.
- 1. Recall at K measures…
- 2. An oracle ablation…
- 3. A feedback loop arises because…
Level 2: Intermediate
★★★☆☆ Intermediate⏱ 25 minUndergraduate students and practising engineers.A two-stage recommender
Exercise 1.
- 300 users and 20,000 items with 16-dimensional latent vectors; true relevance = user·item + item quality.
- Retrieval: a small embedding that captures only 8 of the 16 directions, plus noise 0.4; cheap enough to score everything.
- Ranker: the full relevance plus noise 0.3, costing 0.04 ms per candidate.
- Pipeline: retrieve K candidates, rank them, show the top 10. Quality = mean true relevance of the shown items relative to the best possible 10.
Practice.
- Write the requirements as numbers (QPS, p99, cost, freshness) and size the system before building it.
- Measure each stage (recall of retrieval, precision of ranking) and run oracle ablations to find the bottleneck.
- Fit stages to the latency budget, with fallbacks for timeouts and failures.
- Plan for feedback loops: log what was shown, explore a little, and evaluate with that in mind.
- Design for change: index and feature freshness, retraining, and monitoring from day one.
def recommend(user):
cands = ann_index.search(user_tower(user), K) # recall stage, ~5 ms
cands = [c for c in cands if eligible(c)] # policy filter
scores = ranker(user, cands) # precision stage, ~0.04 ms each
top = rerank_with_rules(cands, scores)[:10] # diversity, freshness
log(user, top) # tomorrow's training data
return top- 1. Retrieving more candidates…
- 2. Without exploration, a recommender…
- 3. Under a tight latency budget, the best number of candidates is…
Level 3: Advanced
★★★★☆ Advanced⏱ 25 minGraduate level, with full derivations and proofs.Theory
Stages multiply. An item reaches the user only if it passes every stage: the probability that one of the truly best items is shown is roughly the product of the filter’s pass rate, the retrieval recall at K and the ranker’s precision among the candidates. A stage with recall 0.6 caps the system whatever follows it, which is why oracle ablations, replacing one stage by the truth, are the quickest way to find the bottleneck.
Latency budgets add. End-to-end latency is the sum of stage latencies, and for tails the slowest stage dominates. With a ranker costing c per candidate, the budget B allows K ≤ (B − fixed costs)/c on average, and less at the p99. When the budget is exceeded, a fallback (cached results, the retrieval order, a popular list) keeps the service up at lower quality.
Back-of-envelope sizing. Memory = number of vectors × dimension × bytes per number; brute-force retrieval costs 2 × items × dimension floating-point operations per query; servers = peak QPS × work per query / (capacity × utilisation). Such arithmetic decides early whether a design fits on one machine, needs approximate search or needs sharding.
Feedback loops. Training data logged from a system’s own choices are a biased sample: items never shown have no labels, so a greedy system cannot discover that they are good. Exploration, randomized positions and inverse-propensity weighting give the data needed to learn and to evaluate new policies offline.
- 1. With filter pass rate 0.9 and retrieval recall 0.6, at most what share of the best items can be shown?
- 2. Brute-force retrieval costs per query about…
- 3. Inverse-propensity weighting helps to…
Level 4: Expert
★★★★★ Expert⏱ 30 minResearchers: original papers and open problems.In a two-stage recommender, how do the number of candidates and the quality of each stage determine end-to-end quality and latency, and which stage is worth improving?
History.
- Multi-stage ranking grew in web search: cheap index lookups followed by learned rankers (for example C. Burges and colleagues’ RankNet, 2005, and LambdaMART, 2010).
- P. Covington, J. Adams and E. Sargin (2016) described YouTube’s deep candidate-generation and ranking networks, the template of modern recommenders; X. Yi and colleagues (2019) the two-tower retrieval model with sampling-bias correction.
- D. Sculley and colleagues (2015), “Hidden technical debt in machine learning systems,” showed that the model is a small part of the system and named feedback loops among its hidden costs.
- Approximate nearest-neighbour libraries such as FAISS (J. Johnson, M. Douze and H. Jégou, 2017) and ScaNN (2020) made billion-item retrieval practical.
Exercise 2.
- Latency: 1 ms network + 5 ms retrieval + 0.04 ms per candidate ranked.
- Number of candidates K from 10 to 3,000; at K = 100, oracle ablations (perfect retrieval, perfect ranker, both) and two realistic upgrades (a retrieval embedding with 12 directions, a ranker with half the noise).
Seed 0:
| K | recall of true top 10 | quality of shown 10 | latency ms |
|---|---|---|---|
| 10 | 0.1510 | 0.6961 | 6.4 |
| 30 | 0.2670 | 0.8414 | 7.2 |
| 100 | 0.4403 | 0.9074 | 10.0 |
| 300 | 0.6550 | 0.9410 | 18.0 |
| 1,000 | 0.8393 | 0.9571 | 46.0 |
| 3,000 | 0.9510 | 0.9629 | 126.0 |
At K = 100 (quality): real system 0.9074; perfect ranker 0.9276; perfect retrieval 0.9655; both perfect 1.0000; ranker upgrade 0.9214; retrieval upgrade 0.9497 (recall 0.7100).
Seeds 1 and 2: real system at K = 100 0.9138 and 0.9119; perfect ranker 0.9370 and 0.9358; perfect retrieval 0.9639 and 0.9640; ranker upgrade 0.9310 and 0.9299; retrieval upgrade 0.9522 and 0.9489. Quality at K = 1,000: 0.9561 and 0.9571.
Findings.
- Quality rose steeply up to a few hundred candidates and then flattened, while latency grew linearly: K = 1,000 fit a 50 ms budget, K = 3,000 did not.
- A perfect retrieval stage was worth more than twice a perfect ranker in every seed (about 5 against 2 points).
- The realistic retrieval upgrade beat the ranker upgrade (about 3.7 to 4.2 against 1.4 to 1.8 points): effort should go where the ablation shows headroom.
End-to-end systems in practice
- Design documents start from requirements and back-of-envelope numbers, then propose stages, data flows, failure modes and metrics, and are reviewed before code is written.
- Recommenders and search engines at large companies use three or four stages (retrieval from several sources, pre-ranking, ranking, re-ranking), each with its own models, budgets and owners.
- Logging and evaluation keep the propensities of shown items so that new policies can be evaluated offline and feedback loops measured.
- Earth-observation platforms follow the same pattern: cheap screening of every scene (cloud masks, change detection), heavy models on the candidates, and products whose freshness is set by the downlink and processing schedule.
References
- D. Sculley and colleagues (2015). “Hidden technical debt in machine learning systems.” NeurIPS.
- P. Covington, J. Adams and E. Sargin (2016). “Deep neural networks for YouTube recommendations.” RecSys.
- J. Johnson, M. Douze and H. Jégou (2017). “Billion-scale similarity search with GPUs.” arXiv (FAISS).
- X. Yi and colleagues (2019). “Sampling-bias-corrected neural modeling for large corpus item recommendations.” RecSys.
- C. Burges (2010). “From RankNet to LambdaRank to LambdaMART: an overview.” Microsoft Research technical report.
- C. Huyen (2022). Designing Machine Learning Systems. O’Reilly.