Design an ad click-through rate (CTR) prediction system

    An ad click-through-rate (CTR) prediction system estimates P(click) for candidate ads in milliseconds to drive real-time bidding. It needs very low latency, rich feature crossing, and continuous online learning.

    AI & Machine Learning, 7 components, about 35 min

    1. Scope

    In scope

    • • Predict CTR per ad
    • • Real-time bidding decision
    • • Feature engineering/crossing
    • • Online model updates

    Out of scope

    • • Ad creative tooling
    • • Billing/budget pacing deep-dive
    • • Brand-safety

    2. Functional requirements

    Must have

    • • Predict CTR with low latency
    • • Rank/bid on ads
    • • Update model from click feedback
    • • Balance explore vs exploit

    Nice to have

    • • Conversion (CVR) modeling
    • • Budget pacing
    • • Multi-touch attribution

    3. Non-functional requirements

    Latency< 50 ms for our bid— inside a ~100 ms exchange round trip; ~10 ms of it for scoring
    ThroughputVery high (every impression)
    FreshnessNear-real-time model updates
    CalibrationPredicted CTR must be accurate, not just ranked

    4. Back-of-envelope estimation

    Assumptions

    • • 100K+ ad requests/sec
    • • LR/FM for fast serving
    • • FTRL online learning
    • • Click stream into trainer

    Derived numbers

    Predict QPS100K+/s
    ModelLR/FM — microsecond scoring
    Update lagMinutes via online learning

    5. API design

    POST/api/bid

    Ad request → CTR-scored bid

    6. High-level design

    What each box in the reference diagram is responsible for:

    Bidding Engine — Fetches features + ads, scores CTR, computes bid.
    CTR Predictor — Logistic Regression / Factorization Machines.
    User Profile — Interest segments for features.
    Ad Inventory — Candidate ads + metadata.
    Click Stream — Kafka/Kinesis of impressions + clicks.
    Online Trainer — FTRL updates model weights in near-real-time.

    7. Data model

    User Profile: user → interest segments, features

    8. Deep dives

    Why LR/FM, not deep nets

    At a 50ms bid timeout and 100K+ QPS, model latency must be microseconds. Logistic Regression and Factorization Machines are linear-ish, extremely fast, and interpretable. FMs add pairwise feature interactions (e.g., user_interest × ad_category) cheaply, capturing much of what a deep net would at a fraction of the cost.

    Feature crossing

    CTR depends on combinations: a sports fan × a sneaker ad. Cross features (interactions between user, ad, and context) are the heart of CTR modeling; FMs learn these crosses automatically via latent factors, avoiding manual explosion.

    Online learning (FTRL)

    Ad performance shifts constantly (new campaigns, trends). Follow-The-Regularized-Leader updates weights incrementally from the click stream, so the model adapts within minutes — far faster than nightly batch retraining. It also produces sparse models (good for serving).

    Explore vs exploit

    A purely greedy system never learns about new ads (no impressions → no data). Use Thompson sampling / epsilon-greedy to occasionally show uncertain ads, gathering data to improve future predictions while mostly exploiting known-good ads.

    9. Bottlenecks

    • Bid-timeout latency → linear models, feature caching
    • Cold-start ads → exploration
    • Calibration drift → online learning + monitoring

    10. Trade-offs

    Model family

    Choose: Fast models (logistic regression with feature crosses, factorization machines, or a small DNN) inside the auction latency budget.

    Instead of: Large deep models: more accurate, too slow to score many candidates per auction without heavy batching.

    Hundreds of candidates per request with a ~10 ms scoring budget.

    Training cadence

    Choose: Online or frequent incremental updates (FTRL-style) on streaming clicks.

    Instead of: Daily batch only: misses new ads and campaigns for a day.

    Ad inventory changes constantly.

    Calibration

    Choose: Calibrate predicted CTR because it multiplies the bid.

    Instead of: Rank-only models: fine for ordering, wrong for pricing.

    A 10% overestimate means overcharging or overpaying 10%.

    11. Failure modes

    Click fraud

    Advertisers pay for fake clicks; model learns from noise.

    Mitigation: Invalid-traffic filtering before both billing and training.

    Delayed conversions

    Recent data looks like negatives.

    Mitigation: Delayed-feedback models or waiting windows before labelling negatives.

    Feature pipeline outage

    Stale or missing features skew predictions.

    Mitigation: Defaults with monitoring of prediction distribution; alert on shifts.

    12. What interviewers probe

    Answer each one yourself before opening it.

    How do you evaluate calibration?Show a strong answer

    Compare mean predicted CTR to observed CTR overall and per bucket; normalized entropy and log loss rather than accuracy.

    How are high-cardinality features handled?Show a strong answer

    Hashing trick or learned embeddings for ids like ad id and user id, with frequency thresholds.

    What is the end-to-end latency budget?Show a strong answer

    Real-time bidding often allows ~100 ms total; candidate retrieval, scoring and auction share it, leaving ~10 ms for scoring.

    How do you avoid training on the model's own biases?Show a strong answer

    Reserve a small randomized exploration traffic slice for unbiased data.

    Sign in to track this guide in your curriculum progress.