Skip to main content
BLUEPRINT #03Media & Streaming

Design Real-Time Ad Click Event Aggregation

Target AWS Architecture:DynamoDBS3ElastiCacheSQS
10-Stage Structure:1. Requirements2. Sizing3. Topology4. Data Model5. AWS Topology6. Deep-Dive7. Failures8. SRE Playbooks

1. Problem Statement & Scope

System Mission

Design a mission-critical, low-latency, real-time ad click event ingestion and aggregation pipeline capable of processing hundreds of thousands of ad clicks per second, computing multi-granularity tumbling/sliding window metrics (1-minute, 1-hour, 24-hour CTR and spend), filtering click fraud and botnet attacks in real time, and maintaining strict exactly-once processing guarantees for advertiser billing.

Interactive Architecture Diagram
Synthesizing vector architecture diagram...

Functional Requirements

  1. Ad Click Ingestion: Securely collect, authenticate, and validate ad click tokens globally with sub-20 ms20\text{ ms} response times.
  2. Multi-Window Real-Time Aggregation: Aggregate click counts, conversion counts, and total monetary spend partitioned by ad_id, campaign_id, and publisher_id across 1-minute, 1-hour, and 24-hour sliding/tumbling windows.
  3. Real-Time Click Fraud & Botnet Filtering: Discard duplicate clicks, bot click-farms, and rapid replay clicks before aggregating billing figures.
  4. Advertiser Analytics Query API: Provide sub-50 ms50\text{ ms} query responses for advertiser campaign dashboards tracking Click-Through Rate (CTR) and budget pacing.
  5. Historical Replay & Data Lake Audit: Persist raw click streams in a Snappy-compressed Parquet data lake for regulatory reconciliation and ML model retraining.

Non-Functional Requirements (SLAs/SLOs)

  • High Throughput: Baseline 50,000 clicks/sec50,000\text{ clicks/sec}; peak capacity up to 200,000 clicks/sec\mathbf{200,000\text{ clicks/sec}}.
  • Aggregation Freshness / Pipeline Latency: Aggregated metrics reflected in advertiser dashboards within <3 seconds< 3\text{ seconds} (P99P99).
  • Data Accuracy & Billing Consistency: Strict Exactly-Once processing semantics. Zero duplicate billing for advertiser clicks.
  • High Availability: 99.999%99.999\% uptime for click ingestion endpoints.
  • Fault Tolerance: Lossless stream recovery with stateful checkpoints in <60 seconds< 60\text{ seconds} during worker failure.

Out-of-Scope

  • Real-Time Bidding (RTB) auctions and ad serving exchange matchmaking (sub-50 ms50\text{ ms} DSP/SSP ad bidding covered in dedicated ad serving blueprints).
  • Conversion attribution attribution modeling (e.g. multi-touch Shapley value attribution algorithms).

2. Capacity & Scale Estimation

Traffic & Event Volume Calculations

  • Daily Ad Impressions: 50 Billion50\text{ Billion} impressions/day.
  • Average Click-Through Rate (CTR): 2%    1 Billion clicks/day2\% \implies 1\text{ Billion clicks/day}. Average Ingestion QPS=109 clicks86,400 s11,574 clicks/sec\text{Average Ingestion QPS} = \frac{10^9 \text{ clicks}}{86,400 \text{ s}} \approx 11,574 \text{ clicks/sec} Peak Ingestion QPS (5× Super Bowl / Black Friday)=100,000 clicks/sec\text{Peak Ingestion QPS (5}\times\text{ Super Bowl / Black Friday)} = \mathbf{100,000\text{ clicks/sec}}

Storage & Data Sizing Math

  • Raw Click Event Payload Size: 500 Bytes\approx 500\text{ Bytes} (JSON/Protobuf: click_id, ad_id, campaign_id, user_token, ip_hash, cpc_cents, timestamp). Peak Ingestion Network Bandwidth=100,000×500 Bytes=50 MB/s=400 Mbps\text{Peak Ingestion Network Bandwidth} = 100,000 \times 500\text{ Bytes} = 50\text{ MB/s} = \mathbf{400\text{ Mbps}} Daily Raw Click Storage=109×500 Bytes=500 GB/day\text{Daily Raw Click Storage} = 10^9 \times 500\text{ Bytes} = 500\text{ GB/day} Snappy-Parquet Compressed Storage (4× compression)=125 GB/day    45.6 TB/year\text{Snappy-Parquet Compressed Storage (4}\times\text{ compression)} = \mathbf{125\text{ GB/day}} \implies 45.6\text{ TB/year}
  • Aggregated Metric Record Volume:
    • 100,000 active campaigns aggregated per minute     100,000 rows/minute=1,667 writes/sec\implies 100,000 \text{ rows/minute} = 1,667 \text{ writes/sec} to (vs. 100,000 raw writes/sec, a 98.3%98.3\% write load reduction).

