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 +
eventIddedup beats chasing exactly-once everywhere
Assumptions
| Assumption | Value | Why it matters |
|---|---|---|
| Daily active users (DAU) | 50M | Event volume base |
| Events per user per day | 20 | Page views, clicks, purchases |
| Average event JSON size | 500 B | Kafka + lake sizing |
| Peak multiplier | 3ร average | Launch / viral traffic |
| Compression ratio (Parquet) | ~5ร | Cold storage |
| Rollup granularity | Hourly | Dashboard query speed |
| Raw retention (hot) | 7โ90 days | Replay 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
| Operation | Type | Rate | Notes |
|---|---|---|---|
| Client โ Ingestion API | Write | ~11.6k/s avg; ~35k/s peak | Fast 202 ack |
| Kafka produce | Write | ~35k/s peak | Durable log |
| Stream processor consume | Read | ~35k/s peak | Enrich + fork |
| Parquet write to S3 | Write | ~5.8 MB/s avg | Batch sink |
| OLAP rollup insert | Write | ~1M rows/day | Hourly windows |
| Dashboard query | Read | Low QPS | Scans rollups, not raw |
| Backfill replay | Read | Batch | Re-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 +
eventIddedup 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
| Component | Role |
|---|---|
| Ingestion API | Stateless, validates, assigns eventId, produces to Kafka |
| Kafka (or Pulsar) | Durable, partitioned log; buffers spikes |
| Schema Registry | Avro/JSON schema versions; reject bad events |
| Stream processors | Flink / Spark Streaming / Kafka Streams โ enrich, aggregate |
| Raw event lake | S3 + Parquet partitioned by date/hour |
| OLAP warehouse | ClickHouse, BigQuery, or Redshift for analytics SQL |
| Real-time counters | Redis / Druid for last-hour metrics (optional) |
| Query API | Serves dashboard tiles from OLAP + Redis |
| Orchestrator | Airflow / Dagster for batch backfill jobs |
Flows
Event ingestion
- Client batches 10 events,
POST /v1/eventswith API key - Ingestion validates schema, adds
receivedAt,eventId(UUID) - Partition key =
hash(userId)for user-scoped ordering - Produce to topic
events.raw, return 202 Accepted quickly
Stream processing
- Consumer reads
events.raw - Filter bots, drop invalid fields
- Enrich: IP โ country,
userIdโ account tier from cache - Fork: (a) write Parquet to S3, (b) increment hourly rollups in OLAP
Dashboard query
- UI requests "signups by country, last 7 days"
- Query API hits pre-aggregated table
metrics.signups_hourlyin OLAP - Return JSON series โ no full scan of 1B raw rows
Backfill / replay
- New metric defined โ batch job scans S3 Parquet for last 90 days
- Populate new rollup table without re-instrumenting clients
Trade-offs
| Choice | Pros | Cons |
|---|---|---|
| Kafka vs direct to DB | Absorbs spikes, replay | Ops complexity |
| At-least-once vs exactly-once | Simpler, faster | Duplicate events โ need idempotent agg |
| Lambda (stream) + batch | Real-time + cheap historical | Two code paths unless unified (Flink) |
| ClickHouse vs BigQuery | Fast, self-host option | Managed BQ less ops |
| Pre-aggregate vs scan raw | Fast dashboards | Must know metrics upfront |
| 7-day hot + cold archive | Cost | Old 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 +
eventIddedup prevents double-count from retries - Client clocks skew: trust
receivedAtfor ordering; store clienttimestampseparately - Hot partition risk: celebrity user / load test โ salt key or use round-robin for anonymous traffic
6. Scale evolution
| Stage | Volume | Changes |
|---|---|---|
| MVP | 1k events/s | POST โ single Kafka โ Flink โ Postgres daily rollup |
| Growth | 10k events/s | More partitions, ClickHouse, S3 lake, schema registry |
| Peak | 100k+ events/s | Dedicated ingest cluster, async produce acks, rate limit per API key |
| Query | Slow ad-hoc | Pre-aggregate top 50 metrics; raw lake for analyst SQL (Trino/Athena) |
| Retention | Years of data | Tier S3 โ Glacier; rollup tables kept hot |
| Multi-tenant | SaaS analytics | Namespace 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
eventIdrather 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.