Design a Recommendation System
A full Staff-level walkthrough of a large-scale recommendation system that serves personalized items to 1B users under a 200ms budget — built as a two-stage pipeline that retrieves thousands of candidates with approximate nearest-neighbor search, then ranks them with a deep multi-objective model. It trades recall against latency, exploration against exploitation, and freshness against relevance, all while handling cold start and diversity. Follow it top to bottom, or jump to any step.
Requirements
Functional Requirements
- Personalized recommendations: Show relevant items (videos, products, articles) per user
- Multiple surfaces: Home feed, "watch next", related items, email digest
- Real-time signals: Incorporate recent user activity (last click, last search)
- Cold start handling: Reasonable recommendations for new users/items
- Diversity: Avoid recommending too many similar items
- A/B testing: Compare recommendation algorithms via controlled experiments
Non-Functional Requirements
- Latency: < 200ms to generate recommendations
- Scale: 1B users, 100M items, 100K recommendation requests/sec
- Freshness: New items surfaced within hours; user preferences updated in minutes
- Availability: 99.99% — recommendations are on the critical path
Back-of-envelope estimation
API design
Data model
User Profiles: Feature Store (Redis + Bigtable)
Item Features: Feature Store
Interaction Logs: Kafka → BigQuery
High-level architecture
Two-Stage Architecture
Stage 1 — Candidate Generation (retrieve ~1000 from millions):
- Collaborative filtering: Users who interacted with similar items
- Content-based: Items similar to user's history (embedding similarity)
- Popularity: Trending items in user's region
- Social: Items friends interacted with
- Uses ANN (Approximate Nearest Neighbor) index for fast retrieval
Stage 2 — Ranking (score and order top 100):
- Deep neural network scores each candidate
- Features: user features, item features, context (time, device), cross-features
- Output: predicted engagement probability (click, watch time, like)
Stage 3 — Re-ranking (apply business rules):
- Diversity: limit items per category/creator
- Freshness boost: mix in new items
- Business rules: promoted content, content policy filtering
Deep dives
WHERE STAFF IS WONDeep Dive 1: Candidate Generation with ANN
Problem: Find ~1000 relevant items from 100M items in < 50ms.
Approach: Learn user and item embeddings; find nearest items to user embedding.
Embedding models:
- Matrix Factorization: Decompose user-item interaction matrix. Simple, effective.
- Two-Tower Model: Separate neural networks for user and item, trained with contrastive loss. More expressive.
ANN search algorithms:
Our choice: HNSW or ScaNN — best recall/speed tradeoff. Index updated hourly with new items.
Deep Dive 2: Ranking Model
Architecture: Deep & Cross Network (DCN) or Wide & Deep
Features:
- User: embedding, demographics, activity recency, preferences
- Item: embedding, category, popularity, freshness, quality score
- Context: time of day, device type, surface (home vs search)
- Cross-features: user_category × item_category interaction
Training:
- Objective: multi-task (predict click + predict watch time + predict like)
- Data: billions of user-item interaction logs
- Updated: retrained daily; online learning for real-time signal integration
Serving: TF Serving / TorchServe with GPU inference
- Batch scoring: score 1000 candidates in single forward pass
- Latency: < 50ms for batch of 1000
Deep Dive 3: Cold Start Problem
New users (no interaction history):
- Use demographic-based recommendations (popular in user's country/age group)
- Content-based on sign-up interests
- Rapidly incorporate first few interactions (online learning)
New items (no interaction data):
- Content-based features only (title, description, category)
- Exploration: inject new items into some users' feeds with lower ranking weight
- Multi-armed bandit: allocate exploration budget to learn item quality quickly
Bottlenecks & tradeoffs
Key Tradeoffs
Monitoring
- Recommendation quality: Click-through rate, watch time, user satisfaction surveys
- Serving latency p99: Alert if > 500ms
- Model staleness: Age of current model; alert if > 48 hours
- Coverage: % of items ever recommended; low coverage = popularity bias
- A/B test metrics: Statistical significance monitoring, guardrail metrics
Rubric — Senior vs Staff
Want more breakdowns like this?
Join free early access for upcoming RAG, LLM eval, agents, and AI infrastructure walkthroughs.