Case Study 36 โ€” Real-Time ML Recommendation Feed

Design a TikTok/YouTube Shorts-style recommendation system: rank millions of candidate items per user in < 100 ms using online ML features, handle cold start, and run continuous A/B experiments without breaking the serving path.

1. Problem

Users open a feed and expect an endless stream of personally relevant content. Behind each scroll, the system must:

  1. Recall a large candidate set from billions of items
  2. Score and rank them with ML models using fresh user/context features
  3. Apply business rules (diversity, freshness, safety)
  4. Serve results with strict latency SLOs while experiments run in production

The hard part is not training a model offline โ€” it is serving ranked results at scale with low latency, fresh features, safe rollouts, and graceful degradation when components fail.

2. Requirements

Functional (MVP)

  • Feed API: return ranked item list for a user session (cursor pagination)
  • Candidate generation: multi-source recall (follow graph, collaborative filtering embeddings, trending, exploration bucket)
  • Ranking: two-tower or deep ranking model producing relevance score per candidate
  • Feature lookup: user, item, and cross features at request time (< 10 ms budget)
  • Re-ranking: diversity (MMR), dedupe, freshness boost, policy filters
  • Impression/click logging for model training feedback loop
  • Cold start: default feeds for new users; content-based fallback for new items
  • A/B testing: route % traffic to alternate models/recall configs

Out of scope (initially)

  • Full real-time model training on every click (batch + near-line refresh is OK)
  • Cross-platform unified ranking for ads + organic in one auction (separate initially)
  • Explainability UI for every recommendation
  • Federated learning on device

Non-functional

  • p99 feed latency < 150 ms end-to-end (mobile on good network)
  • Ranking stage p99 < 80 ms after candidates assembled
  • Feature store read p99 < 5 ms for hot features
  • Availability 99.95% โ€” fallback to heuristic/trending rank on model outage
  • Scale: 500M DAU, 10B items catalog, 50B impressions/day
  • Experiment isolation: no cross-contamination of metrics between variants
  • Privacy: GDPR delete propagates to feature store within 30 days

3. Back-of-the-envelope

These numbers are rough order-of-magnitude math โ€” not a TikTok capacity plan. In interviews, the goal is to show you know what to count and which resource breaks first. A useful shortcut: 1 QPS sustained โ‰ˆ 86,400 requests/day (86,400 seconds in a day). Recommendation feeds are extreme read volume with a brutal feature lookup ร— candidate multiplication โ€” peak is often 3โ€“4ร— average during prime-time scrolling.

Why we estimate

A recommendation feed must rank millions of candidates per user in < 100 ms. Estimates tell us:

  • Whether feature store reads or GPU ranking inference is the real bottleneck
  • Why you cannot naively score 2,000 full neural models per request
  • How impression logging drives a separate petabyte-scale training pipeline

Assumptions

AssumptionValueWhy it matters
DAU500MGlobal short-video scale
Feed requests/user/day100Heavy scrollers drive volume
Catalog size10B itemsRecall must prune aggressively
Candidates recalled2,000 per requestWide funnel before ranking
Items ranked (scored)500 per requestAfter pruning โ€” GPU batch size
Items returned50 per requestFinal feed page
Feature vector size~200 floats (800 B)Per (user, item) pair for ranking
Impressions/day50B500M DAU ร— 100 requests

Step A โ€” Traffic (QPS / throughput) with labeled arithmetic

Feed request rate:

Feed requests/day = 500M DAU ร— 100 = 50,000,000,000/day
Avg feed QPS      = 50B รท 86,400
                  โ‰ˆ 580,000 requests/second

Peak (ร—3โ€“4 prime time):
  โ‰ˆ 2,000,000 feed requests/second

Feature lookups (naive โ€” before dedup):

Naive feature reads = 580K req/s ร— 2,000 candidates
                    โ‰ˆ 1.16 billion feature lookups/second
  โ†’ Impossible without dedup, batching, and precomputed embeddings

Feature lookups (after dedup + batching โ€” realistic):

Unique user features per request: 1
Unique item features: ~30% overlap in recall set
Effective unique fetches โ‰ˆ 50M/s at peak (still enormous โ†’ feature store sharding mandatory)

Ranking inference (GPU-bound):

Inferences/s      = 580K req/s ร— 500 scored items
                  โ‰ˆ 290 million inferences/second
  โ†’ Requires massive GPU fleet + model distillation to smaller student models
  โ†’ Batch 500 candidates per GPU call to amortize overhead

Step B โ€” Storage

Feature store โ€” hot user features:

