Case Study 20 โ€” Analytics / Metrics Event Pipeline

Design a pipeline that collects billions of product analytics events (page views, clicks, purchases), processes them reliably, and serves dashboards and ad-hoc queries.

1. Problem

Product teams need answers like "how many signups per country yesterday?" and "funnel from landing โ†’ signup โ†’ purchase." Events fire constantly from web and mobile apps. You must ingest high-volume fire-and-forget traffic, never lose critical data, and query aggregates efficiently.

2. Requirements

Functional (MVP)

  • Client SDK sends events: { eventName, userId, timestamp, properties }
  • Ingestion API accepts batch events (JSON)
  • Stream processing: dedupe, validate schema, enrich (geo from IP)
  • Store raw events for replay (short retention)
  • Aggregate metrics: counts per event per hour, DAU, simple funnels
  • Query API or SQL interface for dashboards (last 7 days)

Out of scope (initially)

  • Sub-second real-time alerting (near-real-time OK)
  • ML feature store, session replay, heatmaps
  • Petabyte-scale multi-year retention on hot tier
  • Exactly-once billing-grade accounting (at-least-once + idempotent agg is MVP)

Non-functional

  • Ingestion: 100k+ events/sec peak, p99 ack < 100 ms
  • Durability: survive broker/node failure (replicated log)
  • Processing lag: minutes acceptable for dashboards
  • Query: interactive for pre-aggregated rollups (< 5 s)
  • Cost-aware tiering: hot vs cold storage

3. Back-of-the-envelope estimates

These numbers are rough order-of-magnitude math โ€” not a capacity plan. In interviews, the goal is to show you know what to count and which resource breaks first. A useful shortcut: 1 QPS sustained โ‰ˆ 86,400 events/day. Analytics pipelines ingest fire-and-forget traffic โ€” peak 2โ€“5ร— average during product launches or viral moments.

Why we estimate

Event pipelines decouple fast ingestion from slow analytics. Estimates tell us:

  • Why you cannot write 1B events/day directly to a dashboard database
  • Kafka (or similar) absorbs spikes while processors lag safely
  • Raw storage (TB/day) vs rollup storage (MB/day) drives tiering strategy
  • At-least-once + eventId dedup beats chasing exactly-once everywhere

Assumptions

AssumptionValueWhy it matters
Daily active users (DAU)50MEvent volume base
Events per user per day20Page views, clicks, purchases
Average event JSON size500 BKafka + lake sizing
Peak multiplier3ร— averageLaunch / viral traffic
Compression ratio (Parquet)~5ร—Cold storage
Rollup granularityHourlyDashboard query speed
Raw retention (hot)7โ€“90 daysReplay vs cost

Step A โ€” Traffic (QPS) with labeled arithmetic

Daily event volume:

DAU                   = 50,000,000
Events per user/day   = 20

Daily events          = 50M ร— 20 = 1,000,000,000 events/day (1 billion)

Average ingestion QPS:

Average ingest QPS    = 1,000,000,000 รท 86,400
                      โ‰ˆ 11,574 events/second
                      โ‰ˆ 11,600/s (round)

Peak ingestion QPS (3ร— average):

Peak ingest QPS       = 11,600 ร— 3 โ‰ˆ 34,800/s
                      โ‰ˆ 35,000/s (round)

Ingestion API batches (if clients send 10 events per POST):

