skip to content
jaeaeichsysdes

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

INGESTION (500K–2M PTS/S)HISTORICAL STORAGE & QUERYREAL-TIME ALERTING (<1s)gzip batchfan-outstreamreadfireHost Agentsbatch + gzip 5s10K hosts → 20K RPSIngest GatewayPOST /ingest202 AcceptedKafka Loghash(svc, region)fan-out bufferBatch Writer10K-row chunksamortizes diskClickHouseMergeTree + MVs5m / 1h / 1dQuery APIrollup routerGET /queryFlink StreamDDSketch p995s watermarksAlert ManagerOK→FIRING→RESstate machineRedis Dedupfixed 10m TTLPagerDutySlack / FCMon-call page

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&region=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/s into ~20K compact 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 Writer flushes 10K-row chunks into ClickHouse MergeTree ordered 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, and 1d rollups. Query API picks the resolution from (t2 - t1) (<1h raw, 1h–24h 5m, 1d–30d 1h, >30d 1d).
  • Mergeable sketches: Per-partition p99 values cannot be averaged; Flink emits mergeable DDSketch (or t-digest) states per partition and merges them in keyed windows in O(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 Manager tracks OK → FIRING → RESOLVED per hash(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?

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?

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?

esc
⌘B sidebar[ ] prev/next1–5 flowsc copyt theme