Design an Advertising Frequency-Capping Service
Design a low-latency service that prevents one viewer from receiving an advertisement more often than its configured caps allow.
Problem statement
For this exercise, assume an ad-serving platform must enforce frequency caps before showing an advertisement.
An authorized operator publishes versioned policies for a campaign; each policy contains one or more rolling-window rules such as a maximum number of impressions per viewer and campaign over a configured duration. The ad server calls a reserve operation with a stable request ID before rendering.
The service grants one short-lived permit only when every active rule has capacity, and a retry of the same request must return the original outcome. The caller then confirms the impression or cancels the permit; an unconfirmed permit expires after two minutes and releases its reserved capacity.
New reservations use the current policy version, while an issued permit remains bound to the version that granted it. At any instant, committed impressions plus live permits for a viewer, campaign, policy version, and window must not exceed that rule's cap.
Design for two million reserve decisions and two hundred thousand confirmations per second globally, up to two billion active viewer-campaign keys per day, and policy windows no longer than twenty-four hours.
Healthy in-region reserve decisions should complete within 20 milliseconds at p99, policy changes should reach healthy decision nodes within 60 seconds, and acknowledged permits and confirmations must survive a single-zone failure.
When the authoritative counter state is unavailable or uncertain, return an explicit unavailable outcome rather than silently granting. Exclude ad ranking, auctions, targeting, creative selection, billing, identity resolution, and privacy-consent collection.
What to build
- Authorized operators can publish, activate, and inspect versioned campaign frequency-cap policies.
- An ad server can obtain one retry-safe granted, capped, or unavailable decision before rendering an impression.
- A caller can idempotently confirm or cancel a permit, while abandoned permits expire and release capacity.
- Operators can inspect a decision's policy version, applicable windows, outcome, and settlement state for debugging and reconciliation.
At scale
- For this exercise, assume two million reserve decisions and two hundred thousand permit confirmations per second globally, with up to two billion active viewer-campaign keys per day.
- For this exercise, target 20 milliseconds p99 for healthy in-region reserve decisions and propagate an activated policy to healthy decision nodes within 60 seconds.
- For this exercise, cap windows are no longer than twenty-four hours, unconfirmed permits expire after two minutes, and decision audit records are retained for seven days.
- For this exercise, acknowledged permits and confirmations survive a single-zone failure, and uncertain authoritative counter state produces an explicit unavailable result rather than a grant.