← Back to Index

19. Case Study: Recommendation Engine

Design a production recommendation engine for an e-commerce platform with 50M users and 5M products. The system must serve personalized product recommendations on the homepage, product page, and checkout page.

Requirements

Scale Estimation

MAU: 50M → DAU: ~20M (40%)
Homepage loads per day: 40M (2 per user per day)
Peak QPS: ~2,000 with 5x holiday surge → 10,000 QPS
Products per recommendation: 20
Total recommendations per day: 40M × 20 = 800M

Features per user: ~200 (demographics, purchase history, browsing, etc.)
Features per product: ~100 (category, price, rating, inventory, etc.)
Feature store read per request: ~300 features × 8 bytes ≈ 2.4KB

Architecture

The recommendation system uses a two-stage retrieval + ranking architecture:

# Two-stage pipeline
Stage 1: Retrieval (candidate generation)
  Retrieve 500 candidate products per user
  Methods:
    - Collaborative filtering (matrix factorization)
    - Content-based filtering (product category similarity)
    - Trending / popular items (cold-start fallback)

Stage 2: Ranking
  Score and rank the 500 candidates using a deep neural network
  Features: user embeddings, product embeddings, cross features, context
  Output: top-20 ranked products

Stage 3: Post-processing
  Apply business rules (exclude out-of-stock, respect price filters)
  Apply diversity rules (don't show 20 products from the same brand)
  Apply personalization rules (boost categories user browsed recently)

Caching Strategy

Cache TypeKeyTTLRationale
Homepage recommendationsuser_id + page1 hourSlow-changing for most users
Similar productsproduct_id24 hoursProduct similarity changes rarely
Product embeddingsproduct_id24 hoursStored in Redis, recomputed daily
User embeddingsuser_id1 hourUpdated after purchase/click
Trending productscategory + region5 minutesChanges with real-time trends

Data Pipeline

# Offline training (daily at midnight)
1. Load user interaction logs (clicks, purchases, cart adds) from past 30 days
2. Compute user and product embeddings (Matrix Factorization, 128-dim)
3. Train ranking model (DNN with cross features)
4. Evaluate on held-out test set
5. Register if metrics improve

# Online feature computation (streaming)
Real-time events (click, add-to-cart, purchase):
  → Kafka → feature update → invalidate relevant caches
  → Update user embedding incrementally

# Batch feature computation (hourly)
Compute aggregated features:
  - Popularity scores per category
  - "Frequently bought together" pairs
  - Price elasticity per segment

Monitoring

Key Trade-offs

Exercise: Extend this recommendation engine to include a "real-time session-based" component that uses the user's current browsing session (last 5 clicks) to adjust recommendations instantly, without waiting for daily retraining. Describe: a) how you compute session vectors, b) where you store them, c) how they blend with the user's long-term profile, d) the latency budget for the session component.