Metrics pipeline
Application and device telemetry arrives continuously. Engineers query it on dashboards over windows from the last five minutes to the last two years. Ingest must never block the systems producing the events.
Envelope2M events/s ingest · 2 years retained · dashboard queries in seconds
What the design must do
- Producers never wait on the pipeline — ingest is decoupled from processing
- Rolling aggregates available within seconds of the event
- Two years of raw history queryable at analytical cost, not transactional cost
- A dashboard read never scans raw events
The worked envelope
Assumed. 2M events/s ingest at 100 bytes each, roughly 500k distinct series, two years of retention, dashboards querying arbitrary ranges.
| Raw ingest | 2M/s × 100 B = 200 MB/s = 17 TB/day |
| Two years raw | 17 TB × 730 = 12.6 PB — not viable |
| Raw, 7 days | 17 TB × 7 = 120 TB |
| 1-min, 30 days | 500k series × 43,200 × 16 B = 345 GB |
| 1-hour, 2 years | 500k × 17,520 × 16 B = 140 GB |
| Ladder total | 12.6 PB → ~120 TB |
What the numbers decide. One line does all the work: two years of raw data is 12.6 petabytes, and the same two years as a resolution ladder is about 120 terabytes — a hundred-fold reduction, almost all of it from data nobody queries at full resolution anyway.
That calculation is the architecture. It says retention is not a policy you apply later but the thing the storage layer is built around, it says partitioning must be by time so expiry is a partition drop, and it says the rollup job is a first-class production system rather than a cleanup script. And it raises the question that kills these systems in practice: the 500k series count is an assumption, and one careless label turns it into 500 million.
The decisions, and what each one costs
A resolution ladder: raw for days, minute for weeks, hour for years
- Because
- 12.6 PB of raw is unaffordable and unqueryable, while the questions asked of old data are all coarse — nobody debugs last March at one-second resolution
- The cost
- Old data cannot answer fine-grained questions, and the boundaries are irreversible once raw is dropped
- Rejected
- Uniform retention, which forces the choice between keeping everything and keeping nothing
Store sums, counts and histograms — never averages or percentiles
- Because
- Averages do not compose and percentiles genuinely cannot be recovered from stored percentiles, so a rollup of p99s is a number with no meaning
- The cost
- Histograms cost more per series than a single value, and bucket boundaries must be chosen up front
- Rejected
- Storing the computed p99 per minute, which produces confident, wrong charts that nobody detects
Partition by time, expire by dropping partitions
- Because
- Deleting a day of rows from a 120 TB table is the most expensive statement in the system; dropping a partition is instant and leaves no bloat
- The cost
- Late-arriving data may target a partition that is compressed or already gone
- Rejected
- A retention DELETE job, which competes with ingest and never quite keeps up
Labels are for dimensions you group by, never for identifiers
- Because
- Series count multiplies out across every label, so one user_id label turns 500k series into millions and the ingest tier falls over
- The cost
- Debugging a single user’s traffic needs logs or traces instead, which are indexed differently
- Rejected
- Allowing free-form labels, which is one innocuous line of instrumentation away from an outage
Components in the reference design
- Client
- Browser or mobile app
- API Gateway
- Auth, routing, quota enforcement
- Event Log
- Kafka-style, ordered, replayable
- Stream Proc
- Windowed aggregation over an event log
- SQL DB
- Relational, ACID, joins
- Object Store
- S3-style blobs, media, backups
- App Service
- Stateless business logic
What not to draw
- Search Index — A search index finds documents by content. Metrics queries are aggregations over time ranges, which a columnar store answers orders of magnitude more cheaply.
The reference design
The log is the seam. Everything upstream of it is ingest and must be fast and dumb; everything downstream is processing and can fail without the producers noticing. A gateway that writes to an ordered, retained log and acknowledges is the whole ingest tier — and because the log is replayable, a broken consumer is recoverable rather than a data loss.
Two consumers of one stream, and this is the tier's point. A stream processor computes rolling windows and writes small aggregates that dashboards read; separately, raw events land in columnar files in object storage. Fresh answers come from the aggregate table, historical ones from a scan over the columnar data. Lesson 7.03's batch-versus-stream question, answered with both — which is what real pipelines do.
Why the raw store is columnar. A dashboard reads three columns out of forty across billions of rows. Column layout means you read only those three, and values within a column compress five to twenty times because they share a type and a narrow range. Partition by date, because every query has a time predicate — Lesson 7.02's pruning, and the reason a two-year query returns in seconds rather than minutes.
Event time, not arrival time. A device offline for three hours delivers its 6am events at 9am. Bucketing by arrival puts them in the wrong window and the chart looks perfectly smooth while being wrong. Window on event time, set a watermark, and decide explicitly what happens to data arriving after it — Lesson 7.03 in the one place it always bites.
Pre-aggregate deliberately. Dashboards ask a small number of shapes over and over. Materialise those roll-ups by minute, hour and day; let ad-hoc exploration pay the scan. A dashboard that recomputes from raw on every page load is a cost bug that arrives as a bill.