SysDesignAtlas

Design an Ad Click Aggregator

Stream processing at enormous scale with a hard requirement most systems don't have — the numbers are money, so approximate isn't good enough.

HLDHard11 min readcase-studystreamingkafkaanalytics
Confidence

#1. Requirements

Functional

  • Ingest click and impression events from ad serving.
  • Advertisers see near-real-time metrics (clicks, impressions, spend) by campaign, time, and dimensions like geography and device.
  • Support arbitrary time-range queries and drill-down.
  • Detect and exclude fraudulent clicks.

Non-functional

  • Correctness matters — these numbers drive billing.
  • Dashboard latency: metrics visible within ~1 minute.
  • Query latency: sub-second for dashboards.
  • Handle late and out-of-order events (mobile devices go offline and upload later).
  • No event loss.

Clarifying questions: How fresh must the dashboard be — seconds or minutes? Is the same pipeline used for billing? What's the acceptable window for late events? Do we need per-user uniqueness (unique reach), which is much harder than counting?

#2. Estimation

10B clicks/day, 100B impressions/day.

   Clicks:      10¹⁰ / 10⁵ s  = 100,000/s avg → 300,000 peak
   Impressions: 10¹¹ / 10⁵ s  = 1,000,000/s avg → 3,000,000 peak

   Event size: ad_id 8 + campaign 8 + user 8 + ts 8 + geo 4
               + device 4 + cost 8 + ~50 B context ≈ 100 B

   Raw ingest: 1.1M events/s × 100 B = 110 MB/s
               ≈ 9.5 TB/day raw

   Kafka retention (7 days): ~67 TB   ← affordable, and it's
                                        your replay safety net

   AGGREGATED output (the point of the system):
     1M campaigns × 24 hourly buckets × 10 dimensions
     ≈ 240M rows/day × 100 B = 24 GB/day
     → 400× reduction. This is what makes queries fast.

What this decided: 1.1M events/s means the ingest layer must be a partitioned log, not a database. The 400× reduction from aggregation is why the architecture exists — you never query raw events for a dashboard.

#3. High-level design

   ad servers ──► ┌──────────────┐
   (global)       │  Ingest API  │ validate, enrich, dedupe-tag
                  └──────┬───────┘
                         ▼
              ┌──────────────────────┐
              │  KAFKA (partitioned  │  7-day retention
              │  by campaign_id)     │  ← the buffer AND
              └──────┬───────────────┘     the replay log
                     │
        ┌────────────┴────────────────┐
        ▼                             ▼
   ┌──────────────┐            ┌──────────────┐
   │ SPEED layer  │            │ BATCH layer  │
   │ Flink        │            │ hourly/daily │
   │ windowed agg │            │ exact recompute
   │ → ~1 min     │            │ from raw     │
   └──────┬───────┘            └──────┬───────┘
          ▼                           ▼
   ┌──────────────┐            ┌──────────────┐
   │ Real-time    │            │ Warehouse    │
   │ OLAP store   │            │ (exact,      │
   │ (approximate,│            │  billing)    │
   │  fast)       │            └──────────────┘
   └──────┬───────┘                   │
          └─────────┬─────────────────┘
                    ▼
             ┌──────────────┐
             │  Query API   │ recent → speed layer
             │              │ historical → batch layer
             └──────────────┘

Partition Kafka by campaign_id. All events for one campaign land on one partition, so aggregation per campaign is local — no cross-partition coordination. The cost is a hot partition risk: one very large advertiser's campaign could produce more events than a single partition can handle. Mitigate by sub-partitioning — add a small bucket number to the key (campaign_id:0 … campaign_id:9) so that campaign's events spread over ten partitions, and add the ten partial counts together afterwards.

#4. Deep dive — why two layers

This is the central design decision, and the reasoning matters more than the diagram.

   SPEED LAYER          fast, approximate, may double-count
                        under failure. Powers the dashboard.

   BATCH LAYER          slow (hourly), exact, deduplicated,
                        reprocessible. Powers BILLING.

