AlgoViz

Design a Recommendation System

hard 55 min

Choose 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

0:00 / 3:54

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

  1. 1994

    GroupLens publishes collaborative filtering for Usenet news: people who agreed before help predict what others will like.

  2. 2003

    Linden, Smith and York publish Amazon's item-to-item collaborative filtering.

  3. 2006 to 2009

    The Netflix Prize offers one million dollars for a 10% better rating prediction. It makes matrix factorization famous.

  4. 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.

  1. 1ApproachWhat this question tests, where it gets hard, and how to spend the time.
  2. 2ScopeAsk what changes the design, then commit to a short list.
  3. 3NumbersWork out a number when the answer changes what you build.
  4. 4API and dataThe contract first, then the data that serves it.
  5. 5DesignStart with the smallest system that works. Change it only when something breaks.
  6. 6Deep divesCompare the options before you open them, then say which you would pick.
  7. 7Explain your choicesAnswer first. Then compare your reasoning.
  8. 8By levelThe same question is judged differently for mid, senior and staff.
  9. 9MockAnswer against the clock, then score yourself.

How it works

Step through the idea, or press Play.

Step 1 of 3

Narrow. 50,000 titles become 1,000 candidates

Understand it

The toy room with a helper
1,000 toys1,000 toys, one child

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 typeWhat 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

Viewers reach a gateway and the home service, which asks the candidate index, the ranker and the mixer. The ranker reads the feature store. Events go to Kafka, then Flink updates features and the event lake stores history. Training reads the lake and publishes models through a registry.
Blue is a page request, orange is what happens afterwards. Dashed means asynchronous.

Which page owns each idea

IdeaRead
Event logLog-based queues
Updating features in secondsStream processing
Serving when a part is slowFallbacks and Circuit breaker
Reading what was computed aheadCaching
Storing years of eventsObject storage
Why percentiles do not addLatency

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