AlgoViz

Design a Recommendation System

hard 55 min

Choose 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

Our systemViewerAPI gatewayHome serviceFeature storeCandidatesvectors, listsRankerMixerPopular listsfallback

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

Our systemViewerAPI gatewayHome serviceFeature storeCandidatesvectors, listsRankerMixerPopular listsfallback

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

    major

    Fresh features change nothing until it expires, and finished titles stay.

    Wrong
    cache.set(viewer, page, ttl=300)
    Right
    cache.set(viewer, candidates, ttl=300)page = mix(rank(filter_watched(candidates)))
  • Logging only clicks

    critical

    The model never sees what was shown and ignored.

    Wrong
    if clicked: log(item)
    Right
    log(page_id, items, positions, model)# clicks join by page_id later
  • Random split of events

    critical

    Tomorrow's behaviour leaks into training, and the result looks far too good.

    Wrong
    train, test = random_split(rows)
    Right
    train = rows[rows.day < D]test = rows[rows.day >= D]
  • ef smaller than k

    major

    The search list cannot hold as many results as you ask for.

    Wrong
    index.search(q, k=1000, ef=500)
    Right
    index.search(q, k=1000, ef=1200)
  • No deadline

    critical

    One slow model then makes every page slow.

    Wrong
    s = ranker.score(c)
    Right
    s = 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.