Why not just the speed layer? Stream processing under failure gives at-least-once semantics. A worker crash and restart can re-emit counts. For a dashboard, a 0.1% error is invisible. For an invoice, it's a dispute and potentially a legal problem.

Why not just the batch layer? Advertisers need to see whether a campaign is working now, not an hour from now. A campaign burning budget on a broken creative needs to be caught in minutes.

The modern alternative worth mentioning: kappa architecture — keep only the streaming path, and when the logic changes, replay the retained event log through the new version rather than maintaining a separate batch implementation. Simpler to maintain, and viable here because Kafka already holds 7 days of raw events. The catch is deduplication: to be exact, the stream processor must remember every event ID it has seen for as long as a duplicate could still arrive — potentially a day or more of IDs at a million events a second — which is a lot of state to keep and checkpoint.

#5. Deep dive — exactly-once and deduplication

The hard correctness problem.

Sources of duplicates:

  1. The ad server retries a failed ingest call.
  2. The client fires the same click event twice (double-tap, retry).
  3. A stream processor reprocesses after failure.

Layered defense:

   1. EVENT ID at the source. The ad server generates a UUID
      per impression/click. Duplicates share the ID.

   2. INGEST DEDUP: check the ID against a short-window store
      (Redis, 24h TTL) or a Bloom filter in front of it.
      Catches network-level retries cheaply.

   3. STREAM DEDUP: Flink keyed state per (campaign, event_id)
      within the watermark window. Exactly-once *within* the
      streaming pipeline via checkpointing.

   4. BATCH DEDUP: the authoritative pass. Deduplicate on
      event_id across the full raw dataset. Slow, exact.

Layer 3 in plain terms: Flink remembers the event IDs it has seen for each campaign (its keyed state), but only for as long as late events are still accepted — the watermark window explained in the next section. It regularly saves that state in a checkpoint, so after a crash it resumes with its memory of seen IDs intact rather than starting blank.

Note that layer 4 is the one that makes billing correct. Layers 1–3 make the streaming numbers close.

Fraud filtering is a related but separate concern: bot traffic, click farms, and repeated clicks from one user must be excluded before billing. Run it as a stage in the batch layer (where you can afford to be expensive and look at wider context), with a cheap heuristic filter in the stream layer. Advertisers are billed on valid clicks, and this distinction is worth naming.

#6. Deep dive — windowing and late events

Events arrive out of order. A mobile device offline for 3 hours uploads its events afterwards. Which time bucket do they belong to?

   EVENT TIME  — when the click actually happened  ← use this
   PROCESSING TIME — when your system saw it       ← not this

Always aggregate by event time, or your hourly numbers shift depending on network conditions, and reprocessing produces different results than the original run.

Watermarks handle the "how long do I wait?" question:

   A watermark asserts: "I believe I've seen all events with
   event_time <= T."

   Window [10:00-11:00) closes when the watermark passes 11:00
   (plus an allowed lateness, say 10 minutes).

   Events arriving after that are LATE:
     - within allowed lateness → update the window, re-emit
     - beyond it → route to a side output for the batch layer

The tradeoff to state: longer allowed lateness means more complete results and higher latency plus more state held in memory. Shorter means faster dashboards and more corrections later. A common answer: ~10 minutes of lateness in the stream, with the batch layer catching everything within 24 hours.

Never drop late events silently. Route them somewhere. The batch layer will pick them up, and silently discarding billable events is the failure mode nobody notices until an advertiser audits you.

#7. The serving layer

   Pre-aggregate at multiple granularities:
     minute → hour → day → month

   Dashboards query the coarsest granularity that answers
   the question. "Last 30 days" reads 30 daily rows,
   not 43 million minute rows.

Use a real-time OLAP store (Druid, Pinot, ClickHouse): they ingest directly from Kafka, pre-aggregate on write, and serve sub-second aggregate queries. This is exactly the workload they exist for.

Dimensions are the cost driver. Supporting drill-down by campaign × geo × device × creative × hour multiplies row counts combinatorially: one campaign shown in 200 countries on 5 device types with 10 creatives is already 10,000 rows per hour, before you add any other dimension. Pre-aggregate the common combinations; compute rare ones on demand from a finer granularity. Naming the cardinality explosion is a good depth signal, because it's the thing that makes these systems expensive.

