Design: Ad Click Prediction
Predicting whether a user will click a digital advertisement with calibrated probabilities for auction pricing.
The Problem
Design a system that predicts the probability a user will click a given ad. The system serves 50,000 queries per second. Each query must score hundreds of candidate ads in under 20ms. Predicted probabilities must be well-calibrated because they directly determine how much advertisers pay.
High-Level Architecture
Ad Request (User visits page)
│
▼
┌───────────────────────────────┐
│ STAGE 1: AD CANDIDATE RETRIEVAL│ (~3ms)
│ Targeting filters + ANN search │
│ Output: ~200 Eligible Ads │
└───────────────────────────────┘
│
▼
┌───────────────────────────────┐
│ STAGE 2: CTR PREDICTION │ (~10ms)
│ Deep Model (DCNv2 / DIN) │
│ Output: pCTR for each ad │
└───────────────────────────────┘
│
▼
┌───────────────────────────────┐
│ STAGE 3: AUCTION RANKING │ (~2ms)
│ Score = pCTR × Bid Price │
│ Second-price auction rules │
│ Output: Winning ad(s) │
└───────────────────────────────┘
Stage 1: Ad Candidate Retrieval
Not all ads in the system are relevant. Filter candidates by:
- Targeting Rules: Advertiser-specified constraints (geography, age group, device type, interest segments).
- Budget Checks: Skip ads that have exhausted their daily budget.
- Embedding Retrieval: Use a lightweight Two-Tower model to find ads semantically similar to the page content or user profile.
This narrows millions of active ads down to a few hundred candidates.
Stage 2: CTR Prediction Model
The core model predicts $P(\text{Click} = 1 \mid \text{User}, \text{Ad}, \text{Context})$.
Features
- User Features: Demographics, interest segments, recent browsing history, historical CTR.
- Ad Features: Advertiser category, ad creative type (image vs video), ad text embedding, historical CTR of this ad.
- Context Features: Time of day, day of week, page content topic, device type, browser.
- Cross Features: Has this user seen this ad before? How many times?
Model Architecture
Industrial systems use architectures like:
- DCNv2 (Deep & Cross Network v2): Learns explicit feature crosses automatically alongside deep MLP layers.
- DIN (Deep Interest Network): Uses attention over user behavior sequences to activate relevant past clicks for the current candidate ad.
- Multi-Task: Predict pCTR and pConversion simultaneously using shared bottom layers (MMoE).
Calibration
After training, check the reliability diagram. If predictions are not well-calibrated, apply Platt Scaling (fit a logistic regression on validation logits) or Isotonic Regression to align predicted probabilities with true click rates.
Stage 3: Auction
Rank ads by expected revenue:
$$\text{eCPM} = \text{pCTR} \times \text{Bid}_{\text{CPC}} \times 1000$$
The highest eCPM ad wins the slot. Under a Generalized Second-Price Auction, the winner pays just enough to beat the second-highest bidder, not their full bid.
Key Design Challenges
- Feature Sparsity: User IDs and Ad IDs have millions of unique values. Use embedding tables with hashing to handle this scale.
- Training Data Volume: Billions of impressions per day. Use online learning or frequent batch retraining.
- Serving Latency: Score hundreds of ads per request within 20ms. Use model distillation and TensorRT optimization.
- Click Fraud: Detect and filter bot clicks and click farms from training data to prevent poisoned models.
Say this out loud
An ad click prediction system retrieves candidate ads via targeting filters, scores each with a deep CTR model that produces calibrated probabilities, and ranks ads by expected revenue (pCTR times bid). Calibration is critical because predicted probabilities directly determine advertiser pricing in the auction.
Followups to expect
- What is delayed conversion attribution? A user may click an ad today and purchase 7 days later. The system must join click logs with conversion events using attribution windows to create proper training labels.
- How do you handle the cold start problem for new ads? Use exploration strategies (show new ads with boosted priority for initial impressions) and transfer learning from similar ads to estimate initial CTR.
Check yourself
Why must ad click prediction models output calibrated probabilities rather than just correct rankings?