500M users ร— 2 KB user features โ‰ˆ 1 TB
  โ†’ Sharded in-memory (Redis/RocksDB) with replication

Feature store โ€” item features (mostly cold):

10B items ร— 1 KB item features โ‰ˆ 10 TB total
Hot set (~5% actively served) โ‰ˆ 500 GB in memory
  โ†’ Long-tail items fetched from SSD tier on demand

Impression logs (training feedback loop):

50B impressions/day ร— 50 items ร— 200 B per log entry
  โ‰ˆ 500 TB/day raw

Compressed (~10ร—) โ‰ˆ 50 TB/day
30-day rolling window โ‰ˆ 1.5 PB compressed training data

Step C โ€” Bandwidth / other

Feed API response bandwidth (peak):

50 items ร— ~2 KB metadata per item โ‰ˆ 100 KB per feed response
2M req/s ร— 100 KB โ‰ˆ 200 GB/s peak egress
  โ†’ CDN for media; feed metadata is JSON โ€” pagination + compression required

Latency budget (p99 < 150 ms end-to-end):

Feature store read  โ†’ < 5 ms (hot features)
Candidate recall    โ†’ < 30 ms (multi-source parallel)
Ranking inference   โ†’ < 80 ms (GPU batch)
Re-rank + filters   โ†’ < 20 ms
Network + overhead  โ†’ ~15 ms

Step D โ€” Ratios and capacity table

MetricAveragePeakNotes
Feed QPS~580K/s~2M/sPrimary serving load
Candidates recalled2,000/reqโ€”Wide funnel
Items scored (ranked)500/reqโ€”After pruning
Items returned50/reqโ€”Final page
Recall:rank:serve ratio2000:500:50โ€”Funnel narrows 40:10:1
GPU inferences/s~290M/s~1B/sBatch 500 per call
Impression log/day~500 TB rawโ€”~50 TB compressed
Feature store hot~500 GBโ€”5% of 10B item catalog

What the numbers tell us

  • 580K feed QPS average, 2M peak โ†’ multi-stage funnel: recall wide โ†’ prune โ†’ rank narrow โ†’ re-rank
  • 1.16B naive feature lookups/s is impossible โ†’ precomputed embeddings, batch fetch, dedupe by itemId
  • 290M GPU inferences/s โ†’ model distillation, two-tower architecture (dot product, not full MLP per pair)
  • 1.5 PB training data (30-day window) โ†’ separate offline pipeline; serving path must not touch this
  • Feature store p99 < 5 ms โ†’ hot user + item features in memory; cold tail on SSD
  • Fallback on model outage โ†’ heuristic/trending rank preserves 99.95% availability SLO

Common mistake for this problem

Trying to score all 2,000 candidates with a full deep neural ranker per request โ€” 580K ร— 2,000 = 1.16B model calls/s is infeasible. Interviewers want a multi-stage funnel: cheap recall (embeddings, graph) โ†’ prune to 500 โ†’ expensive rank โ†’ MMR re-rank to 50. Another mistake: computing features at request time โ€” user and item features must be precomputed in the feature store with online join for cross-features only.

4. High-Level Design (HLD)

Request path (sequence)

Components

ComponentRole
Feed OrchestratorLatency budget owner; parallelizes recall, features, rank
Recall ServiceANN on embeddings, graph walk, trending, explore bucket
Online Feature StoreLow-latency point + batch reads; versioned features
Ranking ServiceGPU inference; batching, model warmup, circuit breaker
Re-ranking / PolicyMMR diversity, NSFW filter, creator fairness caps
Experiment ServiceDeterministic bucketing; config overlays per variant
Impression PipelineKafka โ†’ Flink โ†’ training tables + real-time counters
Feature RegistryLineage, TTL, backfill jobs, PIT-correct joins for training

Multi-stage ranking funnel

Stage 0 โ€” Candidate generation (2,000 items, ~20 ms)
  Sources: follow graph, ANN (two-tower item tower), trending, random explore

Stage 1 โ€” Lightweight prerank (500 items, ~15 ms)
  Small logistic model or dot-product on precomputed embeddings

Stage 2 โ€” Deep ranker (100 items, ~40 ms)
  Full feature cross, DCN/DeepFM on GPU

Stage 3 โ€” Re-rank (50 items, ~5 ms)
  MMR, dedupe same creator, boost fresh, demote seen

Trade-offs

ChoiceProsCons
Precompute user embedding every 5 minFast dot-product recallStale for breaking interests
Compute user embedding onlineFresherAdds 10โ€“20 ms; GPU cost
Monolithic rankerSimpler opsHarder A/B at sub-component level
Layered ranker + config-driven recallFlexible experimentsOrchestration complexity
Redis feature storeSub-ms readsMemory cost; large feature sets need sharding
Point-in-time correct offline storeNo training leakageSlower backfills

