Worked design: clickstream to warehouse
Try the question yourself first, then compare with the walkthrough below.
Design a clickstream pipeline from apps to the warehouse
Web and mobile apps emit about 50,000 events per second at peak (2–3 billion a day). Product analysts want dashboards that are at most 15 minutes behind, and data scientists want full history for modeling. Events can arrive days late from offline phones.
Design the pipeline from the SDK to analyst-ready tables. Cover ingestion, storage, transformation, data quality, backfills and cost.
Hint 1
Separate the "land it safely" path from the "make it useful" path.
Hint 2
Which timestamp do you partition by, and what does that do to late events?
Show solution
SDK → collector API (validates against a schema registry, adds received_at) → Kafka (partitioned by user/device for per-user order) → a streaming sink writes raw Parquet/Iceberg partitioned by received hour → micro-batch transforms (every 5–15 minutes) dedup on event_id, type the common fields and produce fct_events partitioned by event date → sessions and daily aggregates downstream.
The main points: keep an immutable raw layer so anything can be replayed; make every step idempotent (dedup keys, overwrite-by-partition); reprocess a sliding window of the last N event dates for late data; enforce contracts at the collector and quarantine bad events rather than drop them; control cost with compaction, partition pruning, tiered retention, and by not streaming what only needs batch.
The full walkthrough is in the worked design.
What a strong answer covers
- Clarifies requirements: volume, freshness SLA, late data window, consumers (weight 2)
- Durable buffer (Kafka/Kinesis) and an immutable raw layer for replay (weight 3)
- Idempotent processing: dedup on event_id, partition overwrite (weight 2)
- Event time vs received time, and a reprocessing window for late events (weight 3)
- Schema contracts and a quarantine path for invalid events (weight 2)
- Cost controls: compaction, partition pruning, retention tiers (weight 1)
- Says what changes at 10x volume (weight 1)
Follow-ups
- The 15-minute SLA becomes 10 seconds for one fraud dashboard. What do you add, and what do you not change?
Add a streaming path for that one dashboard: a stream processor reading the same Kafka topic and writing to a low-latency store, such as a real-time OLAP database. Keep the existing batch path, its SLA and the warehouse as the source of truth.
- A producer bug sent 6 hours of events with the wrong user_id. Walk through the fix.
Find the affected window and events (by producer version and time). Fix the producer, then correct the data: if the true user_id can be derived (from the session or device), rewrite the affected partitions. Otherwise quarantine or flag them. Reprocess downstream aggregates for those hours, and tell the consumers what changed.
Further reading
1. Requirements
Clarify and write down:
- Volume: 50k events/s peak, about 2.5B/day, around 1 KB each, so about 2.5 TB/day raw and roughly 300 GB/day as Parquet.
- Freshness: 15 minutes for dashboards. Data science is fine with daily.
- Late data: most within minutes; offline phones up to 3 days. Anything later is rare enough for a monthly fix-up.
- Correctness: analytics-grade. Small duplicate rates are tolerable but should be removed; not money.
- Retention: raw for 13 months hot, then archive; modeled tables indefinitely.
2. Data contracts
A tracking plan (event name, owner, required properties and types) lives in a schema registry. The collector validates every event against it. The contract also promises: a globally unique event_id generated on the device, an event_ts set by the client, and no PII in free-text properties.
3. Ingestion
SDK → collector API → Kafka (raw-events, partitioned by device_id) → sink → object storage (raw)
↘ invalid events → Kafka (quarantine) → quarantine table
- The collector is a thin, stateless HTTP service: it authenticates, validates, stamps
received_at, and writes to Kafka. If it's down, SDKs buffer and retry. - Kafka decouples producers from consumers and gives replay. Partitioning by
device_idkeeps one device's events in order. - A sink (Kafka Connect or a small Spark/Flink job) writes Parquet to object storage every few minutes.
4. Storage and format
- Raw: an Iceberg (or Delta) table, append-only, partitioned by
received_hour. The payload is kept as-is. This is the replay source for everything. - Modeled: Iceberg or warehouse tables partitioned by
event_date(fromevent_ts) and clustered byevent_name.
Partitioning raw by received time makes loads idempotent: each run processes a closed set of hours. Partitioning modeled by event time makes queries match how analysts think.
5. Transform
Every 15 minutes, an incremental job:
- Reads newly landed raw partitions.
- Dedups on
event_idagainst the affected event dates (SDK retries create duplicates). - Types the common columns, normalizes time zones, stitches
anonymous_idtouser_id. - Overwrites the affected
event_datepartitions offct_events.
Downstream: fct_sessions, agg_user_daily and per-event tables (fct_purchases) with exploded properties. See the modeling question below.
6. Serving
Dashboards read the daily and session aggregates, not raw events. Data scientists read fct_events directly. A "data complete through" timestamp is published per table, so dashboards can show how fresh they are.
7. Quality
- At the collector: schema validation, with invalid events quarantined, not dropped. Alert when the quarantine rate jumps.
- On the modeled layer: uniqueness of
event_id, not-null keys, volume per event type versus the same hour last week (catches a broken release), and the share of late events. - Reconciliation: events in Kafka versus rows landed in raw per hour.
8. Orchestration and backfill
- The incremental job always reprocesses the last 3 event dates, which absorbs late events automatically.
- Backfills replay raw partitions for a date range through the same code, at limited concurrency.
- Because every write is a partition overwrite keyed on dedup'd
event_id, re-running is always safe.
9. Failure modes
| Failure | Detection | Recovery |
|---|---|---|
| Bad SDK release sends wrong property types | Contract violations spike | Quarantine, fix SDK, replay quarantined events |
| Sink falls behind | Kafka consumer lag alert | Scale consumers; Kafka retention gives a buffer (set it to at least 3 days) |
| Duplicate events | Uniqueness test | Dedup on event_id is already part of the transform |
| Late event after the 3-day window | Late-share metric | Monthly correction job over older partitions |
10. Cost
The biggest costs are usually storage of raw events, re-processing and small files. The dials: compress and compact raw hourly, keep raw hot for 13 months then archive, prune by partition in every job, and don't run streaming where a 15-minute micro-batch meets the SLA.
11. What changes at 10×
At 500k events/s: more Kafka partitions and brokers, a horizontally scaled collector, sinks that write larger files less often, and probably sampling or aggregating very chatty low-value events (such as scroll events) at the collector. The layered design and the late-data policy don't change.