S30 Logo
S30 AI Labwww.thes30.com
Back
#66

Design Real-Time Recommendation System

Expert🏗️ System DesignW14 D1

Before You Start

Required knowledge
Recommender retrieval/ranking, freshness, evaluation, and system design.
Libraries used
Python standard library only
Dataset and source
A recommendation-system scenario and design dictionaries supplied in the notebook.
Expected output
A real-time recommendation design covering data/labels, retrieval, ranking, monitoring, and constraints.

Design Real-Time Recommendation System — Theory & Concepts

Read this before attempting the exercises. A strong real-time recommender answer is mostly about system decomposition: retrieval, ranking, freshness, safety, and latency budgeting.


1. What Are You Actually Designing?

A home-feed recommender is not one model.

It is a multi-stage serving system:

Request
  -> candidate generation
  -> ranking
  -> re-ranking / policy filters
  -> response

The main reason is scale:

  • ●the corpus may contain millions of items
  • ●you cannot score every item with a heavy model on every request
  • ●new content must appear quickly
  • ●safety and spam filtering must happen before the user sees the feed

So the standard answer is:

Large corpus
   -> fast retrieval gets ~1k candidates
   -> richer ranker scores those candidates
   -> final re-ranker applies diversity, freshness, and business rules

2. Requirements and Success Metrics

For a real-time feed, the primary objective is usually a long-term engagement metric, not just CTR.

Examples:

  • ●watch time
  • ●session depth
  • ●retained sessions
  • ●long-term user value

Guardrails are equally important because optimizing only clicks can create spammy or addictive behavior.

Typical guardrails:

  • ●hide / report rate
  • ●not-interested rate
  • ●creator concentration
  • ●content diversity
  • ●freshness coverage
  • ●policy violation rate
  • ●latency SLO violations

You should explicitly separate:

Primary online metric:
  the main thing to optimize

Guardrails:
  metrics that must not regress while improving the primary metric

3. Latency Budgeting

The prompt gives a p95 end-to-end latency target of 200 ms.

A strong answer allocates that budget across stages.

Example:

StageExample Budget
API / auth / request parsing10 ms
candidate generation40 ms
online feature fetch35 ms
ranking60 ms
re-ranking + policy filters20 ms
response serialization + network slack35 ms

This does two things:

  1. ●It shows you understand that latency is a system budget, not just a model metric.
  2. ●It forces design choices. A slow cross-encoder ranker may simply not fit.

4. Data, Labels, and Feature Freshness

A recommender is only as good as its events and features.

Core events to log

  • ●impression
  • ●click
  • ●dwell time / watch time
  • ●like / save / share
  • ●hide / not interested
  • ●report / safety action

Labels

You usually need both short-term and delayed labels:

  • ●short-term labels: click, short watch, dwell
  • ●delayed labels: long watch, completion, next-day return, purchase, unsubscribe risk

This matters because a feed often optimizes a combination of:

immediate engagement
+ long-term satisfaction

Feature freshness tiers

A strong answer distinguishes:

TierExamplesTypical Use
Realtimecurrent session actions, recent clicks, recent hidesrequest-time personalization
Nearlinerolling counters over minutes / hoursfresh aggregates
Batchstable profile features, long-term embeddingscheaper offline computation

Point-in-time correctness

When training, features must reflect only what was known at prediction time.

That means:

  • ●store timestamps for events and features
  • ●build training examples using time-travel or point-in-time joins
  • ●avoid leakage from future interactions

If you do not mention this, the design is incomplete.


5. Candidate Generation

Candidate generation must be high recall and low latency.

Typical sources:

Two-tower retrieval

user tower -> user embedding
item tower -> item embedding
ANN search returns nearest items

This is the default answer for large-scale retrieval because it supports millions of items.

Graph / collaborative retrieval

Examples:

  • ●users similar to you liked this
  • ●item-to-item neighbors
  • ●follow-graph or creator-graph expansion

Freshness injection

Pure ANN retrieval often favors older, well-observed items.

So a strong answer adds:

  • ●recent uploads
  • ●trending items
  • ●creator-follow candidates
  • ●exploration bucket

This is how you avoid stale feeds.

Early safety filtering

Safety should not be left only to the final stage.

You usually want:

  • ●blocked or deleted content removed before retrieval or immediately after
  • ●NSFW / policy classifiers as hard filters
  • ●spam suppression before expensive ranking

6. Ranking and Re-Ranking

After retrieval, ranking uses richer features and a more expressive model.

Typical ranker inputs:

  • ●user features
  • ●item features
  • ●context features
  • ●user-item cross features
  • ●recent session signals
  • ●safety and quality signals

