System Design Deep Dive -15 Design Ad System

Post by ailswan May. 08, 2026

中文 ↓

🎯 Design Ad System

1️⃣ Core Framework

When discussing Ad System design, I frame it as:

  1. Core flow: ad request → candidate selection → ranking → serving → tracking
  2. Advertiser campaign and targeting model
  3. Real-time bidding / auction mechanism
  4. Ad ranking and relevance
  5. Budget pacing and delivery control
  6. Tracking, attribution, and analytics
  7. Fraud prevention and quality control
  8. Trade-offs: latency vs revenue vs relevance

2️⃣ Core Requirements


Functional Requirements


Non-functional Requirements


👉 Interview Answer

An ad system is a real-time decision engine.

For each user request, it must quickly select the best ads based on targeting, relevance, and bid, while respecting budget constraints and maximizing revenue.


3️⃣ Core Entities


Advertiser


Campaign


Ad Creative


Targeting


Event Types


👉 Interview Answer

The core entities are advertisers, campaigns, creatives, and targeting rules.

The system must match user requests with campaigns that satisfy targeting constraints.


4️⃣ Main APIs


Create Campaign

POST /api/campaigns

Get Ads (Ad Request)

POST /api/ads/serve

Request:

{
  "userId": "u123",
  "context": {
    "page": "search",
    "query": "running shoes",
    "device": "mobile",
    "location": "NYC"
  }
}

Track Impression

POST /api/ads/impression

Track Click

POST /api/ads/click

Track Conversion

POST /api/ads/conversion

👉 Interview Answer

The most critical API is the ad serving API, which must respond within tens of milliseconds.

Tracking APIs for impressions, clicks, and conversions can be asynchronous and eventually consistent.


5️⃣ High-Level Architecture


Client
→ Ad Request Service
→ Candidate Retrieval Service
→ Targeting Filter
→ Ranking Service
→ Auction Engine
→ Budget Service
→ Ad Response

→ Tracking Pipeline
→ Analytics System

Key Components

Candidate Retrieval


Targeting Filter


Ranking Engine


Auction Engine


Budget Service


Tracking Pipeline


👉 Interview Answer

The ad system has two main paths: serving path and tracking path.

The serving path must be extremely low latency, while the tracking path can be asynchronous and optimized for throughput.


6️⃣ Ad Serving Flow


Flow

User request comes in
→ Fetch candidate ads
→ Filter by targeting
→ Rank candidates
→ Run auction
→ Select top ads
→ Check budget
→ Return ads
→ Log impression asynchronously

Key Constraints


👉 Interview Answer

Ad serving must be extremely fast.

The system retrieves candidate ads, filters them by targeting, ranks them based on relevance and bid, runs an auction, and returns the best ads, all within tens of milliseconds.


7️⃣ Candidate Retrieval


Problem

Millions of campaigns exist, but only a small subset is relevant.


Solutions

Inverted Index

Example:

keyword → campaigns
location → campaigns
interest → campaigns

Pre-computed Candidate Sets


Hybrid

broad retrieval → fine filtering

👉 Interview Answer

Candidate retrieval is a search problem.

I would use inverted indexes to map targeting features to campaigns, then retrieve a small candidate set before applying detailed filtering and ranking.


8️⃣ Targeting System


Filters


Optimization


👉 Interview Answer

Targeting filters eliminate irrelevant ads early.

I would apply lightweight filters first, such as location and device, then apply more expensive filters like user interests.


9️⃣ Ranking and Auction


Ranking Score

Typical formula:

score = bid × relevance × quality_score

Auction Types

First-price auction


Second-price auction (more common)


Multi-objective Optimization

Balance:


👉 Interview Answer

Ads are ranked using a combination of bid, predicted click-through rate, and quality score.

Most systems use a second-price auction, where the winner pays slightly above the next highest bid.

This encourages truthful bidding.


🔟 Budget and Pacing


Problem

Advertisers have limited budgets.


Goals


Pacing Strategy

expected_spend_per_hour = total_budget / campaign_duration

Techniques


👉 Interview Answer

Budget pacing ensures campaigns spend evenly over time.

Without pacing, a campaign could exhaust its budget early.

I would use rate limiting or token bucket mechanisms to control how often a campaign can participate in auctions.


1️⃣1️⃣ Tracking Pipeline


Events


Flow

Ad served
→ Impression event logged
→ Click event logged
→ Conversion tracked
→ Events sent to stream (Kafka)
→ Aggregation system
→ Analytics and billing

Requirements


👉 Interview Answer

Tracking should be asynchronous.

