Module 45: System Design — Ad Click Aggregation & Event Analytics

Comprehensive architectural breakdown of a Real-Time Ad Click Aggregation and Analytics System (Google Ads, Meta Ads, TikTok), covering stream processing (Flink/Spark), windowing strategies (Tumbling, Hopping, Sliding), Watermark heuristics for late-arriving events, and click-fraud deduplication.


Prerequisites

Before tackling ad click aggregation, review these core modules:


ELI5 (30-Second Summary)

Imagine a busy highway with 1 billion cars driving past billboard ads every day. Every time a driver looks at an ad or visits a store, a penny is transferred from the advertiser to the billboard owner.

If your accounting system counted every single click on a separate piece of paper and billed advertisers one by one, your computers would melt under the paperwork. Even worse: what if a mischievous competitor writes a bot that clicks your billboard 10,000 times in 10 seconds (Click Fraud)? Or what if a driver drives through a mountain tunnel with no cell service, and their click arrives 15 minutes late (Late-Arriving Events)?

An Ad Click Aggregation System groups clicks into tidy 1-minute buckets (Tumbling Windows), filters out robotic duplicate clicks, and uses an intelligent clock (Watermarks) to count late-arriving clicks accurately so advertisers are billed fairly to the exact penny!


Physical Analogy

Think of a Rain Gauge Measuring Rainfall During a Storm:

Rain Gauge Analogy Ad Click Aggregation Concept Technical Description
Individual Raindrops Falling Raw Ad Click Events High-velocity click streams ingested into Apache Kafka.
Marked 1-Minute Measurement Beaker Tumbling Window (1-Minute) Non-overlapping fixed time window aggregating metric counts.
Water Splashing from Shaken Trees (Delayed Drops) Late-Arriving Events (Watermark Lag) Events delayed by mobile cell handoffs or airplane offline modes.
Mesh Filter Straining Leaves and Debris Click Fraud & Bot Filter (Bloom Filter) Identifies duplicate or robotic clicks from the same IP/Device.
Logging Total Inches in the Farm Logbook Analytical OLAP DB (ClickHouse / Druid) Stores summarized aggregation tables for real-time advertiser dashboards.

The Problem It Solves

Digital advertising engines (Google Search Ads, Instagram Sponsored Posts) power multi-billion dollar ecosystems:

  1. Financial Integrity: Advertisers pay real money per click (Cost-Per-Click: CPC). Over-counting due to network retries causes advertiser lawsuits; under-counting loses revenue.
  2. Massive Scale: Platforms process 10 billion ad impressions and 1 billion clicks daily (> 50,000 clicks/second peak).
  3. Event Time vs. Processing Time Discrepancies: A user clicks an ad at 14:00:00 on a subway train; their phone loses signal and delivers the click payload at 14:12:30. The system must attribute the click to the 14:00:00 window for accurate analytics.

Visual Architecture

Real-Time Ad Click Aggregation (Flink Exactly-Once Pipeline)

flowchart TD
    subgraph Clients["1. Ingestion Layer"]
        UserPhone["User Mobile / Web Client"] -->|"POST /v1/ad_clicks"| LB["Global Load Balancer"]
        LB --> EdgeGW["Click Tracking Ingestion Service"]
        EdgeGW --> KafkaRaw["Kafka: raw-clicks-topic (Partitioned by ad_id)"]
    end

    subgraph StreamEngine["2. Real-Time Stream Aggregation (Apache Flink / Spark)"]
        KafkaRaw --> DedupFilter{"Bot & Duplicate Filter (Redis / Bloom Filter)"}
        DedupFilter -->|"Fraud / Duplicate"| Discard["Log & Quarantine"]
        DedupFilter -->|"Valid Click"| WindowEngine["Flink Window Engine (Event-Time Watermarks)"]
        
        WindowEngine --> Tumbling1m["Tumbling Window 1-Minute Aggregation"]
        Tumbling1m --> CountAgg["SUM(clicks), SUM(spend), Unique Users"]
    end

    subgraph Storage["3. Real-Time OLAP & Billing"]
        CountAgg --> KafkaAgg["Kafka: aggregated-clicks-topic"]
        KafkaAgg --> OLAP[("ClickHouse / Apache Pinot (Advertiser Dashboard)")]
        KafkaAgg --> BillingDB[("Double-Entry Billing DB (PostgreSQL / Spanner)")]
    end

Real Numbers & Concurrency Sizing

Consider an ad network handling 1 Billion Ad Clicks Daily:


Deep Dive: Under the Hood

1. Three Types of Streaming Windows

2. Event Time vs. Processing Time & Watermarks

W(t) = \max(Event Time seen so far) - 5 minutes

3. Click-Fraud & Bot Detection

Fraudulent networks generate fake clicks to drain competitor ad budgets.


Trade-offs & Decision Matrix

Dimension Lambda Architecture (Batch + Stream) Kappa Architecture (Pure Streaming - Flink)
Code Maintenance Painful (must maintain dual codebases in Spark & Flink) Elegant (single processing logic in Flink)
Historical Reprocessing Easy (re-run MapReduce batch on raw files) Requires replaying Kafka historical retention
Real-Time Accuracy High (batch job corrects streaming inaccuracies nightly) High (watermarks handle late events cleanly)
System Complexity Very High Moderate

