SYSTEM DESIGN SKETCHBOOK / PRACTICE SHEET / NO. 10
Design a metrics aggregation and query system
Every service in the company reports numbers such as request counts, queue depths and latency histograms, each tagged with things like service, region and host. Engineers open dashboards that chart these numbers over the last hour or the last year, and they expect a chart to load while they are still looking at it.
Requirements
- Accept samples for a series identified by a metric name plus a set of tag key/value pairs.
- Answer range queries that filter by tags, group by tags, and aggregate (sum, rate, max, percentiles) over a time range.
- Keep recent data at full resolution and older data at coarser resolutions, deleting data past its retention.
- Stop one team from degrading the system for everyone else.
Constraints and assumptions
- 50 million active series, each reporting one sample every 10 seconds (about 5 million samples per second, peaks of 2x).
- A sample is a 64-bit timestamp and a 64-bit float. Series have up to 10 tags.
- Retention: 10-second data for 15 days, 5-minute rollups for 90 days, 1-hour rollups for 2 years.
- Dashboard queries over the last 6 hours should return in under 1 second at p95; a 30-day query may take a few seconds.
- Samples arrive already collected and batched by an upstream pipeline. They can be up to 10 minutes late.
Out of scope for the first sketch
Agents on hosts and the collection transport · Alert rule evaluation and paging · Logs and traces
Draw your design
Rough math and notes
SYSTEM DESIGN SKETCHBOOK / FOLLOW-UPS
Design a metrics aggregation and query system
Finish your first sketch before answering these. Cross out freely.
- A dashboard asks for p99 latency across 2,000 hosts over the last 30 days. Which data do you read, and what about a percentile makes rollups tricky?
- A team ships a change that adds a user_id tag, and their series count jumps from 100 thousand to 20 million in an hour. What breaks first, and how do you stop it?
- A storage node crashes after acknowledging a batch but before that data is in a durable block. What is lost, and how does the node recover?
- A region was partitioned and its samples arrive 10 minutes late. What happens to the rollups that already covered that window?
- Several teams open the same heavy dashboard during an incident. How do you keep the query path from taking down ingestion?
Compare afterwards: worked approaches and reading at sketchbook /questions/metrics-aggregation/review