Stop 19 · System Design for AI ApplicationsTopics · 6/9

Designing a Recommendation System

Part of the AI Engineer Roadmap

In short

Every feed you scroll is a recommendation system choosing a few items from millions in milliseconds. The trick is doing it in stages: quickly narrow down, then carefully rank.

A classic ML system design question — architecting a system that ranks and personalizes content for users at scale, balancing relevance, freshness and diversity.

The blob as a shopkeeper spreads a few colourful folded fabrics on a counter in front of tall shelves full of cloth.

Think of it like this

The sari shop salesman

You walk into a sari shop with thousands of saris. The salesman pulls 20 from the shelves based on your budget and taste, then lays out the best 5, and slips in one new style you might like. That's retrieval, ranking and a little diversity.

Watch it happen

From millions of items to a feed of 10

Candidates
Filter
Rank
Re-rank
Serve

1/5Cheap retrieval pulls about 1,000 candidates from millions, using embedding similarity, popular items and what friends liked.

The big ideas

What you need to know

Two-stage design

Fast retrieval narrows millions to thousands. A slower, smarter ranker orders the shortlist.

Embeddings for users and items

Learn vectors so that users sit close to items they like. Nearest-neighbour search then finds candidates.

Cold start

New users and new items have no history. Use popularity, onboarding questions or item content to start.

Diversity and freshness

Pure relevance gets repetitive. Mix in variety and new items so the feed doesn't get stuck.

See it in code

Walk through the code

python
1import numpy as np2 3items = {"i1": [0.9, 0.1], "i2": [0.8, 0.3], "i3": [0.1, 0.9], "i4": [0.7, 0.2]}4user = np.array([1.0, 0.2])5seen = {"i1"}6 7def score(vec):8    v = np.array(vec)9    return float(user @ v / (np.linalg.norm(user) * np.linalg.norm(v)))10 11candidates = [i for i in items if i not in seen]12ranked = sorted(candidates, key=lambda i: score(items[i]), reverse=True)13print(ranked[:2])

1/4Tiny 2-D embeddings for items and one user.

A toy version: filter seen items, then rank by cosine similarity between user and item vectors. Real systems use learned embeddings and an ANN index.

Save yourself the pain

Common mistakes

Oops

Running the heavy ranking model on all million items per request.

Do this instead

Retrieve a shortlist cheaply first. Rank only a few hundred or thousand candidates.

Oops

Optimising only for clicks.

Do this instead

Clicks reward clickbait. Balance with watch time, purchases, satisfaction and diversity.

Your mission

Build a mini recommender

~45 min

Quick check

Why split recommendation into retrieval and ranking stages?

Go deeper

  1. 1Practice designing a recommendation system for a concrete product (e.g. video or product recs).
  2. 2Learn the standard candidate-generation-then-ranking two-stage pattern.
  3. 3Consider cold-start problems for new users/items in your design.

More topics at this stop

Stuck on this topic? Ask an Insider

Get 1:1 guidance from people who've walked this exact path — on the InsideEdge app.

Download
InsideEdge