Design a Recommendation Engine
Architect a production recommendation system combining offline collaborative filtering, real-time feature pipelines, two-tower neural network retrieval, and a lightweight ranking layer — deployed at billions-of-events scale.
A recommendation engine is the highest-leverage system in consumer tech — Netflix estimates 80% of content watched is discovered through recommendations. The design challenge is that the problem spans three completely different engineering disciplines: distributed systems (real-time feature serving), machine learning (model training and serving), and data engineering (event pipelines and feature stores). Staff-level candidates are expected to reason across all three. The interview question is deceptively open-ended: “design a recommendation system” can mean anything from a simple item-to-item collaborative filter to a multi-billion-parameter deep learning stack. The first thing a senior candidate does is reduce that scope by asking precise clarifying questions. The second thing they do is identify which design axes — latency, freshness, cold start, scale, diversity — actually constrain the architecture, and make explicit trade-offs among them. This article walks through the full design, including the pieces that most candidates skip: the feature store, the feedback loop, cold-start handling, and post-ranking business logic.
Clarifying Questions
Before drawing any boxes on the whiteboard, establish the shape of the problem. The answers will change every architectural decision that follows.
| Question | Why It Matters |
|---|---|
| What type of content are we recommending — videos, music tracks, products, articles? | Determines embedding modality (ResNet for images, BERT for text, learned IDs for products) and interaction signal semantics (watch time is meaningful for video, not for products). |
| How do we handle cold start for new users who have no history? | Drives whether we need demographic-based persona clusters, a popularity fallback, or an explicit onboarding flow. |
| How do we handle cold start for new items with no interactions? | Determines whether we need content-based embeddings from metadata or an exploration injection strategy. |
| What is the latency SLA for returning recommendations? | Separates online (< 100ms) from near-line (< 1s, acceptable for pre-computed feeds) from batch (nightly pre-generation) architectures. |
| Are recommendations generated online per request, or pre-computed and cached? | Pre-computation dramatically simplifies serving but sacrifices real-time session signals; online generation adds latency complexity. |
| What interaction signals are available — explicit (thumbs up/down) or implicit (clicks, watch time, skips)? | Implicit signals are noisier but far more abundant; the model design and negative sampling strategy differ significantly. |
| Do we need diversity or serendipity constraints — no 10 identical categories in a row? | Post-ranking diversity filters add complexity; if required, they become a hard constraint, not a nice-to-have. |
| Is there an A/B testing framework, and must recommendations be experiment-aware? | Requires the serving layer to route users to model variants and log the model version alongside every impression. |
| What is the expected model refresh cadence — hours, days, real-time? | Drives the training infrastructure: daily batch retraining vs. continuous online learning vs. incremental updates. |
| What is the catalog size? | 10K items (small startup) vs. 10M items (mid-scale) vs. 1B items (e-commerce at Amazon scale) sets the vector index strategy entirely. |
For this design we assume: video content, 100M DAU, 10M items in catalog, online recommendation serving at < 100ms p99, implicit signals (clicks/watch/skips), diversity required, daily + hourly retraining, and a pre-existing A/B framework.
Functional Requirements
- Generate personalized recommendations for each active user, returning a ranked list of top-K items tailored to their long-term preferences and current session context.
- Handle cold start for new users with no interaction history — fall back to demographic-based persona clusters and trending content, and transition to personalized retrieval once at least 5 interactions are observed.
- Handle cold start for new items with no collaborative embedding — use content-based embeddings derived from metadata (title, description, thumbnail) until sufficient interactions accumulate.
- Incorporate real-time session signals: items watched or clicked in the current session should immediately influence the next recommendation request, within a 30-second propagation window.
- Exclude already-consumed items from recommendations — if a user has watched a video in the last 30 days, it must not appear in their feed.
- Enforce diversity constraints: no more than 2 items from the same category in any 10-item recommendation window.
- Provide explainability hooks: each recommended item should carry a reason string (“Because you watched X”, “Trending in your region”) for use in the UI.
- Support multiple recommendation contexts: homepage feed, “more like this” (item-to-item), end-of-content next-up, and email digest.
Non-Functional Requirements
- Scale: 100M daily active users, 10M items in catalog.
- Recommendation latency: < 100ms p99 for online serving. This is the hardest constraint and drives the two-stage architecture.
- Throughput: 50K recommendation requests/sec at peak (roughly 5× average load).
- Model retraining: daily full retrain of embeddings + hourly incremental updates to the ranking model’s feature weights.
- Feature freshness: real-time interaction signals reflected in recommendations within 30 seconds.
- Availability: 99.99% uptime — recommendations should degrade gracefully (trending fallback) rather than return errors.
- Storage: interaction logs retained for 90 days; embeddings refreshed on each model version; consumed-item sets retained per user for 30 days.
- Consistency: eventual consistency is acceptable — a video watched 10 seconds ago may reappear briefly before the consumed-item set is updated.
API Design
GET /api/v1/recs/{userId}?context=homepage&limit=20
→ { items: [{itemId, score, reason}], requestId, modelVersion }
GET /api/v1/recs/{userId}/similar?itemId=X&limit=10
→ Item-to-item recommendations ("More like this")
POST /api/v1/events (internal, from client SDK)
body: { userId, itemId, eventType: CLICK|WATCH|SKIP|LIKE, timestamp, sessionId }
→ 202 Accepted — feedback ingestion endpoint; async processing via Kafka
GET /api/v1/candidates/{userId}?strategy=collaborative|trending|new
→ Raw candidate set before ranking (for debugging / A/B test override)
GET /api/v1/recs/batch
body: { userIds: [...], context: "email_digest", limit: 10 }
→ Batch recommendation for email/push pipelines
The modelVersion field in every response is non-negotiable for A/B testing — it allows the analytics layer to attribute downstream engagement to the exact model that produced the recommendation. The /candidates debug endpoint is a staff-level addition that makes the system introspectable: on-call engineers can inspect the raw retrieval output, diagnose ranking bugs, and override the retrieval strategy during incidents.
Data Model
CREATE TABLE user_embeddings (
user_id UUID PRIMARY KEY,
embedding BYTEA NOT NULL, -- 256-dim float32, 1024 bytes
model_version VARCHAR(50) NOT NULL,
updated_at TIMESTAMPTZ DEFAULT NOW()
);
CREATE TABLE item_embeddings (
item_id VARCHAR(100) PRIMARY KEY,
embedding BYTEA NOT NULL,
item_type VARCHAR(50) NOT NULL,
model_version VARCHAR(50) NOT NULL,
updated_at TIMESTAMPTZ DEFAULT NOW()
);
CREATE TABLE interaction_events (
event_id UUID DEFAULT gen_random_uuid(),
user_id UUID NOT NULL,
item_id VARCHAR(100) NOT NULL,
event_type VARCHAR(20) NOT NULL,
session_id UUID,
occurred_at TIMESTAMPTZ NOT NULL
) PARTITION BY RANGE (occurred_at);
CREATE TABLE consumed_items (
user_id UUID NOT NULL,
item_id VARCHAR(100) NOT NULL,
last_consumed TIMESTAMPTZ NOT NULL,
PRIMARY KEY (user_id, item_id)
);
The interaction_events table is partitioned by occurred_at (monthly or weekly partitions). This enables efficient partition pruning when the Spark training job reads only the last 30 days of data, and makes partition drops the mechanism for 90-day log retention — no DELETE scans required.
Redis session and exclusion structures:
HASH user:{userId}:session
recentItems → JSON array of last 20 item IDs (current session)
sessionStart → epoch ms
categoryAffinity → JSON map of category → score
SET user:{userId}:consumed
→ Item IDs consumed in last 30 days (for exclusion filter)
→ TTL: 30 days
ZSET trending:global
→ Item IDs scored by real-time view count (24h rolling)
→ Used for cold-start fallback and trending-override injection
HASH user:{userId}:embedding
vector → binary blob, 1024 bytes (float32 × 256)
updated_at → epoch ms
The consumed-items set in Redis uses a TTL of 30 days. For high-cardinality users who consume thousands of items, a Bloom filter (space-efficient probabilistic structure, ~10 bits/element at 1% FPR) is preferable — but Redis sets are simpler to reason about for correctness.
Architecture
The diagram encodes the key insight: offline and streaming pipelines converge into shared stores (Redis embedding cache, Faiss vector store, online feature store) that the online serving path reads at query time. The serving path never runs training code — it only reads pre-materialized artifacts.
Deep Dive: Two-Stage Architecture
The fundamental constraint driving the two-stage design is latency. Scoring every item in a 10M catalog with a full neural network model at query time is physically impossible within 100ms — even at 2μs per scoring call, 10M items would take 20 seconds. The solution is to decompose the problem into two stages with different latency and accuracy trade-offs.
Stage 1: Retrieval (Candidate Generation)
Goal: reduce 10M items to approximately 1K candidates in under 30ms.
The retrieval stage sacrifices precision for speed. It does not need to find the perfect ranking — it needs to find the right set of candidates, quickly. The mechanism is Approximate Nearest Neighbor (ANN) search in an embedding space.
Collaborative Filtering — Matrix Factorization (ALS): Factorize the user-item interaction matrix into user embeddings and item embeddings where . The Alternating Least Squares algorithm scales to hundreds of millions of interactions using Spark MLlib. At inference, retrieve top-K items for a user by ANN search: find items whose embedding is closest (by dot product or cosine similarity) to the user embedding. This is the foundation of collaborative filtering — users who watched similar content get similar recommendations.
Two-Tower Neural Network: The two-tower model is the industry-standard upgrade from pure matrix factorization. Architecture:
- User tower: takes user ID embedding + user features (age bucket, country, device type, historical CTR by category) through 3–4 dense layers → 256-dim user embedding.
- Item tower: takes item ID embedding + item features (content category, duration, recency, quality score) through 3–4 dense layers → 256-dim item embedding.
- Training objective: maximize the dot product between user and item embeddings for interacted pairs, minimize it for non-interacted pairs (in-batch negatives — other items in the same training batch serve as negatives, avoiding expensive random negative sampling).
At inference, item embeddings are pre-computed and indexed in Faiss. The user embedding is computed once per request and ANN-searched against the index. This is the key efficiency insight: the item tower runs offline; only the user tower runs online.
Content-Based Retrieval: For new items without interaction history, generate embeddings from metadata: BERT (or a smaller distilled variant) for title and description text, ResNet for thumbnail images. These content embeddings live in a separate Faiss partition. When a user has shown affinity for a category, content embeddings from that category can supplement collaborative filtering candidates. This approach also handles the cold-start problem for new items before any interactions are recorded.
Session-Based Retrieval:
The current session’s watched items form a sequence. A Transformer-based session encoder (small, 2–4 layers) takes the last 20 item IDs as input and produces a session-context embedding that captures the user’s intent in the current browsing session. This embedding is ANN-searched similarly to the user embedding, but emphasizes recency over long-term preference. The session embedding is computed by the EmbeddingRefreshWorker after each interaction event and stored in Redis — the serving path reads it directly without recomputation.
Merging multiple retrieval sources: In production, all three retrieval sources (collaborative, content-based, session-based) generate candidate sets that are merged (union), deduplicated, and passed to the ranking stage. Each candidate carries its source and retrieval score.
Stage 2: Ranking
Goal: score approximately 1K candidates precisely using richer features, in under 50ms.
The ranking model can use features that would be too expensive to compute for 10M items but are feasible for 1K:
- User features: long-term CTR by category (7-day rolling), average watch-completion rate, preferred content duration, country, device, time-of-day bucket. Served from the online feature store (Redis hash), pre-materialized.
- Item features: 24-hour view count, recency decay score (newer items get a boost), quality score (editor rating or ML-derived), content duration, category, creator follower count.
- Cross features: retrieval score (from the embedding dot product), user-item category match (does the user’s top category match the item’s category?), session recency match (did the user watch something from this creator in the current session?).
Model choice: A gradient-boosted decision tree (XGBoost/LightGBM) is the industry default for ranking — fast inference (< 1ms per candidate), interpretable feature importances, robust to missing features, and easy to update with fresh training data. A small neural network (3–4 layers, 256 hidden units) can capture feature interactions that trees miss, at the cost of higher inference latency and more complex deployment.
Output: predicted probability of engagement (click, or watch ≥ 30% of duration). Candidates are sorted descending by score.
Post-Ranking Filters
After the ranking model produces a sorted list, three additional filters run in sequence:
- Exclusion filter: remove any item in the user’s
consumed_itemsRedis SET. This is a set lookup — O(1) per item, trivial cost. - Diversity enforcement: slot constraint algorithm — iterate sorted candidates, maintain a category counter, skip any item whose category already has 2 representatives in the current output window. Continue until
limititems are selected or candidates are exhausted. - Business rule injection: sponsored/promoted items are inserted at fixed slot positions (typically slots 1, 5, 10) regardless of score. A/B test variant assignments may also override specific slots with exploration items.
Deep Dive: Feature Store
The feature store is the most commonly underspecified component in recommendation system designs. Without it, training-serving skew is inevitable and model quality degrades silently after deployment.
The core problem: a feature like “user’s 7-day click-through rate by category” is computed in two places:
- During training: a Spark SQL job computes it over historical logs, attaching a specific value to each training example based on the feature value at the time of that training example (point-in-time correctness).
- During serving: the same feature must be read from a low-latency store within the 100ms SLA.
If these two computations differ — different windowing logic, different null handling, different data sources — the model encounters feature distributions at inference that differ from what it was trained on. This is training-serving skew, and it causes the model to underperform even when offline evaluation metrics look correct.
Feature store architecture:
Offline store (Hive/Delta Lake on S3):
- Stores historical feature values with timestamps.
- Point-in-time correct joins: when constructing a training dataset for event at time T, the feature pipeline joins to the feature value that was current at time T, not the current value. This prevents data leakage and ensures the training distribution matches reality.
- Updated nightly by Spark jobs that compute aggregate features from the interaction log partitions.
Online store (Redis/DynamoDB):
- Stores the latest pre-materialized feature vector per user and per item.
- Latency target: < 10ms for a feature read (including network round trip from the recommendation service).
- Updated in two ways: (a) batch write from the nightly Spark job (bulk update of all users/items), and (b) streaming write from the Kafka feature worker (incremental update for users with recent activity).
Feature pipeline (Kafka consumer):
The feature_worker service consumes from the interaction-events Kafka topic and computes rolling aggregate features in near real-time:
rolling_ctr_update:
on each CLICK event for (user_id, category):
increment HINCRBY user:{user_id}:features ctr_{category}_clicks 1
increment HINCRBY user:{user_id}:features ctr_{category}_impressions 1
recompute CTR ratio
write to online store (Redis)
write to offline store event log (for training replay)
Feature groups in production:
| Feature Group | Update Cadence | Storage | Example Features |
|---|---|---|---|
| User long-term stats | Daily | Offline + Online | 7d CTR by category, avg watch duration, device type distribution |
| User session stats | Real-time (< 30s) | Online only | Current session item sequence, session category affinity |
| Item stats | Hourly | Offline + Online | 24h views, 7d views, recency decay score, quality score |
| Context features | Per-request (computed inline) | None (computed at serving time) | Hour of day, day of week, device type from request headers |
The key engineering discipline is that every feature must have a registered schema, a defined computation logic, and a defined owner. Ad hoc feature computation in the serving path — computing rolling averages inline during a recommendation request — is a reliability risk and a source of skew.
Deep Dive: Cold Start
Cold start is one of the most common curveball follow-ups in recommendation system interviews. The failure mode is obvious: if you have no interaction history for a user or item, a pure collaborative filter has nothing to work with. The solution is a layered fallback strategy.
New User Cold Start
A new user has zero interaction history. Steps:
-
Demographic cluster assignment: use available signals (country from IP, device type, time of day, referral source) to assign the user to one of N persona clusters (N = 100–1000). Each cluster has a precomputed “persona embedding” derived from the average embedding of users with similar demographics. Serve the persona cluster’s top recommendations.
-
Onboarding preference signals: if the product has an onboarding flow asking users to select preferred categories or explicit interests, treat each selection as a synthetic strong positive interaction and immediately compute a thin embedding.
-
Transition trigger: after 5 real interaction events (clicks, watches), compute a thin personalized embedding by averaging the item embeddings of interacted items. Switch from persona-based to personalized retrieval. This is the “5-interaction cliff” — the system monitors new user cohorts to ensure this transition happens quickly (within the first session for most users).
-
Exploration injection: inject 20% exploration slots in the cold-start period — show items from diverse categories to accelerate preference discovery and provide training signal.
New Item Cold Start
A new item has no collaborative embedding because no users have interacted with it. Steps:
-
Content embedding computation: when an item is published, compute its embedding from metadata using pre-trained models: a BERT-small encoder for title + description text, a ResNet-18 feature extractor for the thumbnail image. These content embeddings are indexed in a separate Faiss partition and used for retrieval in lieu of collaborative embeddings.
-
Exploration injection: reserve 5% of recommendation slots across all users for “new item exploration.” Select items from this exploration bucket randomly weighted by content-embedding similarity to the user’s historical preferences. This provides guaranteed exposure for every new item.
-
Interaction accumulation: once a new item reaches 50–100 interactions, a mini-batch training run computes its collaborative embedding. A feature flag switches the item from the content-embedding index to the collaborative-embedding index.
-
Warm-start initialization: the collaborative embedding is warm-started by the item’s content embedding (projected into the collaborative space) rather than random initialization. This speeds convergence and reduces the cold-start penalty.
Python Integration — Part A: RecommendationService
import numpy as np
import faiss
import redis
import json
from typing import List
class RecommendationService:
def __init__(self, index: faiss.Index, redis_client: redis.Redis,
ranking_model, feature_store):
self.index = index
self.redis = redis_client
self.ranker = ranking_model
self.features = feature_store
def get_recommendations(self, user_id: str, limit: int = 20,
context: str = "homepage") -> List[dict]:
user_emb = self._get_user_embedding(user_id)
if user_emb is None:
return self._cold_start_recommendations(user_id, limit)
candidates = self._retrieve_candidates(user_emb, k=500)
consumed = self._get_consumed_items(user_id)
candidates = [c for c in candidates if c["item_id"] not in consumed]
ranked = self._rank_candidates(user_id, candidates, context)
diversified = self._apply_diversity(ranked, limit=limit * 2)
return diversified[:limit]
def _get_user_embedding(self, user_id: str):
raw = self.redis.hget(f"user:{user_id}:embedding", "vector")
if raw is None:
return None
return np.frombuffer(raw, dtype=np.float32).reshape(1, -1)
def _retrieve_candidates(self, user_emb: np.ndarray, k: int) -> List[dict]:
distances, indices = self.index.search(user_emb, k)
return [
{"item_id": self.index_to_item[idx], "retrieval_score": float(dist)}
for idx, dist in zip(indices[0], distances[0])
if idx != -1
]
def _rank_candidates(self, user_id: str,
candidates: List[dict], context: str) -> List[dict]:
user_feats = self.features.get_user_features(user_id)
scored = []
for cand in candidates:
item_feats = self.features.get_item_features(cand["item_id"])
features = {**user_feats, **item_feats,
"retrieval_score": cand["retrieval_score"],
"context": context}
score = self.ranker.predict(features)
scored.append({**cand, "rank_score": score})
scored.sort(key=lambda x: x["rank_score"], reverse=True)
return scored
def _apply_diversity(self, ranked: List[dict], limit: int) -> List[dict]:
result, category_counts = [], {}
for item in ranked:
cat = item.get("category", "unknown")
if category_counts.get(cat, 0) >= 2:
continue
category_counts[cat] = category_counts.get(cat, 0) + 1
result.append(item)
if len(result) >= limit:
break
return result
def _get_consumed_items(self, user_id: str) -> set:
members = self.redis.smembers(f"user:{user_id}:consumed")
return {m.decode() for m in members}
def _cold_start_recommendations(self, user_id: str, limit: int) -> List[dict]:
trending = self.redis.zrevrange("trending:global", 0, limit - 1)
return [{"item_id": t.decode(), "rank_score": 1.0} for t in trending]
Python Integration — Part B: EmbeddingRefreshWorker
from kafka import KafkaConsumer
import numpy as np
import redis
import json
import logging
logger = logging.getLogger(__name__)
class EmbeddingRefreshWorker:
"""Consumes interaction events and updates user session embeddings in Redis."""
def __init__(self, consumer: KafkaConsumer, redis_client: redis.Redis,
session_model, decay_factor: float = 0.85):
self.consumer = consumer
self.redis = redis_client
self.model = session_model
self.decay = decay_factor
def run(self):
for message in self.consumer:
try:
event = json.loads(message.value)
self._process_event(event)
except Exception as e:
logger.error("Failed to process event %s: %s", message, e)
def _process_event(self, event: dict):
user_id = event["user_id"]
item_id = event["item_id"]
event_type = event["event_type"]
weight = {"WATCH": 1.0, "CLICK": 0.5, "LIKE": 0.8, "SKIP": -0.3}.get(event_type, 0)
if weight == 0:
return
session_key = f"user:{user_id}:session_items"
self.redis.lpush(session_key, item_id)
self.redis.ltrim(session_key, 0, 19)
self.redis.expire(session_key, 3600)
session_items = [i.decode() for i in self.redis.lrange(session_key, 0, -1)]
if len(session_items) >= 3:
new_emb = self.model.encode_session(session_items)
self.redis.hset(
f"user:{user_id}:embedding",
mapping={"vector": new_emb.astype(np.float32).tobytes(),
"updated_at": event["timestamp"]}
)
self.redis.expire(f"user:{user_id}:embedding", 86400)
The EmbeddingRefreshWorker encodes the core real-time freshness guarantee: within one Kafka lag cycle (typically 1–5 seconds at normal load), a user’s embedding is updated to reflect their most recent session behavior. The 30-second end-to-end freshness SLA includes Kafka consumer lag + Redis write + next recommendation request serving the refreshed embedding.
Candidate Retrieval Strategies
| Strategy | Cold Start | Freshness | Compute Cost | Quality | When to Use |
|---|---|---|---|---|---|
| Matrix Factorization (ALS) | Poor — requires user history; fails for new users and new items | Low — daily retrain; misses session signals entirely | Low — fast ALS training on Spark; inference is a vector lookup | Good for stable preferences; misses context and cross-features | Mature catalog with stable interaction patterns; good baseline to beat |
| Two-Tower Neural Network | Moderate — user tower can use demographic features; item tower can use content features as warm start | Moderate — daily full retrain; hourly incremental embedding update via fine-tuning | High training cost (GPU hours); low inference cost (one matrix multiply + ANN search) | Excellent — captures complex user and item feature interactions; state of the art | Primary retrieval in production systems at scale; YouTube, TikTok, Spotify |
| Content-Based (metadata embeddings) | Excellent — works immediately for new items using BERT/ResNet; no interaction history needed | High — updates as soon as item metadata changes; not dependent on interaction log freshness | Low training cost (pre-trained encoders, fine-tuned once); low inference cost | Moderate — limited by metadata quality; cannot capture collaborative signal | New item cold start; niche content with sparse interactions; content-diversity discovery |
| Session-Based (Transformer) | Poor — requires at least 3–5 items in current session; useless on session start | Excellent — real-time session context; updates within 30 seconds of each interaction | Moderate training cost (Transformer, GPU required); moderate inference cost (sequence encoding at serving time) | Excellent for within-session intent; poor for long-term preference | High-engagement apps where within-session intent changes rapidly (e-commerce, music, short-form video) |
How would you handle a major live event where the same content is relevant to 50M users simultaneously?
During a viral live event — the Super Bowl halftime show, a major product launch, a breaking news event — 50M users simultaneously become interested in the same narrow set of content items. The personalization model fails in this scenario: collaborative filtering recommends based on historical preference clusters, not real-time cultural moments. Even if users start clicking on Super Bowl content, the embedding refresh lag (30 seconds to minutes) means the model is behind the moment.
The architectural fix is a trending override layer inserted after ranking but before the final response. A separate real-time aggregation pipeline (Kafka Streams or Flink) computes a rolling 5-minute view-count delta for every item. When an item’s delta exceeds a configurable threshold (e.g., 10,000 views in the last minute, 50× its baseline), it enters the “trending injection set.” The recommendation service injects one trending item into every recommendation slate at slot 3, regardless of the item’s personalized rank score.
This provides three benefits: (a) the viral content reaches all users within seconds of crossing the trending threshold, (b) the personalization model does not need to be retrained to handle the spike, and (c) the injected item slot is tagged separately in the impression log so it does not pollute the training signal for personalized slots. Degrade gracefully: during extreme load (a major event often causes 10× normal traffic), the system can switch entirely from personalized retrieval to a cached trending list with zero ANN search cost, served from a Redis sorted set that updates every 30 seconds.
A user complains their recommendations are stuck in a filter bubble. How do you add serendipity without tanking engagement metrics?
The filter bubble problem is a direct consequence of optimizing for predicted engagement: the model learns that users engage with content similar to what they have already watched, so it recommends more of the same, which further reinforces the preference signal, creating a positive feedback loop that narrows recommendations over time. This is the exploration-exploitation dilemma applied to recommendations.
The solution is a controlled exploration budget with separate metric tracking. Reserve 10% of recommendation slots (2 out of 20 items) for exploration items — items the ranking model would not have placed in the top-K. Select exploration candidates using one of two strategies:
- Epsilon-greedy: with probability ε, randomly sample from items in categories the user has not interacted with in the last 14 days. Simple but unweighted.
- Thompson Sampling / UCB: model uncertainty over user preference for unexplored categories as a prior, sample from the upper confidence bound of the preference estimate. More statistically principled; explores categories where the model is most uncertain rather than uniformly at random.
The critical operational requirement: track engagement on exploration slots separately from personalized slots in the impression log. This allows the A/B test to measure whether exploration slots have lower immediate CTR (they will) but higher long-term session length and return rate (the hypothesis). A/B test different exploration rates (5%, 10%, 15%) and measure 7-day and 30-day retention, not just next-click CTR. The exploration budget also serves as a data collection mechanism for improving the model on underexplored item clusters — without forced exploration, new content categories may never accumulate enough interactions to be represented in the training data.
Your ranking model is a 10-billion-parameter transformer. Inference latency is 800ms — too slow. How do you fix it?
An 800ms ranking model is a common problem in teams that start with a research model without co-designing for serving latency. The goal is to bring it under 50ms (leaving 50ms budget for retrieval and feature fetching within the 100ms SLA). The solution is a multi-stage compression and caching strategy — no single technique gets you from 800ms to 50ms, but four applied together do.
Step 1 — Knowledge distillation: train a 100M-parameter “student” ranking model to mimic the output distributions of the 10B-parameter “teacher” model, using the teacher’s output logits as soft targets. The student model runs 100× faster with typically 5–10% quality loss on offline metrics — often invisible in online A/B tests because the bottleneck was never model capacity but feature quality. This alone gets you from 800ms to 8–10ms.
Step 2 — INT8 quantization: quantize the student model’s weights and activations from float32 to INT8 using post-training quantization (PyTorch torch.quantization or TensorRT). This reduces memory bandwidth pressure by 4× and enables SIMD vectorization on CPU. Latency drops by another 2–3×, to approximately 3–5ms.
Step 3 — Cache ranking scores for popular pairs: compute the ranking model’s score offline for popular item × user-segment pairs (top 10K items × 1K user segments = 10M pairs) and store in Redis with a 5-minute TTL. At serving time, check the cache before running the model. Cache hit rate for popular items is typically 40–60%, reducing effective model calls by half.
Step 4 — Batch scoring and async pre-ranking: for users who are predicted to request recommendations in the next 60 seconds (based on session activity signals), pre-compute and cache their full ranked list in Redis. At request time, serve the cached result and trigger an async refresh. This reduces online model calls to zero for active users, at the cost of up to 60 seconds of ranking staleness — acceptable for homepage feeds, not for end-of-content next-up.
The architectural lesson: model serving latency is a product decision made at model design time, not an optimization problem to solve post-hoc. Staff engineers push back on model architecture choices that are incompatible with the serving SLA before the model ships, not after.
Further Reading
The academic and engineering literature on recommendation systems is exceptionally rich. The foundational paper is Covington, Adams, and Lakshmanan’s “Deep Neural Networks for YouTube Recommendations” (RecSys 2016), which introduced the two-stage retrieval + ranking paradigm and remains the most-cited practical reference for large-scale recommendation systems — it covers candidate generation with matrix factorization, deep ranking with wide-and-deep features, and the importance of example weighting by watch time over clicks. Facebook’s DLRM (Deep Learning Recommendation Model) paper describes the architecture for embedding-heavy recommendation models at trillion-parameter scale, with specific attention to memory bandwidth bottlenecks in embedding lookup on heterogeneous hardware. The Faiss library from Meta AI Research provides the production-grade ANN index implementations (IVF-PQ, HNSW, ScaNN) used by most large recommendation systems; the Faiss wiki documents the trade-offs between index types in detail. TensorFlow Recommenders (TFRS) provides a reference implementation of the two-tower model with in-batch negatives and a retrieval + ranking pipeline, making it an excellent starting point for prototyping. Spotify’s engineering blog on Discover Weekly covers Bayesian Personalized Ranking (BPR) and the matrix factorization approach used in their “made for you” products — particularly relevant for implicit feedback (stream counts as positives, no explicit negatives) and the time-decay weighting of older interactions. Finally, the Feature Store papers — Feast (open-source, Gojek/Google), Tecton (commercial, built by the Michelangelo team from Uber), and the Uber Michelangelo blog post itself — are essential reading for understanding point-in-time correctness, the offline/online store duality, and feature registry patterns at scale. The Michelangelo post in particular is the canonical reference for training-serving skew and how a feature store eliminates it.