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.
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
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)
| Cache Type | Key | TTL | Rationale |
|---|---|---|---|
| Homepage recommendations | user_id + page | 1 hour | Slow-changing for most users |
| Similar products | product_id | 24 hours | Product similarity changes rarely |
| Product embeddings | product_id | 24 hours | Stored in Redis, recomputed daily |
| User embeddings | user_id | 1 hour | Updated after purchase/click |
| Trending products | category + region | 5 minutes | Changes with real-time trends |
# 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