Design a Metrics Monitoring & Alerting System: Time-Series DBs, Gorilla & Pull vs Push
Why Relational Databases Collapse Under Infrastructure Metrics
Metrics Monitoring & Alerting Architecture
1. Data Model: Metric Name + Dimensional Labels + Timestamp
texthttp_requests_total{service="auth-api", env="prod", status="500"} 42 @1793450000
user_id or request_uuid into metric labels! Each unique label combination creates a separate time series index entry, causing OOM crashes in TSDBs.2. Collection Models: Pull (Prometheus) vs. Push (CloudWatch/Datadog)
| Criteria | Pull Model (Prometheus) | Push Model (Agents / StatsD) |
|---|---|---|
| Health Detection | Instant! Target down = failed scrape | Ambiguous (quiet or crashed?) |
| Service Discovery | Tight integration with K8s / Consul / etcd | Targets only need collector endpoint |
| Short-Lived Batch Jobs | Needs Pushgateway intermediary | Native push before job exits |
| Firewalls / Edge IoT | Hard to scrape behind NAT | Outbound HTTPS push works anywhere |
3. TSDB Storage Internals & Gorilla Compression
(timestamp, float64) sample!) using Facebook's Gorilla encoding:- Delta-of-Delta Timestamp Encoding: Since scrape intervals are almost constant (
10s, 10s, 10s),delta_of_deltais frequently0—encoded in a single0bit! - XOR Floating-Point Compression: Consecutive CPU/latency readings change slightly, so XORing adjacent IEEE-754 floats leaves dozens of trailing/leading zeros compressed tightly.
- Downsampling & Retention Tiering: Keep raw 10s precision for 7 days, downsample to 1-minute averages for 30 days, and 1-hour rollups for 1 year!
4. Decoupled Query Caching & Alerting Engine
- Metrics Collector → Kafka Buffer: Decouples spike traffic from TSDB writes so no operational telemetry drops during partial outages.
- Query Service + Cache: Pre-aggregate dashboard queries (
rate(http_requests[5m])) in Redis so 50 engineers opening the Grafana war-room don't re-scan the TSDB simultaneously. - Alert Manager: Evaluates YAML rules (
latency_p99 > 500ms for 5m) periodically against the TSDB, deduplicates & groups flapping alerts, and routes incidents cleanly to PagerDuty, Slack, or Opsgenie.