KatInterview

Build a Real-Time Ad Decision Service with Spend Controls

System DesignModel answer
MediumModel answerSystem Designsystem-designad-servinglow-latency

Last updated

Design a backend that picks one ad per page slot within a strict latency limit, logs views and clicks for billing, and keeps campaign spend within budget and spread across the day.

Problem

You are asked to design the serving backend for an advertising platform. Each time a web page or app screen with an ad slot loads, the client calls your service. Your service must choose an ad, return it quickly enough that the page is not slowed down, and record what was displayed and clicked so advertisers can be billed and budgets respected.

Assumptions

  • Advertisers set up campaigns with targeting rules (such as location, device type, placement and audience segments), a bid, a daily budget and a run schedule. Campaign management tooling already exists. You are designing the serving side.
  • Each ad request describes the slot (placement and size) and its context (site or app, device, coarse location, and a user or session ID when consent was given).
  • The service returns at most one ad per slot, or no ad, within a tight latency budget because the page is waiting.
  • Impressions and clicks are tracked for billing and reporting. Spend must not run far beyond a campaign's budget.
  • External real-time bidding exchanges and training the click-prediction model are out of scope unless raised by the interviewer.

Questions you might ask first

  • Is billing per impression, per click, or both?
  • What is the latency budget, and what happens when it is missed?
  • How many requests per second, active ads and advertisers?
  • How strict is budget enforcement, and who absorbs a small overspend?
  • Are frequency caps required?

Part 1: Selecting and returning an ad

Describe the APIs, data model and main components. Walk through the path from the arrival of a request to the response: finding eligible ads, ranking them, choosing a winner and its price, and returning the creative. Explain how campaign edits made by advertisers reach the serving machines.

Cover:

  • Request and response APIs, including tracking links in the response
  • Candidate retrieval from a targeting index, then filtering and ranking by expected value
  • The auction and pricing rule, and a fallback when nothing qualifies
  • Propagation of campaign changes and how stale serving data may be

Part 2: Tracking, budgets and pacing

Explain how impressions and clicks are captured, deduplicated and converted into spend. Describe how that spend is fed back so a campaign stops before overspending, and how a daily budget is spread across the day rather than used up in the first hour.

Cover:

  • Durable event logging, signed tracking links and deduplication
  • Real-time spend aggregation versus exact billing from the event log
  • Budget enforcement across many servers, and the overspend you tolerate
  • Pacing across the day

Hints

Hint 1

Split the system into two paths: a synchronous decision path that must finish in tens of milliseconds, and an asynchronous event path that can lag by seconds. Keep anything slow or heavy off the first path.

Hint 2

Ranking by bid alone is wrong. Think about expected revenue per impression (for example bid times predicted click probability for pay-per-click ads), and consider what price the winner should pay.

Hint 3

Budget counts will always be slightly stale when many servers serve in parallel. Design for bounded overspend: for example, give each server a small allowance of budget, and throttle serving probability so spend follows a target curve across the day.