Designing an Ad Serving and Auction System (Google Ads / Meta Ads)

Difficulty: Advanced Topics: Real-Time Bidding, Ad Ranking, ML Prediction, Budget Pacing, Fraud Detection Asked at: Google, Meta, Amazon, Microsoft, LinkedIn, Flipkart Ads · Prerequisites: Caching, Message Queues, and Scalability


1. Understanding the Problem

An ad system selects and serves the most relevant (and profitable) ad to a user in real-time — within 100ms of a page load. It runs an auction among millions of advertisers for every single ad impression, predicts which ads the user is likely to click, charges advertisers fairly, and tracks conversions. The hard parts: running billions of auctions per day at sub-100ms latency, preventing budget overspend in a distributed system, and detecting click fraud that costs advertisers billions.

Real examples: Google Ads, Meta Ads Manager, Amazon Sponsored Products, LinkedIn Campaign Manager.


2. Naive First Cut

flowchart LR
    USER["User loads page"]
    ADAPI["Ad Server"]
    DB[("SQL DB<br/>ads + budgets")]

    USER -->|"request ad"| ADAPI
    ADAPI -->|"SELECT ads WHERE budget > 0<br/>ORDER BY bid DESC LIMIT 1"| DB

Find the highest bidder with remaining budget, show their ad.

Why this breaks:

  • Highest bid is not the best ad — an ad nobody clicks earns nothing (need predicted click-through rate, or CTR)
    💡 Click-through rate (CTR) = clicks divided by impressions. If an ad is shown 1,000 times and clicked 20 times its CTR is 2%, and since we are paid per click that number decides what an impression is actually worth.
  • Single SQL query per impression at 1M queries per second (QPS) overwhelms any relational DB
    💡 QPS is just “how many requests arrive each second.” 1M QPS means a million separate database round trips every second, which is far past what one relational database serves.
  • Budget check + decrement is a race condition — 1000 concurrent auctions can overspend
    💡 A race condition = two requests read the same value at the same time and both act on it. Here a thousand auctions all read “budget left: $10” and all spend it, so the campaign spends $10,000 it did not have.
  • No targeting — everyone sees the same ad regardless of demographics
  • No conversion tracking — advertisers can’t measure return on investment (ROI)
  • Fraud bots clicking ads drain budgets with no real value

The rest of the doc evolves this into a multi-stage ranking pipeline with real-time budget pacing and fraud detection.


3. Prior Art We’re Drawing From

  • Google Second-Price Auction (GSP) - Generalized second-price auction: the winner pays the minimum bid needed to maintain their position (not their actual bid). This incentivizes truthful bidding — advertisers bid their true value without fear of overpaying. (Google Research)
  • Meta Ad Ranking (Total Value) - Ranks ads by bid x predicted_CTR x quality_score (called “total value”). This means a $1 bid with 10% CTR outranks a $5 bid with 1% CTR. Maximizes both revenue and user experience. (Meta Business Help)
  • LinkedIn Ad Budget Pacing - Uses a PID controller to adjust bid multipliers throughout the day so campaigns spend their daily budget evenly (not all in the first hour). The pacer observes actual spend rate vs expected and throttles/accelerates accordingly. (LinkedIn Engineering)
    💡 A PID controller is the same feedback loop a thermostat uses: measure how far you are from the target, then nudge the control knob up or down. Here the knob is a bid multiplier and the target is “spend the daily budget evenly across the day.”
  • Google Click Fraud Detection - Multi-layered system: real-time filters (impossible click patterns, bot signatures), offline ML models (anomaly detection on click streams), and manual review. Automatically credits advertisers for invalid clicks. (Google Ad Traffic Quality)

4. Functional Requirements

Core (Top 3)

  1. Serve the highest-value ad for a slot - given a user context and ad placement, run an auction and return the winning ad within 100ms
  2. Track impressions and clicks for billing - record every impression and click event accurately; charge advertisers on click (cost per click, CPC) or impression (cost per mille, CPM)
    💡 CPC means the advertiser pays only when someone clicks; CPM means they pay per thousand impressions (“mille” is Latin for thousand) whether anyone clicks or not. Which one a campaign uses decides what event we have to record durably.
  3. Manage campaign budgets in real-time - enforce daily and total budgets without overspend even under distributed concurrent auctions

Below the Line

  • Ad targeting (demographics, interests, retargeting)
  • A/B testing for ad creatives
  • Conversion tracking (post-click actions)
  • Reporting dashboards for advertisers
  • Ad approval and policy compliance

5. Non-Functional Requirements

Core

  • Latency: Ad selection P99 (the latency 99% of requests beat) < 100ms (ads are on the critical path of page load)
    💡 P99 is the slow tail, not the average: 99 out of 100 ad requests finish faster than this number. Averages hide the stalls that make a page feel broken, which is why the budget is written against the tail.
  • Throughput: 1M+ ad auctions per second globally
  • Budget accuracy: Overspend tolerance < 1% (beyond that, the platform absorbs the loss)
  • Availability: 99.99% — ad serving down = zero revenue