How would you design an ad click-through rate (CTR) prediction system?

ML system design, Case studies
Level L4 Advanced. Length Long. Nature Th+Mat Both.

What's being asked

Whether you understand why auction-based ad systems require well-calibrated probabilities rather than just good ranking, how to handle sparse high-cardinality features at huge QPS (queries per second), and how negative downsampling and delayed feedback are corrected mathematically.

Key idea

In an ad auction, ads are ranked and priced using bid×pCTR (predicted click-through rate), so probability calibration directly governs revenue and advertiser ROI (return on investment). Production systems pair sparse hashed IDs and embeddings, or GBDT (gradient-boosted decision trees) leaf transforms with online logistic regression (as in He et al., 2014), with negative downsampling and exact log-odds recalibration, q=pp+(1−p)/w, plus continuous streaming updates.

Steps

  1. Clarify requirements and scale: confirm the ad surface (feed, search or display), auction mechanism (e.g. second-price or generalised second-price where pCTR sets rank and clearing price) and strict latency SLA (service-level agreement, often under 30–50 ms for ad scoring). Back-of-envelope estimate: at 106 QPS peak and 500 candidate ads scored per request, the CTR service evaluates 106×500=5×108 ad–user pairs per second, demanding compact models and aggressive caching.
  2. Define the ML objective and metrics: predict the calibrated probability P(click∣user,ad,context) under binary log loss. Offline, pair AUC (area under the ROC curve, which measures ranking only) with NE (normalised entropy), NE=−1N∑i=1N(yiln⁡pi+(1−yi)ln⁡(1−pi))−(pˉln⁡pˉ+(1−pˉ)ln⁡(1−pˉ)), where yi∈{0,1} is the click label, pi is the predicted probability, N is the number of impressions and pˉ is the empirical background CTR (lower NE is better; NE =1 equals predicting pˉ). Also measure calibration (ratio of average predicted CTR to empirical CTR, plus ECE, expected calibration error). Online, track CTR, conversion rate, RPM (revenue per mille, or per thousand impressions) and advertiser budget pacing.
  3. Design data pipelines, downsampling and delayed feedback: ad impressions are huge (∼98–99% non-clicks), so keep all positive clicks and downsample negative impressions at rate w∈(0,1) to cut storage and training cost. Because downsampling inflates the predicted probability p, recalibrate at serving time using q=pp+(1−p)/w. Verification example: for w=0.1 and model output p=0.5, q=0.50.5+(1−0.5)/0.1=0.50.5+5=0.55.5=111≈0.0909. Handle delayed feedback via an online stream joiner (waiting a fixed attribution window before emitting negatives, or weighting recent impressions for click-delay hazard) so late clicks are not mislabelled as negatives.
  4. Engineer features for massive sparsity: combine three groups: (1) historical performance: smoothed empirical CTR and impression counts for the ad, campaign, advertiser and user over multiple time windows (1 hour, 1 day, 7 days), using Bayesian smoothing to avoid noisy ratios on new ads; (2) sparse categorical IDs and feature crosses: user ID, ad ID, publisher/page ID, device, geo and pairwise crosses mapped via the hashing trick into a fixed hash space or embedding table; (3) contextual and sequential signals: time of day, slot position and recent user clicks. An interviewer will probe hash-collision trade-offs and cold-start smoothing for new ads.
  5. Choose models from baseline to production: start with regularised LR (logistic regression) trained online with FTRL (Follow-the-Regularised-Leader) for L1 sparsity and fast weight updates on streaming clicks. Next, follow He et al. (2014): train GBDT (gradient-boosted decision trees) periodically to turn continuous and historical features into non-linear leaf-index features, and feed those leaf indices alongside sparse hashed IDs into a real-time online LR layer. At larger scale, replace or augment GBDT+LR with a deep feature-interaction network (such as Wide & Deep or DLRM, Deep Learning Recommendation Model) that learns dense embeddings for high-cardinality IDs and higher-order crosses.
  6. Design serving architecture and latency: after retrieval and budget/targeting filtering narrow millions of ads to hundreds, score candidates within a 40 ms budget: 10 ms for parallel user/context feature lookup, 20 ms for batched scoring (evaluating user-only subgraphs once per request and ad-side embeddings/trees per candidate), and 10 ms for recalibration, auction ranking (bid×q), pacing multipliers and network overhead. Interviewers will probe why model freshness matters: delaying training by even one day measurably raises NE, so separate hourly/streaming updates for linear/embedding layers from slower daily tree or deep-backbone refreshes.
  7. Evaluate and launch safely: evaluate offline on chronological test windows sliced by placement, advertiser tier and cold-start ads, checking calibration per slice (since global calibration can hide miscalibration on rare placements). Run shadow scoring to verify training–serving feature parity, then canary and run an online A/B test, often with budget-split or cookie-level/advertiser-level isolation to avoid cannibalising shared advertiser budgets.
  8. Monitor and iterate: track live calibration ratio (predicted CTR/observed CTR) by ad slot and device every few minutes (because miscalibration immediately distorts auction clearing prices), alongside NE drift, feature null rates and stream-joiner lag. Apply lightweight post-hoc recalibration (e.g. isotonic or Platt scaling per placement) if drift spikes, and probe trade-offs between model complexity, retraining cadence and auction stability.

Common mistakes

How long your answer should be

8–10 minutes; walk through the auction calibration requirement, NE, downsampling math and fresh streaming joins.

Read this

Previous in ML system design: How would you design a video recommendation system for a large video platform?
Next in ML system design: How would you design a search ranking system for a large e-commerce site?