Design a Metrics & Alerting System
Real-time observability at 500K–2M metrics/sec (Datadog / Prometheus at scale): batched Kafka ingestion, ClickHouse MergeTree rollups, Flink DDSketch p99 alerting, and state-machine deduplication.
The whiteboard
Toggle between ingest & store, real-time alerts, and dashboard query below — applying B-Trees vs LSM-Trees, Sharding & Hotkeys, and Queues vs Event Logs.
whiteboard · press 1–4 to trace flows
Full observability pipeline: batched Kafka ingestion fans out into ClickHouse for dashboards and Flink for sub-second p99 alerting.Isolating hot alerting from historical queries ensures a heavy ad-hoc dashboard never delays a production page.
Scale & APIs
500K / 2M
avg / burst metrics per sec
43–173 TB
raw/day before compression
5–10×
ClickHouse columnar compression
POST /ingest body: { "service": "checkout", "region": "us-east", "batch": [ ... ] } -> 202 Accepted
GET /query?service=checkout®ion=us-east&start=t1&end=t2&agg=p99 -> 200 OK { "resolution": "5m", "series": [ ... ] }How it works
1. Batched ingestion without Kafka hot partitions
- Agent-side batching: 10K hosts buffering and gzip-compressing points every 5s collapse
2M points/sinto~20Kcompact requests/sec, cutting TLS/HTTP overhead by 100x. - Composite Kafka key: Partitioning by
hash(service_id, region)(plus key salting for mega-services) prevents one noisy service from saturating a single broker.
2. Columnar storage & rollup query routing (ClickHouse)
- Bulk LSM-style writes:
Batch Writerflushes 10K-row chunks into ClickHouseMergeTreeordered by(service_id, metric_name, region, timestamp), achieving 5–10x compression and contiguous range scans. - Tiered rollups: Raw points expire after 14 days while materialized views persist
5m,1h, and1drollups.Query APIpicks the resolution from(t2 - t1)(<1hraw,1h–24h5m,1d–30d1h,>30d1d).
3. Accurate distributed p99 in Flink
- Mergeable sketches: Per-partition
p99values cannot be averaged; Flink emits mergeable DDSketch (or t-digest) states per partition and merges them in keyed windows inO(1)memory. - Event-time watermarks: A 5-second watermark grace window handles slightly delayed host batches without skewing alert windows.
4. Alert deduplication without silencing outages
- State machine:
Alert ManagertracksOK → FIRING → RESOLVEDperhash(rule_id, service_id, region)so on-call is paged immediately on transition and notified on recovery. - Non-refreshing cooldown:
SETNX alert:{id} 1 EX 600(never renewed while firing) caps re-notifications to every 10 minutes instead of silencing a multi-hour outage.
lesson
Interview differentiator: Fanning out from Kafka into two independent paths (Flink for sub-second alerting vs ClickHouse for historical dashboards) ensures an expensive 90-day dashboard query can never delay a critical production page.
Check yourself
interactive · stress-test this observability pipeline against classic interview probes
01 · Why refreshing the dedup TTL backfires
3-hour outage · TTL refreshed every 1m · on-call escalation
Your checkout p99 latency crosses 2s and stays above 2s for 3 hours. Alert Manager runs SETNX alert:{id} EX 10m on the first tick, and refreshes the 10-minute TTL on every subsequent firing tick. What happens?
Select an option to check the trade-off.
02 · Global p99 across 100 regions/partitions
50 partitions · global p99 alert · skewed traffic
Your gateway emits latency metrics across 50 Kafka partitions. A teammate proposes computing p99 per partition in Flink and averaging the 50 p99 numbers to trigger the global p99 alert. Why does this fail?
Select an option to check the trade-off.
03 · Why host agents batch before hitting /ingest
10k hosts · 2M metrics/s · HTTP overhead
You have 10,000 hosts emitting 200 metrics/sec each (2M points/sec total). Why shouldn't each host send 1 HTTP POST per metric point directly to the gateway?
Select an option to check the trade-off.
key saltingsharding
Appending a small random suffix (e.g. 0–9) to a hot partition key to spread writes across shards, at the cost of fan-out reads.