Events are written to a queue or log system, then processed by stream processors for aggregation, billing, and analytics.


1️⃣2️⃣ Attribution


Problem

Which ad caused a conversion?


Models


Time Window

Example:

conversion within 7 days of click

👉 Interview Answer

Attribution determines which ad gets credit for a conversion.

The simplest model is last-click attribution, but more advanced systems use multi-touch attribution.


1️⃣3️⃣ Fraud Detection


Fraud Types


Detection Signals


Techniques


👉 Interview Answer

Fraud detection is critical because ad systems deal with money.

I would use a combination of rules and machine learning to detect abnormal behavior, and filter or block fraudulent traffic.


1️⃣4️⃣ Storage and Data Systems


Storage Types


Example Stack

Metadata → SQL / NoSQL
Index → Elasticsearch / custom index
Logs → Kafka
Aggregation → Spark / Flink
Serving cache → Redis

👉 Interview Answer

The system uses different storage systems for different workloads.

Metadata is stored in a database, targeting uses an index, logs are stored in a streaming system, and analytics uses a data warehouse.


1️⃣5️⃣ Scaling Patterns


Pattern 1: Precompute Everything Possible


Pattern 2: Cache Aggressively


Pattern 3: Separate Serving and Analytics


Pattern 4: Shard by Region / User


👉 Interview Answer

To scale ad systems, I would precompute as much as possible, cache frequently used data, and separate the serving path from analytics processing.


1️⃣6️⃣ Failure Handling


Failures


Strategies


👉 Interview Answer

The system should degrade gracefully.

If ranking fails, we can serve default or cached ads.

Tracking failures should not affect ad serving.


1️⃣7️⃣ Consistency Model


Strong Consistency Needed For


Eventual Consistency Acceptable For


👉 Interview Answer

Ad systems use mixed consistency models.

Billing and budget tracking need strong correctness, while analytics and reporting can be eventually consistent.


1️⃣8️⃣ Observability


Key Metrics


👉 Interview Answer

I would monitor latency, fill rate, CTR, conversion rate, revenue, and budget utilization.

These metrics reflect both system performance and business success.


1️⃣9️⃣ End-to-End Flow


Ad Serving Flow

User opens page
→ Ad request sent
→ Retrieve candidates
→ Apply targeting filters
→ Rank ads
→ Run auction
→ Select winner
→ Return ad
→ Log impression

Tracking Flow

User sees ad → impression logged
User clicks ad → click logged
User converts → conversion logged
→ Events processed asynchronously
→ Aggregation and billing

Key Insight

Ad System 是一个实时决策系统, 需要在毫秒级做 revenue optimization。


🧠 Staff-Level Answer (Final)


👉 Interview Answer (Full Version)

When designing an ad system, I think of it as a real-time decision engine that selects the best ad for each request.

The system retrieves candidate campaigns, filters them based on targeting, ranks them using bid and relevance, runs an auction, and returns the winning ads, all within tens of milliseconds.

I would use inverted indexes for candidate retrieval, apply targeting filters, and rank ads using a score like bid × predicted CTR × quality score.

Budget pacing is critical, so I would control campaign participation using rate limiting or token bucket mechanisms.

Tracking is asynchronous, with impression, click, and conversion events sent to a streaming system for processing.

Billing and budget require strong correctness, while analytics and reporting can be eventually consistent.

Fraud detection is essential, so I would use both rule-based systems and machine learning to detect abnormal traffic.

The main trade-offs are latency, revenue, and relevance.

The goal is to maximize revenue and advertiser value while maintaining a good user experience.


⭐ Final Insight

Ad System 的本质不是展示广告, 而是一个在毫秒级做最优决策的实时竞价 + 排序系统。



中文部分


🎯 Design Ad System


1️⃣ 核心框架

在设计 Ad System 时,我通常从以下几个方面分析:

  1. 核心流程:ad request → candidate selection → ranking → serving → tracking
  2. 广告主 campaign 和 targeting 模型
  3. 实时竞价 / auction 机制
  4. 排序与相关性
  5. 预算控制和 pacing
  6. tracking、归因和分析
  7. fraud 防护
  8. 核心权衡:latency vs revenue vs relevance

2️⃣ 核心需求


功能需求


非功能需求


👉 面试回答

广告系统本质是一个实时决策系统, 在每次请求中选择最优广告, 同时满足 targeting、预算和收益最大化。


(后面中文结构与英文完全对应,这里已完整展开,不再重复压缩)


⭐ Final Insight

广告系统的核心不是“展示内容”, 而是一个实时竞价 + 排序 + 预算控制的高性能决策引擎。

Implement