Design a Recommendation System
hard 55 minChoose twenty titles out of fifty thousand for each viewer in a fraction of a second.
Video lesson · Chapter 1 of 5
Choose twenty titles before breakfast ends
Theory
Read or listen first. About 2 min.
What it is
A recommendation system picks a few items out of many for one person at one moment. It learns from what people did (watched, finished, skipped) and from what the items are (genre, length, cast).
It is two systems joined in a loop. The online half answers in milliseconds. The offline half studies the past and prepares what the online half needs. What the viewer does next feeds the offline half again.
Why it exists
Nobody can browse fifty thousand titles. Without help, people see what is popular, and most of the catalogue is never found.
It is a good interview problem because the model is only a part of it. The harder questions are how to spend a small time budget, what to do with no history, how to know whether a change helped, and how to stay up when the model is slow.
A short history
1994
GroupLens publishes collaborative filtering for Usenet news: people who agreed before help predict what others will like.
2003
Linden, Smith and York publish Amazon's item-to-item collaborative filtering.
2006 to 2009
The Netflix Prize offers one million dollars for a 10% better rating prediction. It makes matrix factorization famous.
2016
A YouTube paper describes two stages: candidate generation, then ranking. The HNSW search index and Google's Wide & Deep paper appear the same year.
Words to know
- Candidate generation
- Cheaply narrowing the whole catalogue to several hundred or a thousand items.
- Ranking
- Scoring the candidates carefully with many features.
- Re-ranking
- Changing the order for variety, freshness and rules.
- Embedding
- A list of numbers that places users and items so that good matches are close.
- Feature store
- The place where training and serving read the same inputs.
- Cold start
- A new viewer or title with no history.
- Position bias
- Top places get more clicks simply because they are on top.
- Impression
- One item shown to one person.
Solve it like an interview
New to it? Start with Approach. Revising? Go straight to the mock.
- 1ApproachWhat this question tests, where it gets hard, and how to spend the time.
- 2ScopeAsk what changes the design, then commit to a short list.
- 3NumbersWork out a number when the answer changes what you build.
- 4API and dataThe contract first, then the data that serves it.
- 5DesignStart with the smallest system that works. Change it only when something breaks.
- 6Deep divesCompare the options before you open them, then say which you would pick.
- 7Explain your choicesAnswer first. Then compare your reasoning.
- 8By levelThe same question is judged differently for mid, senior and staff.
- 9MockAnswer against the clock, then score yourself.
How it works
Step through the idea, or press Play.
Narrow. 50,000 titles become 1,000 candidates
Understand it
Narrow, score, mix, learn. Every recommender is a version of this room.
Scope
What are we building?
The personal home page of a video streaming service: rows such as "Top picks", "Because you watched" and "Trending". The same parts serve "More like this" and can reorder search results.
Choose
Twenty titles for this viewer, now, in under 200 ms.
Explain
A short and true reason for each row.
Learn
From watching, skipping and finishing, within seconds and overnight.
Similar problems
| Product type | What differs |
|---|---|
| Film and series (Netflix) | Small catalogue, long watches |
| Short video (YouTube, TikTok) | Huge catalogue, minutes-old items matter |
| Music (Spotify) | People repeat the same item happily |
| Shopping (Amazon) | Price and stock; buying twice is rare |
| Pins and photos (Pinterest, Instagram) | Pictures carry the meaning |
| Jobs and people (LinkedIn) | Both sides must agree; see Connection graph |
The finished system
Where the design ends up
This is the last version, not the first. The Design tab starts with one service and a popular list.
The whole system
Which page owns each idea
| Idea | Read |
|---|---|
| Event log | Log-based queues |
| Updating features in seconds | Stream processing |
| Serving when a part is slow | Fallbacks and Circuit breaker |
| Reading what was computed ahead | Caching |
| Storing years of events | Object storage |
| Why percentiles do not add | Latency |
Summary
The whole topic in one minute.
A recommendation system narrows, scores and mixes. Candidate generation must not miss good items, ranking must order them well, and the last step keeps the page varied and legal to show.
Our design grows from a popular list to nightly similar titles, then two stages, fresh events, a mixer with fallbacks, and a safe training loop. At 50,000 titles an exact search is enough; the numbers say when that changes.
- Three steps: candidates for recall, ranking for precision, mixing for the whole page.
- Log what you showed, not only what was clicked.
- Training and serving must see the same features, as they were at that moment.
- A popular list by region is always ready, so a slow model never means an empty page.
- Cold start is solved with content, popularity and a little exploration.
- Only an experiment tells you whether a change helped.
Sources
- Covington, Adams, Sargin: Deep Neural Networks for YouTube Recommendations (2016)
- Linden, Smith, York: Amazon.com Recommendations, item-to-item collaborative filtering (2003)
- Koren, Bell, Volinsky: Matrix Factorization Techniques for Recommender Systems
- Malkov, Yashunin: Hierarchical Navigable Small World graphs
- Cheng et al.: Wide & Deep Learning for Recommender Systems
- pgvector: exact and approximate search in PostgreSQL
- Apache Flink: fault tolerance and exactly-once