Peak HTTP requests    = 35,000 รท 10 โ‰ˆ 3,500 POST/s to ingestion API
(still must ack in < 100 ms p99 โ€” produce to Kafka, don't process inline)

Stream processor throughput:

Must sustain โ‰ฅ peak ingest on consumer side; lag of minutes is OK for dashboards but not hours.

Step B โ€” Storage

Raw events per day (uncompressed):

Events/day          = 1 billion
Event size          = 500 B

Daily raw volume    = 1B ร— 500 B = 500 GB/day
Monthly raw         = 500 GB ร— 30 โ‰ˆ 15 TB/month (uncompressed)

After Parquet compression (~5ร—):

Monthly in object lake โ‰ˆ 15 TB รท 5 โ‰ˆ 3 TB/month in S3
Annual (if kept)       โ‰ˆ 36 TB compressed (tier older partitions to Glacier)

Pre-aggregated rollups (OLAP):

Assume 1M unique (metric, dimension, hour) rows/day
Row size            โ‰ˆ 200 B

Rollup storage/day  = 1M ร— 200 B = 200 MB/day
Yearly rollups      โ‰ˆ 73 GB โ€” fits easily in ClickHouse/BigQuery hot tier

Kafka retention (7 days buffer):

7 ร— 500 GB = 3.5 TB in Kafka (plan partition count + retention accordingly)

Step C โ€” Bandwidth

Ingestion ingress at peak:

Peak events         = 35,000/s
Event size          = 500 B

Ingress bandwidth   = 35,000 ร— 500 B = 17.5 MB/s
                    (modest โ€” network rarely the bottleneck; disk write speed to lake matters)

Kafka โ†’ S3 lake write (batch flush):

Processors batch events into ~128 MB Parquet files
Write throughput must keep up with 500 GB/day โ‰ˆ 5.8 MB/s average sustained

Dashboard query egress:

Pre-aggregated queries return KBโ€“MB JSON โ€” negligible vs ingest

Step D โ€” Read:write ratio table

OperationTypeRateNotes
Client โ†’ Ingestion APIWrite~11.6k/s avg; ~35k/s peakFast 202 ack
Kafka produceWrite~35k/s peakDurable log
Stream processor consumeRead~35k/s peakEnrich + fork
Parquet write to S3Write~5.8 MB/s avgBatch sink
OLAP rollup insertWrite~1M rows/dayHourly windows
Dashboard queryReadLow QPSScans rollups, not raw
Backfill replayReadBatchRe-reads S3 Parquet

Ratio on hot path: write-heavy (ingest >> query). Analytics reads hit pre-aggregated tables, not 1B raw rows.

What the numbers tell us

  • Ingestion API is stateless โ€” validate, assign eventId, produce to Kafka, return 202 in < 100 ms
  • Kafka decouples 35k/s spikes from slow Flink/Spark processors
  • 500 GB/day raw โ†’ S3 data lake (Parquet), not OLTP database
  • Dashboards query rollups (200 MB/day growth) โ€” never full-scan 1B daily rows
  • At-least-once delivery + eventId dedup in processor handles retries without double-counting
  • Partition by hash(userId) for user-scoped ordering; salt hot keys to avoid celebrity skew
  • 7-day Kafka + 90-day hot lake + Glacier = sensible cost tiering
  • Processing lag of minutes is acceptable for MVP dashboards

Common mistake for this problem

Writing events directly into Postgres or serving dashboards with SELECT COUNT(*) FROM events on the raw table. At 1B rows/day, that collapses in days. Correct pattern: ingest โ†’ queue โ†’ process โ†’ lake + OLAP rollups. Another mistake: insisting on exactly-once end-to-end โ€” at-least-once + idempotent eventId is simpler and sufficient for product analytics.

4. High-Level Design (HLD)

End-to-end path in words:

Ingestion โ†’ Queue/Stream โ†’ Processing โ†’ Warehouse/OLAP (+ optional real-time layer)

Components

ComponentRole
Ingestion APIStateless, validates, assigns eventId, produces to Kafka
Kafka (or Pulsar)Durable, partitioned log; buffers spikes
Schema RegistryAvro/JSON schema versions; reject bad events
Stream processorsFlink / Spark Streaming / Kafka Streams โ€” enrich, aggregate
Raw event lakeS3 + Parquet partitioned by date/hour
OLAP warehouseClickHouse, BigQuery, or Redshift for analytics SQL
Real-time countersRedis / Druid for last-hour metrics (optional)
Query APIServes dashboard tiles from OLAP + Redis
OrchestratorAirflow / Dagster for batch backfill jobs

Flows

Event ingestion

  1. Client batches 10 events, POST /v1/events with API key
  2. Ingestion validates schema, adds receivedAt, eventId (UUID)
  3. Partition key = hash(userId) for user-scoped ordering
  4. Produce to topic events.raw, return 202 Accepted quickly

Stream processing

  1. Consumer reads events.raw
  2. Filter bots, drop invalid fields
  3. Enrich: IP โ†’ country, userId โ†’ account tier from cache
  4. Fork: (a) write Parquet to S3, (b) increment hourly rollups in OLAP

Dashboard query

  1. UI requests "signups by country, last 7 days"
  2. Query API hits pre-aggregated table metrics.signups_hourly in OLAP
  3. Return JSON series โ€” no full scan of 1B raw rows

Backfill / replay

  1. New metric defined โ†’ batch job scans S3 Parquet for last 90 days
  2. Populate new rollup table without re-instrumenting clients

Trade-offs

ChoiceProsCons
Kafka vs direct to DBAbsorbs spikes, replayOps complexity
At-least-once vs exactly-onceSimpler, fasterDuplicate events โ€” need idempotent agg
Lambda (stream) + batchReal-time + cheap historicalTwo code paths unless unified (Flink)
ClickHouse vs BigQueryFast, self-host optionManaged BQ less ops
Pre-aggregate vs scan rawFast dashboardsMust know metrics upfront
7-day hot + cold archiveCostOld ad-hoc queries slower

5. Low-Level Design (LLD)

APIs

POST /v1/events
Headers: X-Api-Key, Content-Type: application/json
Body: {
  "events": [
    {
      "eventName": "page_view",
      "userId": "u_123",
      "timestamp": "2026-07-20T10:00:00Z",
      "properties": { "path": "/pricing", "referrer": "google" }
    }
  ]
}
โ†’ 202 { "accepted": 10, "batchId": "b_991" }

GET /v1/metrics/signups?from=2026-07-13&to=2026-07-20&groupBy=country
โ†’ { "series": [{ "country": "IN", "count": 12000 }, ...] }

GET /v1/metrics/funnel?steps=landing,signup,purchase&from=...&to=...
โ†’ { "steps": [{ "name": "landing", "users": 100000 }, ...] }

Event schema (registered)

Event {
  eventId: string (UUID, server-generated if missing)
  eventName: string (enum or pattern)
  userId: string | null
  anonymousId: string | null
  timestamp: ISO8601 (client clock; server adds receivedAt)
  properties: map<string, string | number | boolean>
  context: {
    ip: string (hashed after geo lookup)
    userAgent: string
    appVersion: string
  }
  receivedAt: ISO8601 (server)
  schemaVersion: int
}

Kafka topics

events.raw          โ€” partitions: 64, key: userId, retention: 7 days
events.enriched     โ€” optional intermediate
metrics.rollup      โ€” compacted or changelog for materialized agg

OLAP tables (ClickHouse-style)

-- Raw (optional in OLAP for recent window)
events_raw (
  event_date Date,
  event_time DateTime,
  event_name LowCardinality(String),
  user_id String,
  country FixedString(2),
  properties JSON
)
ENGINE = MergeTree()
PARTITION BY toYYYYMM(event_date)
ORDER BY (event_name, event_date, user_id)

-- Pre-aggregated hourly
signups_hourly (
  hour DateTime,
  country FixedString(2),
  signup_count AggregateFunction(count)
)
ENGINE = AggregatingMergeTree()
ORDER BY (hour, country)

S3 lake layout

s3://analytics-lake/events/
  year=2026/month=07/day=20/hour=10/
    part-0000.parquet
    part-0001.parquet

Modules

IngestionController / EventValidator / KafkaProducer
SchemaRegistryClient
EnrichmentProcessor (GeoIP, user cache)
AggregationJob (hourly rollups)
LakeWriter (Parquet batch sink)
MetricsQueryService / FunnelCalculator
BackfillOrchestrator

Algorithm โ€” ingestion (fast ack)

function ingestBatch(apiKey, events):
  tenant = auth(apiKey)
  accepted = []
  for e in events:
    if not schemaValid(e): continue
    e.eventId = e.eventId or uuid()
    e.receivedAt = now()
    e.schemaVersion = CURRENT_SCHEMA
    kafka.produce(
      topic = "events.raw",
      key = e.userId or e.anonymousId,
      value = serialize(e)
    )
    accepted.append(e.eventId)
  return 202, { accepted: len(accepted) }

Algorithm โ€” hourly rollup (stream processor)

function processEvent(e):
  country = geoIp.lookup(e.context.ip)
  e2 = enrich(e, country)

  lakeBuffer.add(e2)
  if e2.eventName == "signup":
    key = (hourBucket(e2.timestamp), country)
    state.increment(key, uniqueUser=e2.userId)   // HyperLogLog or set per hour shard

function onWindowClose(hour):
  for key, count in state.flush(hour):
    olap.insert("signups_hourly", key, count)
  lakeBuffer.flushToS3(hour)   // Parquet files

Algorithm โ€” funnel (batch or OLAP query)

-- Simplified SQL idea: distinct users per step in order
WITH step1 AS (
  SELECT DISTINCT user_id FROM events_raw
  WHERE event_name = 'landing' AND event_date BETWEEN ...
),
step2 AS (
  SELECT DISTINCT e.user_id FROM events_raw e
  INNER JOIN step1 s ON e.user_id = s.user_id
  WHERE e.event_name = 'signup' AND e.timestamp >= ...
)
SELECT count(*) FROM step2;

Algorithm โ€” dedupe (at-least-once)

function aggregateWithDedup(event):
  if dedupStore.seen(event.eventId): return   // Redis SET with 48h TTL
  dedupStore.mark(event.eventId)
  applyAggregation(event)

Concurrency notes

  • Ingestion servers are stateless โ€” scale horizontally
  • Kafka partition count bounds parallel consumers per topic
  • Idempotent producers + eventId dedup prevents double-count from retries
  • Client clocks skew: trust receivedAt for ordering; store client timestamp separately
  • Hot partition risk: celebrity user / load test โ€” salt key or use round-robin for anonymous traffic

6. Scale evolution

StageVolumeChanges
MVP1k events/sPOST โ†’ single Kafka โ†’ Flink โ†’ Postgres daily rollup
Growth10k events/sMore partitions, ClickHouse, S3 lake, schema registry
Peak100k+ events/sDedicated ingest cluster, async produce acks, rate limit per API key
QuerySlow ad-hocPre-aggregate top 50 metrics; raw lake for analyst SQL (Trino/Athena)
RetentionYears of dataTier S3 โ†’ Glacier; rollup tables kept hot
Multi-tenantSaaS analyticsNamespace topics/tables by tenantId; fair scheduling on shared Flink

7. Recap

  • Pipeline shape: ingestion โ†’ queue/stream โ†’ processing โ†’ warehouse/OLAP
  • Kafka (or similar) decouples producers from processors and enables replay
  • Use at-least-once + idempotent eventId rather than chasing exactly-once everywhere
  • Pre-aggregate for dashboards; data lake for exploration and backfill
  • Separate real-time counters (optional) from authoritative batch rollups

Practice: draw the pipeline from memory, then explain what breaks if the stream processor restarts mid-batch and how eventId dedup helps.