KatInterview

Billing-Grade Click Event Pipeline at 10M Events per Second

System DesignModel answer
HardModel answerSystem Designsystem-designstreamingkafka

Last updated

Design a pipeline that records ad clicks at extreme scale, drops repeat clicks within 30 seconds, serves windowed counts per ad, and never loses billable data.

Problem

An advertising platform needs a backend that captures every click on every ad. Peak traffic reaches 10 million clicks per second. Recording a click should feel near-instant (well under a second), while the numbers shown in dashboards and used for invoices may lag slightly behind reality (eventual consistency).

Click event

Each event carries:

(click_id, user_id, ad_id, timestamp, device_info)

What the system must do

  • Ingest click events reliably.
  • Answer queries for the number of clicks on a given ad_id across different time windows (for example per minute, hour, or day).
  • Suppress repeats: if the same user clicks the same ad several times within a 30-second window, only one click is counted.

Quality requirements

  • Availability: traffic spikes must not cause events to be dropped.
  • Scalability: sustain 10M clicks/s at peak.
  • Correctness: billing figures must be accurate. Losing data is not acceptable.

What to cover in your answer

  1. A high-level architecture (entry points, queues, processing services, storage, query path).
  2. The data model, separating raw events from aggregated metrics.
  3. How you partition and shard data so that a single viral ad does not create a hotspot.
  4. How you cope with failures in downstream components, and where you can realistically offer exactly-once behavior.
  5. Your technology and pattern choices (for example NoSQL vs. time-series/OLAP stores, streaming vs. batch) and the trade-offs behind them.

State any assumptions you make about freshness, query shapes, and late or out-of-order events.

Hints

Hint 1

Separate two kinds of duplicates: the same click_id delivered twice by retries, and different clicks from one user on one ad within 30 seconds. Each needs its own mechanism, and the 30-second rule should be judged on event time.

Hint 2

Do the back-of-envelope math first (bytes per event, GB/s at peak, Kafka partitions, size of dedup state). Then pick a partition key that spreads load evenly and keeps one user's clicks together, so dedup stays local.

Hint 3

Aggregating by ad_id concentrates a viral ad on one worker. Think about pre-aggregating locally before the shuffle, salting hot keys, and idempotent upserts. For billing, keep an immutable raw log and recompute from it in batch.