Design a Recommendation System
hard 55 minChoose twenty titles out of fifty thousand for each viewer in a fraction of a second.
See it run
One page request, one event, the training behind them, and what happens when a part is slow.
Operations
Open the home page
From 50,000 titles to 20 in about 50 ms.
Step 1 of 15
The app opens
device
tv
time
20:14
It asks for the home page with device and time. Why: an evening on a television is not a morning on a phone.
Step 1: The app opens
def home(viewer, ctx): f = features.get(viewer) or DEFAULTS c = cache.get(viewer) or candidates(f, 1000) c = [t for t in c if allowed(t, ctx) and t not in f.watched] try: s = ranker.score(f, c, ctx, deadline=0.080) except (Timeout, BreakerOpen): s = order_given(c) or last_page(viewer) or popular(ctx.region) rows = mix(s, ctx) log_impressions(viewer, rows) return rows[:20]- Time
- p50 50 ms
- Size
- 5,800/s
- Peak (3x)
- 17,400/s
- p99
- 200 ms
The watched filter and the scoring run on every request; only candidates are cached.
In the real world
The same recommendation system doing real jobs. Pick one and step through it.
Scoring takes twice as long tonight.
Step 1 of 4
The deadline passes
waited
80 ms
80 ms are gone and no scores have come.
Step 1: The deadline passes
Common mistakes
The bugs interviewers watch for, with the fix beside each.
Caching the finished page
majorFresh features change nothing until it expires, and finished titles stay.
Wrongcache.set(viewer, page, ttl=300)Rightcache.set(viewer, candidates, ttl=300)page = mix(rank(filter_watched(candidates)))Logging only clicks
criticalThe model never sees what was shown and ignored.
Wrongif clicked: log(item)Rightlog(page_id, items, positions, model)# clicks join by page_id laterRandom split of events
criticalTomorrow's behaviour leaks into training, and the result looks far too good.
Wrongtrain, test = random_split(rows)Righttrain = rows[rows.day < D]test = rows[rows.day >= D]ef smaller than k
majorThe search list cannot hold as many results as you ask for.
Wrongindex.search(q, k=1000, ef=500)Rightindex.search(q, k=1000, ef=1200)No deadline
criticalOne slow model then makes every page slow.
Wrongs = ranker.score(c)Rights = ranker.score(c, deadline=0.08)# on timeout: candidate order
Interview tips
Lead with numbers
- "17,400 requests a second at peak."
- 1,000 candidates each: 17.4M scores a second.
- Title vectors are only 51 MB.
Explain trade-offs
- Exact search until a million items.
- Trees first, networks when they win.
- Cached candidates, fresh scores.
Anticipate failures
- Ranker late: candidate order.
- Features missing: defaults.
- Bad data: alarms on fields, not only errors.
Close the loop
- Log impressions with model and place.
- Explore with recorded chances.
- Dark run, 5%, steps, way back.