Typical model families:

  • ●GBDT / tree models for fast tabular ranking
  • ●MLP / DLRM-style rankers for dense + sparse features
  • ●multi-task models predicting CTR, dwell, long-watch, hide risk

The ranking score is often not just one probability.

Example:

score
= 0.45 * P(long_watch)
+ 0.25 * P(click)
+ 0.15 * P(share)
- 0.10 * P(hide)
- 0.05 * policy_risk

Re-ranking

The final stage is usually rule-based or lightweight optimization over the top few hundred items.

Common goals:

  • ●diversity across creators / categories
  • ●freshness
  • ●deduplication
  • ●policy constraints
  • ●sponsored-content limits

This stage exists because the highest-scoring items are often too similar.


7. Serving Path and Caching

A production answer should mention where latency is saved.

Useful caches:

  • ●user embedding cache
  • ●candidate cache for very recent requests
  • ●feature cache for hot items
  • ●score cache for repeated contexts when safe

But caches create freshness risk.

So the tradeoff is:

more caching -> lower latency
less caching -> fresher personalization

Fallbacks are also important:

  • ●if ANN is unavailable, use trending / popular / followed creators
  • ●if online features fail, use batch profile features
  • ●if ranker times out, use a lightweight pre-ranker

Interview answers are stronger when they include degraded-but-safe behavior.


8. Monitoring, Experimentation, and Rollback

You should monitor more than model accuracy.

Online monitoring

  • ●p50 / p95 / p99 latency
  • ●retrieval recall proxies
  • ●click / watch / hide / report rates
  • ●freshness coverage
  • ●creator concentration
  • ●null-feature rate
  • ●cache hit rate

Experimentation

Typical setup:

  • ●A/B test against current feed
  • ●compare primary metric and guardrails
  • ●segment by new users, power users, creators, geography, device

Rollback

Plan for:

  • ●bad model push
  • ●feature pipeline outage
  • ●ANN index corruption
  • ●policy filter failure

Common rollback actions:

  • ●revert to previous model version
  • ●switch traffic to previous index
  • ●disable new feature set
  • ●fall back to safe popularity-based feed

9. Common Tradeoffs

TradeoffWhy It Exists
Relevance vs diversitybest-scoring items may be too similar
Freshness vs engagement confidencenew items have little history
Personalization vs latencyricher request-time features cost time
Exploration vs exploitationshowing only proven items limits discovery
Engagement vs quality / safetyclickbait can win short-term and harm long-term value

A strong design explicitly names these tradeoffs instead of pretending there is one perfect metric.


10. A Strong 5-Minute Interview Answer

If asked to summarize quickly, the structure should sound like:

1. Define objective and guardrails.
2. Split the system into retrieval, ranking, and re-ranking.
3. Use ANN / two-tower retrieval to get ~1k candidates within ~50 ms.
4. Use fresh online features plus stable batch features for ranking.
5. Re-rank for diversity, freshness, and safety.
6. Monitor latency, engagement, reports, freshness, and rollback readiness.

That is the core answer.


Now you're ready to attempt the exercises. Switch to the Exercise tab and start coding!

Ready to practice?

Python 3 — Notebook
0/6 solvedSubstack Notes
1
Dataset & Setup

Design Real-Time Recommendation System — Student Lab

Offline system design lab: structure a real-time home feed recommender under strict latency, freshness, and safety constraints.

Run the tasks in order. Each section adds one part of the final design doc.

Loading editor...
2
0 — Requirements and Metrics

0 — Requirements and Metrics

Fill the system objective, the main online metric, 4–8 guardrails, a p95 latency budget, and offline/online evaluation plans.

Loading editor...
3
1 — Data, Labels, and Feature Freshness

1 — Data, Labels, and Feature Freshness

Specify the events to log, the labels to train on, feature tiers by freshness, and how you avoid point-in-time leakage.

Loading editor...
4
2 — Candidate Generation (Retrieval)

2 — Candidate Generation (Retrieval)

Design a retrieval stage that can return about 1k candidates in under 50 ms. Include multiple sources, freshness injection, early safety filtering, and fallbacks.

Loading editor...
5
3 — Ranking and Serving Architecture

3 — Ranking and Serving Architecture

Design the main ranking stage, how features are fetched online, which caches are useful, which re-ranking rules are applied, and what the fallbacks are if serving fails.

Loading editor...
6
4 — Experimentation, Monitoring, Rollback

4 — Experimentation, Monitoring, Rollback

Add an experiment plan, the core dashboards and alerts, concrete rollback actions, and then write a 5-minute summary of the entire design.

Loading editor...

Need help? Share feedback