← Back to System Design, page 4

system-design-0061 · Hard senior

Design an ad click aggregation system that counts 2 billion clicks per day per ad per minute, for billing and dashboards, with exactly-once results.

Click events go into a partitioned log such as Kafka; a stream processor such as Flink groups them by ad ID into one-minute event-time windows, using watermarks for late events, and writes aggregates to an OLAP store. Exactly-once comes from deduplicating click IDs plus transactional or idempotent writes of window results. A daily batch job recomputes from raw events to reconcile billing.

Full answer

Requirements

Functional:

  • Count clicks per ad per minute (and roll up to hour and day).
  • Query: clicks for ad X between two times; top 100 ads in the last hour.
  • Filter invalid clicks (bots, duplicates).

Non-functional:

  • Counts are correct for billing: no double counting, no loss.
  • Dashboards update within about a minute.
  • Handle bursts and late events (mobile clients send clicks minutes late).

Capacity estimates

Assumptions: 2B clicks per day, peak factor 3, 200 bytes per raw event, 1M active ads.

  • Clicks: 2B / 86,400 = about 23,000 per second; about 69,000 per second at peak.
  • Raw data: 2B x 200 bytes = 400 GB per day, kept for audit and reprocessing.
  • Aggregates: at most 1M rows per minute (one per active ad that got a click), usually far fewer.

API

POST /v1/clicks (from ad servers) {"click_id", "ad_id", "user_hash", "ts", "ip", "ua"} GET /v1/ads/{ad_id}/clicks?from=2026-12-01T10:00Z&to=2026-12-01T11:00Z&granularity=minute GET /v1/ads/top?window=1h&limit=100

Data model

  • Raw events: Kafka topic partitioned by ad_id, then archived to object storage (Parquet, partitioned by date and hour).
  • Aggregates table: (ad_id, minute) primary key, clicks, valid_clicks, updated_at, in an OLAP store such as ClickHouse, Druid or Pinot.
  • Dedup state: recent click_ids within the stream processor's keyed state (with a TTL of a few hours).

High-level design

mermaid
flowchart LR
  AS[Ad servers] --> K[(Kafka: clicks)]
  K --> F[Stream processor]
  F --> AGG[(OLAP aggregates)]
  K --> ARC[(Raw archive)]
  ARC --> B[Daily batch recompute]
  B --> REC[Reconciliation]
  AGG --> REC
  AGG --> Q[Query service]
  Q --> D[Dashboards and billing]

Stream processing details

  1. Key by ad_id so all clicks of one ad go to one task.
  2. Event-time windows of one minute, using the click's own timestamp, not arrival time.
  3. Watermarks declare "events older than T are complete", typically T = now minus 2 minutes. A window is emitted when the watermark passes its end. Events later than that update the window again (allowed lateness) or go to a side output for the batch job.
  4. Deduplicate click_id in keyed state before counting, since ad servers may retry.
  5. Exactly-once output: the processor checkpoints its state and Kafka offsets together. Window results are written with an upsert keyed by (ad_id, minute), so replaying after a failure overwrites the same row instead of adding twice.

Hot ads (a viral campaign) can be split with a two-stage aggregation: count per (ad_id, random 0 to 9) first, then sum the ten partial counts.

Bottlenecks and trade-offs

  • Lateness versus freshness. A longer watermark delay catches more late events but delays dashboards.
  • Lambda-style reconciliation. Streaming gives fast numbers; the daily batch over raw events is the billing truth. Differences above a threshold raise alerts.
  • Invalid click filtering (bots, click farms) needs features over time; run a quick rule filter in the stream and a deeper model in batch, adjusting bills later.
  • Kafka partitions set the parallelism ceiling; choose enough (for example 256) for growth.
  • OLAP store pre-aggregates minute rows into hours and days to keep queries fast.

By level

Fresher: Describe clicks flowing through a queue into a job that counts them per minute and stores the results. A fresher should know why duplicates must be removed for billing.

Mid: Design event-time windows with watermarks, deduplication and idempotent writes, and do the capacity math. Add an OLAP store for queries.

Senior: Discuss lateness trade-offs, two-stage aggregation for hot ads, reconciliation with batch, invalid traffic and replay after bugs. Expect a precise account of how exactly-once is achieved end to end.

Key points

  • Partitioned log in, stream processor out
  • Event-time windows with watermarks for late clicks
  • Deduplicate click IDs and upsert window results for exactly-once
  • Two-stage aggregation for hot ads
  • Batch recompute from raw events reconciles billing

Common mistakes

  • Counting by arrival time instead of event time
  • Incrementing counters in a database on every click
  • Ignoring retries that duplicate click events
  • Having no raw archive to recompute from

Follow-ups

  • How would you handle a click that arrives two hours late?
  • How would you compute unique users per ad?
  • How do you recover if the stream job has a bug for a day?

Tips

  • Explain event time and watermarks with a timeline
  • Show how upserts make replays safe
  • Mention reconciliation for billing

Evaluation rubric

  • Correct pipeline design
  • Correct windowing and late-event handling
  • Exactly-once reasoning
  • Hot keys and reconciliation covered