Design an Ad Serving System
Understanding the Problem
📣 What is an ad serving system? An ad server decides which ad to show in a slot, right now, for this viewer. When a page or app has space for an ad, it sends a request; the ad server finds the campaigns whose targeting matches, runs an auction among them (often including bids from outside buyers in real-time bidding), returns the winner, and then counts the impression and any click so advertisers can be billed. Google Ads, Amazon Ads and the ad systems inside streaming services all have this loop at their core.
There are three common framings, and they're one system:
- Real-time bidding under 500 ms: the focus is the request path, especially fanning out to external bidders under a hard deadline.
- Ads in a streaming service: the same decisioning, but for ad breaks in video, with several ads per break and huge synchronized spikes during live events.
- The Google Ads lifecycle: the focus is the advertiser side: creating a campaign, getting it reviewed, serving it, spending its budget, and reporting.
We'll build the core loop and give each variant its own section.
Functional Requirements
Core Requirements
- Advertisers should be able to create campaigns with a budget, schedule, targeting, bid and creatives, have them reviewed, and see them start serving.
- Publishers' apps and pages should be able to request an ad for a slot and get the best one back, chosen from direct campaigns and real-time bids from external buyers, within 500 ms.
- The system should record impressions and clicks, stop campaigns from overspending their budget, and give advertisers spend reports.
- A streaming video service should be able to fill ad breaks (several ads per break) for on-demand and live content.
Below the line (out of scope)
- Click-through prediction models and their training (we'll treat pCTR as a service).
- Invoicing and payments.
- The buyer side (a DSP's own bidding logic).
Non-Functional Requirements
Core Requirements
- The system should handle 500,000 ad requests per second at peak.
- End-to-end latency must stay under 500 ms, including the external auction; our own decision logic should take under 50 ms at p99. A late ad is a lost ad: the slot renders empty or the video plays on.
- Campaigns must not overspend by more than about 1% of their daily budget.
- Impression and click counts are billing data: they must not be lost or double counted, and invalid (bot) traffic must be filtered.
- Campaign changes (paused, budget raised, new creative approved) should take effect within about a minute.
- Ads must never break content. If the ad system fails, the page or video goes on without an ad (or with a house ad).
Below the line (out of scope)
- Strict real-time global consistency of budgets across regions. Approximately right, then reconciled, is the industry norm.
The numbers that drive the design:
- Index size: say 1 million active line items (the unit with targeting and a bid), each with a few kilobytes of targeting rules and metadata. That's a few gigabytes, small enough to hold in memory on every ad server. This one fact removes the database from the request path.
- External fan-out: if each request is sent to around 10 external bidders, that's 5 million outbound bid requests per second. Outbound traffic, not inbound, dominates our network and cost.
- Event volume: with a 60% fill rate, around 300,000 impressions per second. At about 1 KB per logged event, that's 300 MB/s, roughly 26 TB a day of raw logs.
- Budget contention: a big campaign might win thousands of impressions per second across hundreds of servers, each one spending from the same budget. That's a shared counter under heavy contention.
A strong opening move: "the campaign index fits in memory, so the request path never touches a database. The shared state that's hard is budgets, and the data that's hard is the event stream we bill from." That frames every deep dive that follows.