KatInterview

Frequency Capping at 500K QPS: Low-Latency Ad Eligibility

System DesignModel answer
MediumModel answerSystem Designsystem-designrate-limitingdistributed-systems

Last updated

Design a sharded, in-memory service that decides in under 10 ms whether a viewer has hit ad/campaign caps, with async impression counting and deliberate soft-cap tolerance.

Context

A streaming platform's ad stack must limit how often the same viewer is shown a given ad, campaign, or advertiser within time windows such as "at most 2 impressions per viewer per hour" and "at most 5 per viewer per day." The ad decision service (ADS) calls this component on every ad pod, before it runs its auction, and drops any candidate that is already capped. After an ad is actually played, the platform must record the impression so future checks see it.

Design this frequency-capping (FC) service end to end.

Functional Requirements

  • Check: given a viewer (profile, device, or household identifier) and a batch of candidate ads with their ad/campaign/advertiser IDs, return which candidates are still eligible.
  • Record: given a confirmed impression, apply it to every window that the relevant rules cover.
  • Flexible rules: caps may be scoped to ad, campaign, or advertiser, at profile or household level, and applied over rolling 1h, rolling 24h, calendar-day (viewer's timezone), and possibly multi-day windows. Rules are configurable without redeploying the service.

Non-Functional Requirements

  • Latency: check returns at p99 < 10 ms; the whole FC call sits inside a tight ad-pod budget (assume ~5 ms of the budget).
  • Availability: 99.999% for the service; a degraded FC must never delay or break video playback — ads are the only thing at stake.
  • Throughput: 500K QPS checks, each carrying ~20–50 candidates; ~50K QPS impressions.
  • Consistency: soft caps are the default and a small amount of over-delivery (order 1%) is acceptable; a small set of contractually hard caps must never be exceeded by more than a negligible amount.

What to Cover

  1. API contract for check and record.
  2. Counting data structures and storage footprint estimates.
  3. Component architecture and read/write paths, including how the race between check and impression is closed.
  4. Sharding, hot keys, and multi-region layout.
  5. Failure policy: when to fail open, when to fail closed, and how consumers learn the system degraded.

Hints

Hint 1

Do the arithmetic first: 500K checks × 30 candidates is ~15M counter evaluations per second, so define one key per viewer that holds everything and is read in a single round trip.

Hint 2

Don't store one timestamp per impression. Bucketed ring counters (e.g. 5-minute buckets for the 1h window, 1-hour buckets for 24h) bound memory and the error costs at most one bucket of under-delivery.

Hint 3

The beacon from the client arrives after the ad is chosen, so two back-to-back pods can both pass the check. Close that gap with a short-TTL optimistic increment written at selection time, then reconciled by the real impression event.

Hint 4

Separate soft from hard caps in the failure policy: fail open with a degraded flag for soft caps, fail closed (filter the candidate) for hard ones, and never let either path block playback.