← Back to Index

16. Case Study: Recommendation System

Design a production recommendation system for an e-commerce platform with 50M users and 5M products. Homepage recommendations, product page similar items, checkout cross-sells.

Requirements

Scale Estimation

MAU: 50M → DAU: ~20M (40%)
Homepage loads/day: 40M (2 per user)
Peak QPS: 10,000 (including holiday surge)
Products per recommendation: 20
Features per user: ~200, per product: ~100
Feature vector size: ~300 × 8 bytes = 2.4KB

Architecture: Two-Stage Retrieval + Ranking

Stage 1: Retrieval (candidate generation) — Retrieve 500 candidate products per user from multiple sources:

Stage 2: Ranking — Score 500 candidates with a deep neural network:

Stage 3: Post-processing — Business rules, diversity, personalization boost.

Caching Strategy

CacheKeyTTLStore
Homepage recsuser_id1 hourRedis
Similar productsproduct_id24 hoursRedis
User embeddingsuser_id1 hourRedis
Product embeddingsproduct_id24 hoursRedis
Trending (per category)category + region5 minutesRedis

Data Pipeline

# Offline training (daily)
1. Load 30 days of interaction data (clicks, purchases, cart adds)
2. Compute user and product embeddings (Matrix Factorization, 128-dim)
3. Train ranking model (DNN with cross features)
4. Evaluate → register if metrics improve

# Online feature computation (streaming)
Real-time events → Kafka → Flink → update embeddings
Invalidate caches for affected users/products

# Batch feature computation (hourly)
Popularity scores, "frequently bought together" pairs, price elasticity

Key Trade-offs

Exercise: Extend this system with a real-time session-based component. The user's current browsing session (last 5 clicks) should instantly adjust recommendations without waiting for daily retraining. Describe: session vector computation, storage (which DB?), blending with long-term embeddings, and the additional latency budget.