5. Low-Level Design (LLD)

APIs

GET /v1/feed
Headers: Authorization: Bearer <token>
Query: cursor=<opaque>, limit=20, tab=for_you
โ†’ 200 {
     "items": [
       { "itemId": "vid_9x2", "score": 0.87, "reason": "similar_to_liked", "cursor": "..." }
     ],
     "nextCursor": "eyJ...",
     "experimentVariant": "rank_v3_recall_b"
   }

POST /v1/events
Body: {
  "sessionId": "...",
  "events": [
    { "type": "impression", "itemId": "vid_9x2", "position": 3, "ts": 1710000000 },
    { "type": "click", "itemId": "vid_9x2", "watchMs": 4200, "ts": 1710000004 }
  ]
}
โ†’ 202 Accepted

GET /v1/features/user/{userId}
โ†’ { "features": { "age_bucket": 3, "country": "IN", "embedding_v2": [...] }, "version": "2024-07-01T12:00:00Z" }

POST /internal/v1/rank
Body: { "userId": "...", "candidateIds": ["..."], "modelId": "rank_v3", "featureSnapshotId": "..." }
โ†’ { "scores": [{ "itemId": "...", "score": 0.91 }] }

POST /internal/v1/experiments/assign
Body: { "userId": "...", "surface": "feed" }
โ†’ { "experiments": { "ranking_model": "v3", "recall_mix": "config_B" } }

Schema

Item metadata (Cassandra / DynamoDB)

items (
  item_id          TEXT PRIMARY KEY,
  creator_id       TEXT,
  created_at       TIMESTAMP,
  duration_sec     INT,
  category_ids     SET<TEXT>,
  language         TEXT,
  safety_labels    MAP<TEXT, FLOAT>,
  embedding_v2     BLOB,          -- 128-dim quantized
  stats_7d         MAP<TEXT, BIGINT>  -- views, likes, shares
)

User features (Feature Store โ€” online)

user_features (
  user_id          TEXT,
  feature_name     TEXT,
  feature_value    BLOB,          -- typed serialization
  updated_at       TIMESTAMP,
  PRIMARY KEY (user_id, feature_name)
)

-- materialized wide row for hot path
user_feature_wide (
  user_id          TEXT PRIMARY KEY,
  embedding_v2     BLOB,
  recent_categories MAP<TEXT, FLOAT>,
  last_active_at   TIMESTAMP,
  version          TIMESTAMP
)

Impression log (Kafka โ†’ Iceberg / BigQuery)

impressions (
  request_id       STRING,
  user_id          STRING,
  session_id       STRING,
  item_id          STRING,
  position         INT,
  rank_score       FLOAT,
  model_id         STRING,
  recall_source    STRING,
  experiment_tags  MAP<STRING, STRING>,
  ts               TIMESTAMP
)

clicks / watch_events (
  request_id       STRING,
  item_id          STRING,
  watch_ms         INT,
  completed        BOOLEAN,
  ts               TIMESTAMP
)

Experiment assignment (Redis + Postgres audit)

experiment_assignments (
  user_id          BIGINT,
  experiment_id    INT,
  variant          VARCHAR(32),
  assigned_at      TIMESTAMPTZ,
  PRIMARY KEY (user_id, experiment_id)
)

Modules

FeedController
FeedOrchestrator
ExperimentAssigner
RecallAggregator
  โ”œโ”€โ”€ GraphRecall
  โ”œโ”€โ”€ AnnRecall (VectorDB client)
  โ”œโ”€โ”€ TrendingRecall
  โ””โ”€โ”€ ExploreRecall
FeatureStoreClient (batchGet, cache-aside)
RankingClient (GPU batching pool)
Reranker (MMR, PolicyEngine)
ImpressionLogger (async Kafka producer)
ColdStartResolver
CircuitBreaker / FallbackRanker

Algorithm โ€” feed orchestration (latency-budget aware)

