System design case study C20 · Streaming · OLAP

Design an Ad-Click Aggregation System

Count billions of ad clicks per day, per ad, per minute, accurately enough to bill advertisers, and fast enough to show live dashboards.

What you'll be able to explain
  • Build a streaming aggregation pipeline with event-time windows
  • Achieve exactly-once counting end to end
  • Handle late and duplicate events
  • Serve fast analytical queries over fresh data
  • Reconcile streaming results with batch recomputation

Scope

Functional

  • Ingest click (and impression) events.
  • Query aggregated counts per ad / campaign / advertiser per minute, with filters (country, device).
  • Top-N most-clicked ads in the last M minutes.

Non-functional

  • Dashboard freshness ~1 minute. Billing accuracy: no double counting, no loss.
  • 10B clicks/day → ~115k/s average, ~500k/s peak.
  • Late events accepted up to ~1 hour (mobile apps buffer clicks offline).

Size

  • Event ≈ 200 B → 2 TB/day raw.
  • Aggregates: ~2M active ads × 1,440 minutes × a few dimensions → billions of small rows per day, which a columnar OLAP store handles.

The pipeline

CLIENTClick redirector SDKSERVICEIngest APIvalidate, assign idQUEUE / LOGKafkaraw clicksWORKERStream jobdedupe + windowQUEUE / LOGKafkaminute aggregatesDATABASEOLAP storeClickHouse / PinotOBJECT STORERaw archiveobject storageWORKERDaily batchrecomputeCLIENTDashboards / billingcorrections
Click events stream into Kafka, are aggregated by event time in Flink, and land in an OLAP store.

The 30-second summary

Say it in the interview

"Clicks are ingested with unique IDs into Kafka, deduplicated and fraud-filtered in a Flink job that aggregates in one-minute event-time windows with watermarks and allowed lateness. Exactly-once comes from checkpointed state, transactional output and idempotent upserts into ClickHouse. Hot ads are pre-aggregated, a separate job maintains top-N, and a daily batch over the raw archive reconciles the numbers used for billing."

Follow-up questions the interviewer may ask

  1. How do you avoid counting the same click twice?
  2. How do you filter click fraud?
  3. Advertisers want counts per minute for 90 days. What changes?
  4. How do you reconcile the real-time and batch numbers?

Model answers to each of these are in the book.