Count Every Click

Design the backend that counts ad clicks for a large ad network and reports them to advertisers.

Requirements

Functional:

  • Accept a click event from the ad redirect endpoint and send the user on to the advertiser landing page.
  • Produce per-minute and per-hour click counts grouped by ad and by advertiser.
  • Count a click once even when the same event is delivered several times.
  • Accept events that arrive hours after they happened and still place them in the correct bucket.
  • Support recomputing the last 30 days of counts after a bug in the counting logic, without double counting.

Non-functional:

  • The redirect adds at most 50 ms at p99 and never fails because the analytics path is unhealthy.
  • A minute bucket is visible in the dashboard within 60 s of that minute closing.
  • Counts drive billing, so a bucket older than 6 hours must be exact, not approximate.

Scale

  • 1M click events per second at peak, 400k per second on average (34.6B per day).
  • Event payload roughly 400 bytes of JSON.
  • 10M active ads across 500k advertisers.
  • 50k advertisers open the dashboard daily: about 500 report queries per second at peak, each covering up to 30 days.
  • Raw events retained 13 months for audit, rollups retained indefinitely.

Out of scope

Ad selection and auction, budget pacing, fraud scoring models, and invoice generation. Assume every click already carries an ad id, an advertiser id, and a click id.