function getFeed(userId, cursor, limit):
  budget = LatencyBudget(totalMs=150)
  variant = experimentAssigner.assign(userId, "feed")  // cached locally 5 min

  with budget.stage("recall", maxMs=25):
    candidates = recallAggregator.recall(userId, variant.recallConfig, limit=2000)
    if candidates.empty:
      candidates = coldStartResolver.defaultFeed(userId)

  with budget.stage("prerank", maxMs=20):
    preranked = prerankModel.score(userId, candidates, topK=500)

  with budget.stage("features", maxMs=10):
    features = featureStore.batchGet(userId, preranked.ids, version=variant.featureVersion)

  with budget.stage("rank", maxMs=60):
    try:
      scores = ranker.score(preranked.ids, features, modelId=variant.modelId)
    catch TimeoutOrUnavailable:
      scores = fallbackRanker.heuristic(preranked, features)

  with budget.stage("rerank", maxMs=10):
    seen = sessionStore.getSeen(userId, cursor)
    final = reranker.apply(scores, seen, policies=[diversity, freshness, safety])

  page = paginate(final, cursor, limit)
  async impressionLogger.log(userId, page, variant, scores)
  return page

Algorithm โ€” MMR diversity re-ranking

Maximal Marginal Relevance: balance relevance vs redundancy.

function mmrRerank(scoredItems, lambda=0.7, k=50):
  selected = []
  remaining = scoredItems sorted by score desc
  while len(selected) < k and remaining not empty:
    best = null; bestMmr = -inf
    for item in remaining:
      simToSelected = max(cosine(item.emb, s.emb) for s in selected) if selected else 0
      mmr = lambda * item.score - (1 - lambda) * simToSelected
      if mmr > bestMmr:
        bestMmr = mmr; best = item
    selected.append(best)
    remaining.remove(best)
  return selected

Algorithm โ€” cold start

function coldStartFeed(userId):
  profile = onboarding.get(userId)  // locale, picked topics
  if profile.hasSelections:
    return contentBasedRecall(profile.topics, trendingInLocale)
  else:
    return globalTrending.mix(exploreRatio=0.2)

function newItemBoost(item, user):
  if item.ageHours < 24 and item.creator in user.followed:
    return 1.2
  if item.ageHours < 6:
    return exploreBucket.sample(item, epsilon=0.05)  // multi-armed bandit lite
  return 1.0

Algorithm โ€” A/B assignment (deterministic, sticky)

function assignVariant(userId, experimentId):
  salt = config.experiments[experimentId].salt
  bucket = hash(userId + salt) % 10000
  cumulative = 0
  for variant in config.experiments[experimentId].variants:
    cumulative += variant.allocation * 10000
    if bucket < cumulative:
      return variant.name
  return "control"

Feature store: online/offline consistency

// Offline (training) โ€” point-in-time join
SELECT i.*, u.features_at(i.ts) AS user_features
FROM impressions i
JOIN user_feature_history u
  ON u.user_id = i.user_id AND u.valid_from <= i.ts AND u.valid_to > i.ts

// Online โ€” read latest materialized
function batchGet(userId, itemIds):
  userWide = redis.get("uf:" + userId)
  itemKeys = ["if:" + id for id in itemIds]
  itemRows = redis.mget(itemKeys)
  return joinFeatures(userWide, itemRows)

Concurrency & correctness

  • Idempotent feed requests: request_id dedupes double-tap; cursor is opaque signed blob
  • Feature versioning: model trained on feature_v2 must not read feature_v3 โ€” enforced at deploy
  • Impression logging at-least-once โ†’ training pipeline dedupes on (request_id, item_id, position)
  • GPU batching queue: requests wait max 5 ms to fill batch; timeout โ†’ prerank-only fallback
  • Experiment SRM monitoring: chi-square test on assignment counts; auto-pause if skew detected

6. Scale evolution

StageTrafficArchitecture
MVP10k QPSMonolith + Postgres + Redis; single recall source; logistic rank
Growth100k QPSSplit recall/rank; introduce two-tower embeddings; Kafka impressions
ML maturity500k QPSDedicated feature store (Feast + Redis cluster); GPU ranker fleet; multi-stage funnel
Global2M QPS peakRegional serving stacks; geo-sharded feature store; replicate vector index per region
Advanced2M+ QPSDistilled student models; learned recall (RL); near-line embedding refresh via Flink
Failure modesโ€”Ranker down โ†’ prerank + trending; feature store slow โ†’ cached user wide row + item defaults

7. Recap

  • Funnel, don't brute-force rank: recall thousands โ†’ prerank hundreds โ†’ deep-rank tens
  • Feature store is the product: online/offline parity and point-in-time correctness prevent silent model rot
  • Latency budgets per stage force explicit fallbacks โ€” never block the feed on one dependency
  • Cold start needs exploration (epsilon-greedy / bandits) plus content-based signals, not random alone
  • A/B at orchestration layer (recall config + model id) beats redeploying monoliths per experiment

Practice: draw the recall โ†’ feature โ†’ rank โ†’ rerank pipeline from memory; explain how you'd detect and fix training-serving skew when click-through drops after a feature schema change.