Preparing your design workspace

Loading the problem and your latest saved attempt…

Design a Top-N Ad Analytics System

Design a streaming analytics system that ranks ads over recent time windows while handling retries, late events, skew, and corrections.

Problem statement

For this exercise, assume an advertising platform receives impression and click events from serving systems and lets internal analysts request the top N ads for a selected metric. Every event carries a globally unique event ID, ad ID, event type, event time, market, and device class. A retried event must count once.

Analysts can rank ads by impressions, clicks, or click-through rate over rolling 5-minute, 1-hour, and 24-hour windows, optionally filtering by market and device class.

Results are ordered by the selected metric descending and then ad ID ascending, return up to 100 ads, and include counts, the computed metric, window bounds, a data watermark, a monotonically increasing revision, and one of three lifecycle states.

OPEN_PROVISIONAL means the watermark has not passed the window end and the ranking may be approximate or incomplete.

CLOSED_CORRECTABLE begins when the watermark passes the window end; each published revision is exact for all accepted data covered by that watermark, but an accepted event may still produce a newer exact revision until the correction deadline at window end plus 24 hours.

FINALIZED_IMMUTABLE begins only after that deadline and a successful coverage check, and ordinary late arrivals must not mutate it. Readers must be able to distinguish every revision and state. Design for five million ad events per second at peak, twenty million active ads, and ten thousand ranking queries per second.

Fresh events should influence open provisional rankings within 30 seconds at p95, healthy queries should finish within 400 milliseconds at p95, and accepted events plus finalized immutable aggregates must survive a single-zone failure.

Retain raw accepted events for seven days, finalized immutable window aggregates for ninety days, and make serving indexes rebuildable. Exclude ad serving, auctions, billing, attribution across user journeys, fraud-model training, and the ad creative itself.

What to build

  1. The system can accept impression and click events idempotently and preserve enough event-time information to process retries, out-of-order delivery, and bounded late arrivals.
  2. An analyst can request up to 100 top ads for a supported metric and rolling window, with optional market and device filters and deterministic ordering.
  3. Every result exposes its window, watermark, monotonically increasing revision, and OPEN_PROVISIONAL, CLOSED_CORRECTABLE, or FINALIZED_IMMUTABLE state so readers can distinguish estimates, exact-but-correctable snapshots, and immutable results.

At scale

  • For this exercise, assume five million impression and click events per second at peak across twenty million active ads, with a highly skewed popularity distribution.
  • For this exercise, assume ten thousand ranking queries per second, up to 100 returned ads, 400 milliseconds p95 query latency, and 30 seconds p95 event-to-provisional-result freshness.
  • For this exercise, close an open window when its watermark passes the end, accept revision-producing arrivals only until the correction deadline 24 hours after that end, then finalize it as immutable after a coverage check; retain accepted raw events for seven days and finalized immutable aggregates for ninety days.
  • For this exercise, accepted events and finalized immutable aggregates must survive a single-zone failure, while a regional analytics outage may return an explicit stale or unavailable result rather than fabricate completeness.