3. High-Level Architecture & AWS Component Mapping

Interactive Architecture Diagram
Synthesizing vector architecture diagram...

Component Responsibility Breakdown

ComponentAWS TechnologyOperational Role & Configuration
Ingestion Load BalancingNetwork Load Balancer (NLB)Ultra-low latency layer 4 load balancing terminating TLS, distributing millions of concurrent TCP streams across ECS workers.
Click Ingestion FleetAmazon ECS Fargate (Go binary)Lightweight stateless HTTP receivers validating cryptographic click signatures, producing records to partitioned by hash(ad_id).
Event Streaming PipelineAmazon 64-shard stream partitioned by ad_id guaranteeing ordering per ad and absorbing massive traffic spikes.
Stream Processing EngineManaged Apache FlinkRocksDB state backend, event-time watermarking with 1-minute tumbling and 1-hour sliding windows, emitting aggregated deltas via sinks.
Deduplication & Fraud Filter In-memory (tracking unique click_id in 10-minute sliding windows) and atomic IP velocity sliding rate limiters.
Real-Time Aggregate StoreHigh-performance NoSQL store holding pre-aggregated CTR metrics with for campaign-level rollups.
Data Lake & Audit Store + FirehoseBuffers raw events into partitioned by /year=YYYY/month=MM/day=DD/hour=HH/ in columnar Snappy Parquet format.

4. API Interface Design & Wire Protocols

1. Ingest Ad Click Event

http
POST /v1/events/adclick HTTP/1.1
Host: click.ads.aws.internal
User-Agent: Mozilla/5.0 (iPhone; CPU iPhone OS 17_4 like Mac OS X)...
X-Forwarded-For: 203.0.113.195
Content-Type: application/json

{
  "click_id": "clk_88a91c74f0b21a",
  "ad_id": "ad_nike_airmax_2026",
  "campaign_id": "camp_nike_spring_global",
  "publisher_id": "pub_techcrunch_01",
  "user_id": "usr_998124a87",
  "cost_per_click_cents": 45,
  "impression_timestamp": 1718000000100,
  "click_timestamp": 1718000002340,
  "click_token_sig": "HMAC_SHA256(ad_id + timestamp + salt)"
}

Response: 202 Accepted

json
{
  "status": "ACCEPTED",
  "redirect_url": "https://www.nike.com/air-max-2026?utm_source=ad_campaign"
}

2. Query Real-Time Campaign Performance

http
GET /v1/analytics/campaigns/camp_nike_spring_global/metrics?window=1h&start_time=1718000000&end_time=1718003600 HTTP/1.1
Host: dashboard.ads.aws.internal
Authorization: Bearer <jwt_token>

Response: 200 OK

json
{
  "campaign_id": "camp_nike_spring_global",
  "window": "1_HOUR",
  "total_clicks": 142850,
  "total_conversions": 4285,
  "total_spend_cents": 6428250,
  "ctr_percentage": 2.85,
  "cvr_percentage": 3.00,
  "fraud_clicks_blocked": 1820
}

5. Data Models & Storage Architecture

Interactive Architecture Diagram
Synthesizing vector architecture diagram...

DynamoDB Schema (AdAggregatesTable)

() ()AttributesGSI1- / GSI1-
AD#<ad_id>WIN#1M#<epoch_minute>clicks: 840, spend_cents: 37800, fraud_count: 12, updated_atCAMP#<campaign_id> / DATE#<epoch_minute>
AD#<ad_id>WIN#1H#<epoch_hour>clicks: 50400, spend_cents: 2268000, fraud_count: 720CAMP#<campaign_id> / DATE#<epoch_hour>
AD#<ad_id>WIN#1D#<YYYY-MM-DD>clicks: 1209600, spend_cents: 54432000CAMP#<campaign_id> / DATE#<YYYY-MM-DD>

Part 2: Production Deep-Dive Locked1 Coin = 24 Hours

Unlock Complete Architecture & Production Runbooks

Your Balance:40 Coins

You have explored the free architectural preview (~41%). Spend 1 Coin to unlock the remaining 6 production deep-dive sections for a full 24 hours.

Sections Included in This 24-Hour Pass:
6. Core Algorithms & Deep-Dive Workflows
7. Architectural Trade-Off Matrix & Primitive Links
8. Critical Failure Modes, Resiliency & Edge Cases
9. Production Pitfalls & Anti-Patterns (The "Top 5 Gotchas")
10. Production Runbook & Observability Guide
11. System Design Interview Rubric & Deep-Dive Strategy
Keeps page unlocked for exactly 24 hoursSpend coins to fund LLM & compute infrastructure