System Design: Designing a Real-time Bidding (RTB) Ad System
Real-time Bidding (RTB) is the backbone of the modern digital advertising industry. When you load a webpage, an auction happens in the background to decide which ad you see. The entire process—from the moment the page starts loading to the ad appearing—must happen in less than 100 milliseconds.
1. The Core Players
- The Publisher: The website or app where the ad will appear.
- SSP (Supply-Side Platform): Represents the publisher and offers their "Ad Impression" for sale.
- Ad Exchange: The marketplace where the auction happens.
- DSP (Demand-Side Platform): Represents advertisers and bids on impressions based on user data.
2. The 100ms Race: Step-by-Step
- User loads page: The browser pings the Ad Server (SSP).
- Auction Starts: The SSP sends an "Auction Request" to the Ad Exchange.
- Bidding: The Exchange sends the request to multiple DSPs.
- Scoring: Each DSP checks its database: "Who is this user? What is my advertiser willing to pay?"
- Bid Response: DSPs send their bids back to the Exchange.
- Winner: The Exchange picks the highest bid and notifies the winner.
- Rendering: The ad is delivered to the browser.
3. High-Performance Infrastructure
To survive this 100ms window, every microsecond counts.
- The Database: Traditional SQL or even Redis might be too slow for the heavy profiling DSPs do. Many DSPs use Aerospike or Scallay, which are optimized for ultra-low latency flash storage.
- Network: Use Global Server Load Balancing (GSLB) to route requests to the nearest data center.
- Protocol: Use binary protocols like gRPC or Protocol Buffers instead of JSON to reduce serialization overhead.
4. Scaling the DSP: User Profiling
DSPs store massive amounts of data about users (cookies, history, demographics).
- The Challenge: Millions of user profiles must be accessible in < 5ms.
- The Solution: Use an In-memory NoSQL store with high throughput and predictable P99 latency.
5. Budget Management and Pacing
An advertiser doesn't want to spend their entire $10,000 daily budget in the first 5 minutes of the morning.
- Pacing Engine: A distributed service that monitors spending in real-time and slows down bidding if the budget is being consumed too fast.
- Synchronization: Use a high-speed counter (Redis) to track global spend across all bidding nodes.
6. Fraud Detection
Ad fraud (bots) is a multibillion-dollar problem.
- The Filter: Use a pre-bid filter (like IAS or DoubleVerify) to detect suspicious IP addresses or user agents before placing a bid.
Summary
The engineering of RTB is the ultimate challenge in Latency vs. Accuracy. By optimizing the network path and using specialized NoSQL databases, you can build a system that runs the world's largest marketplace in the blink of an eye.
