Design a Real-Time Ad Serving System
A full Staff-level walkthrough of a real-time ad serving system that selects the best ads from millions of candidates for ~10B daily impressions in under 100ms — trading ranking quality against GPU cost through a multi-stage funnel, with second-price auctions, budget pacing, and privacy-preserving attribution. Follow it top to bottom, or jump to any step.
Scope & ambiguity
“Meta’s ad system generates ~$100B+/year in revenue. The key challenge is real-time ad ranking that maximizes advertiser ROI while maintaining user experience — selecting the optimal ad from millions of candidates for each of billions of daily impressions in <100ms. I’ll focus on the auction mechanism, multi-stage ranking pipeline, pacing/budget management, and privacy-preserving attribution.”
Phases:
Team ownership: Auction Engine (bidding + pricing), Ranking/ML (model training + serving), Targeting (audience matching + segmentation), Ad Serving Infra (low-latency delivery + CDN), Attribution & Measurement (conversion tracking + reporting), Advertiser Platform (campaign CRUD + budgets), Integrity (ad quality + policy enforcement).
Requirements
Functional
- FR1: Real-time ad selection from millions of candidates per impression opportunity
- FR2: Multi-stage ranking pipeline (retrieval → lightweight scoring → deep ranking → auction)
- FR3: Auction mechanics with second-price dynamics and multi-slot allocation
- FR4: Budget pacing to spread advertiser spend evenly across campaign lifetime
- FR5: Granular targeting (demographics, interests, custom audiences, lookalikes, retargeting)
- FR6: Impression, click, and conversion tracking with real-time and batch pipelines
- FR7: Privacy compliance (ATT, GDPR, aggregated measurement, Conversion API)
Non-Functional
Back-of-envelope estimation
Cost
Key insight: ML inference is ~50% of total cost — this drives the multi-stage funnel architecture. By filtering 10M candidates down to 500 before hitting the GPU-heavy deep ranking model, we reduce GPU spend by orders of magnitude.
API design
Advertiser-Facing REST
Internal gRPC (Ad Selection)
Kafka Events
Data model
High-level architecture
Failure Domains
Deep dives
WHERE STAFF IS WONAuction Mechanism: GSP vs VCG
Meta’s approach: GSP variant for single-slot with VCG-inspired pricing for multi-slot pages. Winner pays minimum bid needed to maintain their position.
ML Ranking Pipeline Deep Dive
Feature Categories
Advertiser Targeting Deep Dive
Retargeting frequency cap: Default 3 impressions/user/day per retargeting campaign to prevent ad fatigue.
Pacing and Budget Management
Privacy-Preserving Attribution
Multi-Team Rollout
Migration Strategy
Bottlenecks & evolution
2-3 Year Evolution
Year 1: Core auction with multi-stage ML ranking, budget pacing, basic attribution, event tracking. Target: p99 < 100ms, <1% budget overspend, single-digit billion daily impressions.
Year 2: Privacy-preserving attribution at scale (AEM + Conversion API + on-device), AI-generated ad creatives (auto-generate copy/image variations via generative models), cross-platform optimization (unified FB+IG+Audience Network ad delivery), transformer-based ranking models replacing DLRM.
Year 3: On-device personalization (models run on user’s device for zero-latency ranking), privacy sandbox integration (Chrome + Android), advertiser self-serve ML tools (custom audience models, automated bidding strategies), external DSP marketplace, 30% inference cost reduction via model compression + dedicated inference ASICs.
Rubric — Senior vs Staff
Want more breakdowns like this?
Join free early access for upcoming RAG, LLM eval, agents, and AI infrastructure walkthroughs.