MongoDB

Designing an Ad Serving System

Build a programmatic advertising system — covering real-time bidding, ad targeting, auction mechanics, click tracking, and fraud prevention.

S

srikanthtelkalapally888@gmail.com

Designing an Ad Serving System

Ad serving systems match advertisers with publishers in real-time, executing auctions in milliseconds.

Key Players

Publisher:   Website/app showing ads
Advertiser:  Brand paying to show ads
DSP:         Demand-Side Platform (buys ads for advertisers)
SSP:         Supply-Side Platform (sells inventory for publishers)
Exchange:    Runs the auction between DSPs
DMP:         Data Management Platform (user data)

Real-Time Bidding Flow

User visits page (100ms budget total)
    ↓
Publisher → Ad Exchange (bid request)
    ↓ (<10ms)
Exchange broadcasts to all DSPs
    ↓ (<80ms)
DSPs evaluate + submit bids
    ↓
Exchange runs auction → Winner
    ↓
Ad creative delivered to browser

Bid Request

{
  "id": "abc123",
  "site": { "page": "https://news.example.com/article/123" },
  "user": { "id": "user_hash", "geo": { "country": "US", "city": "NYC" } },
  "imp": [{ "id": "1", "banner": { "w": 300, "h": 250 } }],
  "tmax": 80
}

Auction Types

First-Price Auction:
  Highest bidder wins, pays their bid
  Simpler, now industry standard

Second-Price (Vickrey):
  Highest bidder wins, pays second-highest bid + $0.01
  Historically common (Google AdWords original)

Ad Targeting

Contextual: Article about travel → Show travel ads
Behavioral: User visited travel sites → Show travel ads
Demographic: Age 25-34, female → Show relevant ads
Retargeting: User visited product page → Show product ad
Lookalike: Similar to existing customers

Click and Impression Tracking

Impression: Ad displayed → Pixel fires → Log event
Click:      User clicks → Redirect through tracker → Log + send to advertiser

Anti-fraud:
  IP blocklist
  Click velocity (>10 clicks/min = bot)
  Invalid traffic detection (IVT)

Frequency Capping

Limit: Show same ad max 3 times per day per user
Implementation: Redis counter per (user_id, campaign_id, day)
  INCR cap:user_123:camp_456:20260325 → if > 3: skip

Conclusion

Ad systems are the most latency-sensitive consumer systems — 100ms total budget for an auction including network round trips. Caching user profiles and pre-computing targeting segments are essential.

Share this article