Common Pitfalls & Anti-Patterns

  1. Relying on Processing Time for Billing: If Kafka experiences an ingestion backlog, 2 hours of queued clicks will all be processed within 1 minute. Using processing time attributes 2 hours of clicks to a single 1-minute billing bucket, corrupting advertiser analytics. Always aggregate on Event Time.
  2. Missing Late-Event Side Outputs: Closing windows strictly and dropping late clicks causes under-counting for mobile users on intermittent transit Wi-Fi. Always configure side outputs in Flink to route late events to reconciliation tables.
  3. Synchronous Database Writes per Click: Writing 35,000 individual SQL inserts per second overwhelms relational databases. Ingest through Kafka, aggregate in memory with Flink, and bulk-insert minute-level summaries into ClickHouse.

Try It Yourself

A runnable, zero-dependency Python simulation demonstrating tumbling window click aggregation, event-time watermark evaluation, late-event side output handling, and bot click deduplication is included in this directory.

Run the simulation:

python demo.py

Production References & Further Reading


Top 5 Interview Questions with Model Answers

Q1: What is the difference between Event Time, Ingestion Time, and Processing Time?

Model Answer:

Model Answer: A Watermark is a monotonically increasing timestamp emitted into the stream that acts as a completion horizon: W(t) = \max(Event Time seen) - Allowed Latency Δ.

Q3: How do you achieve Exactly-Once Processing in an ad click aggregation pipeline?

Model Answer: Exactly-once processing is achieved through end-to-end alignment across three pipeline stages:

  1. Replay-able Source: Apache Kafka retains raw click offsets.
  2. Stateful Stream Processing with Checkpointing (Chandy-Lamport Algorithm): Flink periodically saves consistent snapshots of aggregation state and Kafka read offsets to durable storage (S3/GCS). If a worker crashes, Flink restores state and rewinds Kafka to the last checkpoint.
  3. Idempotent / Transactional Sink: When writing aggregated summaries to ClickHouse or PostgreSQL, writes use Upserts on compound keys (ad_id, window_start_time) or two-phase commit sinks (TwoPhaseCommitSinkFunction).

Q4: How do you detect and filter Click-Fraud in real time?

Model Answer: Click fraud is filtered in a multi-tier pipeline:

  1. Deduplication Filter (Fast Layer): When a click arrives, hash (user_id, ad_id, ip) and check an atomic Redis key with a 5-second TTL. Duplicate clicks within 5 seconds are marked invalid and excluded from billing.
  2. Behavioral Sliding Window (Anomaly Detection Layer): Flink maintains a sliding window tracking click velocity per IP subnet and user agent. If an IP exceeds 50 clicks/minute, subsequent clicks are routed to a fraud queue.
  3. Offline Machine Learning Classifier: A nightly batch job analyzes click-to-conversion rates, IP geolocation hops, and known botnet IP blacklists, issuing retroactive billing credits to advertisers for fraudulent clicks that bypassed real-time filters.

Q5: Why is ClickHouse or Apache Pinot preferred over PostgreSQL for storing aggregated ad analytics?

Model Answer:


Check Yourself Quiz

1. Which windowing strategy produces non-overlapping, fixed-duration time buckets? A) Sliding Window
B) Tumbling Window
C) Session Window
D) Hopping Window

Answer: B. Tumbling windows have fixed size and zero overlap (e.g. 12:00-12:01, 12:01-12:02).
2. Why is Event Time preferred over Processing Time for calculating advertiser ad charges? A) Because Processing Time cannot be stored in Kafka.
B) Because network delays and queue buffering would incorrectly shift clicks to later minutes, distorting billing attribution.
C) Because Event Time is always in UTC.
D) Because Processing Time requires GPS.

Answer: B. Event Time represents the true moment of user engagement, independent of network transit delays.
3. What does a Watermark timestamp W(t) signify to a stream processing operator? A) That the database is full.
B) That no further events with timestamp earlier than t are expected to arrive, triggering window completion.
C) That the server should reboot.
D) That the user clicked twice.

Answer: B. Watermarks provide a temporal progress indicator that allows windows to close and emit results.
4. How does 1-minute click aggregation reduce database storage costs? A) By deleting all ad clicks after 1 hour.
B) By combining thousands of raw click events for an ad into a single summary row per minute, achieving 95%+ storage reduction.
C) By compressing data with MP3.
D) By disabling SQL indexes.

Answer: B. Summarizing thousands of clicks into periodic count/sum rows drastically shrinks data footprint.
5. Which database type is best suited for real-time advertiser analytics dashboards? A) Relational Row Store (PostgreSQL / MySQL)
B) Distributed Columnar OLAP Store (ClickHouse / Apache Pinot)
C) Redis String Key-Value
D) MongoDB Document Store

Answer: B. Columnar OLAP stores provide sub-second aggregations (`SUM`, `AVG`, `GROUP BY`) across billions of rows.