Design a time-series metrics platform

    A metrics & monitoring platform (Prometheus) scrapes time-series data from services, stores it efficiently, and powers dashboards + alerting. The challenges are high-cardinality storage, efficient compression, and a reliable alerting pipeline.

    Streaming & Pipelines, 10 components, about 40 min

    1. Scope

    In scope

    • • Collect metrics from services
    • • Store time series efficiently
    • • Query for dashboards
    • • Evaluate alert rules

    Out of scope

    • • Distributed tracing
    • • Log aggregation
    • • APM internals

    2. Functional requirements

    Must have

    • • Ingest metrics from many targets
    • • Store and query time series
    • • Alert on rule conditions
    • • Discover scrape targets dynamically

    Nice to have

    • • Long-term downsampled storage
    • • Multi-tenancy
    • • Recording rules

    3. Non-functional requirements

    Ingest~333K samples/sec— 10K targets × 500 series / 15 s
    Query latencySub-second for dashboards
    RetentionWeeks hot, months/years cold
    CardinalityMillions of active series

    4. Back-of-envelope estimation

    Assumptions

    • • 10K targets, scrape every 15 s
    • • Each exposes ~500 series
    • • Sample ~16 bytes raw, ~1.4 bytes compressed
    • • 15-day hot retention

    Derived numbers

    Active series~5M— 10K × 500
    Samples/sec~330K/s— 5M / 15s
    Hot storage~600 GB— ~40 GB/day compressed (Gorilla) × 15 days

    5. API design

    GET/metrics

    Target exposes metrics (pull model)

    GET/api/query?expr=

    PromQL query for dashboards

    INTERNALrules.eval()

    Periodic alert rule evaluation

    6. High-level design

    What each box in the reference diagram is responsible for:

    Scraper — Pulls /metrics from each target every 15 s.
    Service Discovery — Consul/k8s feeds the live target list.
    TSDB — Time-series store with Gorilla/delta compression.
    Alert Manager — Evaluates rules; dedups, groups, routes pages.
    Grafana — Dashboards query the TSDB.
    Long-Term S3 — Downsampled rollups (Thanos/Cortex).

    7. Data model

    TSDB: series = {metric, labels} → [(ts, value) ...]— Compressed blocks

    8. Deep dives

    Pull vs push

    Prometheus pulls: the scraper fetches /metrics from each target on a schedule. Pull makes target health observable (a failed scrape is a signal), centralizes scrape config, and avoids targets needing to know the backend. Push (via a gateway) is used for short-lived batch jobs that vanish before a scrape.

    Time-series compression

    Storing raw (timestamp, float) pairs is wasteful. Gorilla-style encoding delta-encodes timestamps (usually a fixed interval) and XOR-encodes consecutive float values (often similar), compressing ~16 bytes down to ~1.4 bytes/sample. This is what makes millions of series affordable.

    High cardinality

    Each unique label combination is a separate series. A label like user_id can explode cardinality and blow up memory. Mitigate by limiting label dimensions, dropping high-cardinality labels at scrape time, and sharding the TSDB.

    Alerting pipeline

    A rules engine evaluates expressions over the TSDB periodically. Firing alerts go to an alert manager that dedups (same alert from many instances), groups (one page for a cluster-wide issue), silences, and routes to PagerDuty — preventing alert storms.

    9. Bottlenecks

    • Cardinality explosion → label hygiene, sharding
    • Single TSDB capacity → federation/Thanos
    • Long-term cost → downsample to object storage

    10. Trade-offs

    Pull or push collection

    Choose: Pull (scrape) for long-lived services; push gateway or agents for short-lived jobs.

    Instead of: Push everything: works through firewalls, but the server cannot tell "down" from "quiet".

    Pull makes target health part of collection; push covers what pull cannot reach.

    Storage format

    Choose: Time-series blocks with delta-of-delta timestamps and XOR-compressed values (Gorilla): ~1.4 bytes per sample.

    Instead of: Generic database rows: tens of bytes per sample.

    Samples arrive at hundreds of thousands per second; compression decides the hardware bill.

    Retention

    Choose: Raw data for ~15 days hot, then downsampled (5 min, 1 h) for long-term in object storage.

    Instead of: Keep raw forever: storage grows without bound and long-range queries crawl.

    Old data is queried coarsely; downsampling matches resolution to use.

    11. Failure modes

    Cardinality explosion (user id as a label)

    Millions of new series; memory exhausted.

    Mitigation: Per-tenant series limits, label allowlists, and rejecting unbounded labels at ingest.

    Monitoring down during an incident

    Blind exactly when you need it.

    Mitigation: Run monitoring separately from what it monitors, plus a minimal meta-monitor elsewhere.

    Expensive dashboard query

    Query nodes overloaded.

    Mitigation: Recording rules precompute common aggregations; query limits on series and time range.

    12. What interviewers probe

    Answer each one yourself before opening it.

    Estimate ingest for 10K targets with 500 series each at 15 s.Show a strong answer

    5M series ÷ 15 s ≈ 333K samples/s; at ~1.4 B/sample ≈ 470 KB/s, ~40 GB/day before replication.

    How do you compute p99 latency across 100 servers?Show a strong answer

    Not by averaging p99s. Export histograms (bucket counts), sum buckets across servers, then compute the quantile.

    How does alerting avoid flapping?Show a strong answer

    Require the condition for a duration ("for: 5m"), use hysteresis, and alert on SLO burn rates rather than raw thresholds.

    How do you scale beyond one Prometheus?Show a strong answer

    Shard scrape targets, remote-write to a horizontally scalable store (Thanos, Mimir), and query through a federating layer.

    Sign in to track this guide in your curriculum progress.