Design a News Feed Ranking Pipeline
hardFeed ranking decides what each user sees in their home feed. Twitter, Facebook, Instagram, TikTok, LinkedIn all use ML-ranked feeds — chronological is dead at scale because engagement demands it. The ranker sits on top of the feed fanout (ID 170) with a tight 150 ms budget on app open.
Key Concepts
High-level design
Stage 1 — Candidate generation: union of timeline + trending + recommendations + ads, deduped.
Stage 2 — Feature fetch: online feature store batch read.
Stage 3 — Score: ML model (GBDT or DNN), often cascaded.
Stage 4 — Rerank: diversity rules, freshness, ad insertion, business rules.
Stage 5 — Log: served features + scores + impressions to training data lake.
Offline: nightly retraining on logs; A/B framework for new models.
Components
- Candidate generators (timeline service, trending detector, recommendation engine, ad server).
- Feature store (Feast, Tecton, custom) with online (Redis/Cassandra) + offline (Parquet/Hive).
- Model server (TF Serving, Triton, ONNX Runtime).
- Re-ranker / business rules engine.
- Impression logger (Kafka → training data lake).
- Training pipeline (Spark + ML framework).
- A/B test framework.
- Counterfactual / off-policy evaluator.
Model architecture
GBDT (XGBoost, LightGBM): fast inference, interpretable, handles tabular features well. Default for many production rankers.
DNN (TF, PyTorch): better with sparse / embedded features, higher quality at higher cost.
Two-tower models: user tower + item tower → embedding similarity. Used for candidate generation.
Multi-task: predict click + like + share + dwell-time jointly; weighted sum gives engagement score.
Distillation: train a smaller fast model on the labels of a bigger model. Common for latency.
Feature store
Online: low-latency key-value (Redis, Cassandra, DynamoDB). 1-10 ms per fetch.
Offline: data lake (Parquet, ORC) for batch training.
Parity: same code path computes features; or log online features to use for training. Prevents training-serving skew.
Streaming features: real-time updates (last hour engagement) computed via Flink / Kafka Streams.
Trade-offs
Latency budget caps model size — distill or cascade.
Offline training: cheap but drifts; combine with streaming features.
More candidates: better recall, higher cost.
Pure relevance: filter bubble; need diversity and exploration.
Logging bias: only see outcomes for served items. Off-policy evaluation hard.
Multi-task: balances objectives; weights are political (the team owning each metric fights for theirs).
Pitfalls
Training-serving skew silently degrades model quality. Catch with serving-side feature logging.
Filter bubble: pure relevance starves diverse content. Add diversity constraints + exploration.
Counterfactual evaluation hard. Live A/B is the gold standard but slow.
Cold start: new users / items have no features. Fallback to content-based or popular content.
Latency drift: features cost grows silently as new features added; budget enforcement matters.