Prompt
How do I downsample metrics data automatically over time?
Latest observation
Oct 25, 2026
OpenAI APIWeb search: off
Automatically downsampling metrics over time usually means:
- Keep raw, high-resolution data for a short period
- Roll it up into lower-resolution aggregates as it ages
- Delete the raw data once the rolled-up version is safe to use
Common pattern
Use a retention + aggregation policy like:
- 0–7 days: keep 10-second or 1-minute samples
- 7–30 days: store 5-minute averages/min/max/sums
- 30–365 days: store 1-hour aggregates
- Older than 1 year: keep only daily aggregates or delete
What to aggregate
Depends on the metric type:
- Gauge values like CPU, temperature:
- average
- min
- max
- last
- Counter values like requests, bytes:
- sum over interval
- rate derived from deltas
- Histogram/latency:
- percentiles if supported
- bucket counts
- average/min/max
How to automate it
Option 1: Time-series database built-in rollups
Many systems support this natively:
- InfluxDB: retention policies + continuous queries / tasks
- TimescaleDB: continuous aggregates
- Prometheus: recording rules + remote storage for long-term retention
- VictoriaMetrics / M3 / Thanos: downsampling or compaction features
Option 2: Scheduled batch jobs
If your database doesn’t do this automatically:
- Run a cron job every hour/day
- Query older raw data in time windows
- Write aggregates into a lower-resolution table/measurement
- Delete or archive the raw data after verification
Example strategy
For a metric sampled every 10 seconds:
- Store raw 10-second points for 7 days
- Every hour, aggregate the last completed hour into 1-minute rows
- After 7 days, remove raw 10-second rows
- After 30 days, aggregate 1-minute rows into 1-hour rows
- After 1 year, keep only 1-day rows
Important design tips
- Align rollup windows to fixed boundaries like
:00,:05,:10 - Preserve enough statistics to avoid losing information
- Be careful with averages: prefer storing
sumandcountso you can recompute - For counters, store rates or deltas, not raw values alone
- Make the process idempotent so reruns don’t duplicate data
Example SQL idea
A generic rollup might look like:
INSERT INTO metrics_1m (time_bucket, metric_name, avg_value, min_value, max_value, count)
SELECT
date_trunc('minute', ts) AS time_bucket,
metric_name,
AVG(value),
MIN(value),
MAX(value),
COUNT(*)
FROM metrics_raw
WHERE ts >= now() - interval '1 hour'
GROUP BY 1, 2;
Then later:
DELETE FROM metrics_raw
WHERE ts < now() - interval '7 days';
Best practice
If you’re starting from scratch, choose a TSDB or warehouse that supports:
- retention tiers
- rollups/downsampling
- continuous aggregates
- automated deletion of old raw data
If you tell me what stack you’re using (Prometheus, InfluxDB, TimescaleDB, SQL, etc.), I can give you the exact setup and example config.