Approximate where it's safe: HyperLogLog for unique reach (counting distinct users exactly requires storing every user ID per campaign — enormous), and it's mergeable: each HyperLogLog is a small fixed-size summary (a sketch), and you can combine Monday's sketch with Tuesday's to get the unique count across both days without going back to the raw events. But use exact counts for clicks and spend, because those are billed.

#8. Failure modes & follow-ups

FailureBehaviour
Ingest API downAd servers buffer locally and retry. Some loss if the outage is long — hence local buffering with generous capacity.
Kafka partition unavailableProducers retry; ingest blocks for that partition. Replication (RF=3) makes this rare.
Flink job crashesRestarts from the last checkpoint and reprocesses. May emit duplicates downstream — which is exactly why billing doesn't come from here.
OLAP store downDashboards fail; ingestion continues into Kafka. Recovery replays from the log. Decoupling ingest from serving is what makes this survivable.
Batch job failsRerun it. Kafka retention is the safety net, which is why 7 days rather than 1.
Sudden traffic spikeKafka absorbs it as a deeper queue; consumers lag and catch up. This is the buffering benefit of a log, and worth stating.

Follow-ups:

  • "How do you handle a hot campaign?" — sub-partition the key (campaign:0…campaign:9) and sum on read. Same fix as any hot shard.
  • "Reduce cost?" — sample impressions (they're 10× clicks and less valuable individually), but never sample clicks, since those are billed. Choosing what to approximate by business value is the right instinct.
  • "Budget enforcement — stop a campaign at its cap?" — this needs low latency and can't wait for batch. Maintain a fast counter of remaining budget in the ad-serving path, and stop serving a little before it reaches zero (a conservative buffer) to allow for clicks still in flight. Then reconcile exactly in batch. If the campaign still overspends slightly, the platform usually absorbs that cost rather than stopping campaigns early and under-delivering what advertisers paid for.
  • "Multi-region?" — aggregate locally per region, then merge. Sums and counts are commutative and associative — the order and grouping in which you add numbers doesn't change the total — so adding up each region first and then adding the regional totals gives exactly the same answer. That's a nice property to point out.

#Check yourself

What makes the ad click aggregator different from most analytics?Show answer
The numbers are billed to advertisers, so approximate isn't good enough — yet advertisers also need to see results within about a minute.
Why does the architecture exist, in one number?Show answer
Aggregation shrinks ~9.5 TB of raw events a day to ~24 GB of rollups — about 400× — so dashboards never query raw events.
Why both a speed layer and a batch layer?Show answer
Streaming under failure is at-least-once and may double-count — fine for a "preliminary" dashboard. Billing comes from an exact, deduplicated batch recomputation over the raw log. That's lambda architecture; its cost is two codebases.
What is kappa architecture?Show answer
Only a streaming path; when logic changes, replay the retained event log through the new version instead of maintaining a separate batch implementation.
Name the four layers of click deduplication.Show answer
An event ID at the source, an ingest check against a short-window store or Bloom filter, keyed stream state within the watermark window, and the authoritative batch dedup across all raw events.
How should late events be handled?Show answer
Aggregate by event time with a watermark plus allowed lateness (~10 minutes); later events go to a side output the batch layer picks up. Never drop billable events silently.
How does the serving layer stay fast?Show answer
Pre-aggregate at minute, hour, day and month and query the coarsest that answers; use a real-time OLAP store; pre-aggregate common dimension combinations to tame cardinality.
What may you approximate, and what not?Show answer
Unique reach via mergeable HyperLogLog sketches, and sampled impressions; never clicks or spend, which are billed.

#What to carry forward

  • Billing-grade numbers need an exact path; dashboards can use a fast, approximate one.
  • Kafka partitioned by campaign is both the buffer and the replay log — keep a week.
  • Deduplicate in layers; only the batch pass makes billing correct.
  • Aggregate by event time with watermarks; route late events, never drop them.
  • Pre-aggregate at several granularities and approximate only what isn't billed.
00:00
/ 11m