Prompt
How do I downsample raw metrics automatically over time?
Latest observation
Oct 25, 2026
OpenAI APIWeb search: off
To downsample raw metrics automatically over time, the usual pattern is:
- Keep raw data for a short period
- Roll it up into coarser resolutions on a schedule
- Query the rolled-up data for older time ranges
Typical retention/downsampling strategy
Example policy:
- Raw 10s/1m data: keep for 7 days
- 5m aggregates: keep for 30 days
- 1h aggregates: keep for 1 year
What to downsample
For each metric, store aggregate values such as:
- min
- max
- avg
- sum
- count
- last (if the metric is a gauge)
- p95/p99 if you need percentiles and your backend supports it
How it works
A background job runs periodically:
- reads raw points older than a cutoff
- groups them into fixed time buckets
- writes aggregated points to a downsampled table/index/measurement
- optionally deletes the original raw points after the rollup is confirmed
Example bucketing logic
If you want 5-minute rollups:
- bucket timestamps by
floor(timestamp / 5m) * 5m - aggregate all points in each bucket
Common implementation options
1. Time-series database native retention/downsampling
Many TSDBs support this directly, for example:
- InfluxDB: retention policies + tasks/downsampling
- TimescaleDB: continuous aggregates + retention policies
- Prometheus: recording rules + remote storage
- OpenSearch/Elasticsearch: ILM/downsampling features depending on version
- Grafana Mimir/Cortex/Thanos: compaction/downsampling patterns
2. Custom scheduled ETL job
If your storage doesn’t support it natively:
- cron/job scheduler
- SQL batch job
- stream processor (Kafka Streams/Flink/Spark)
- serverless scheduled function
Design tips
- Use fixed bucket boundaries so rollups are deterministic
- Preserve labels/dimensions exactly so queries still filter correctly
- Don’t overwrite raw data; write rollups to a separate tier/table
- Make rollup jobs idempotent in case they rerun
- Choose retention based on how often old raw precision is actually needed
Example SQL approach
If you’re using a relational DB:
INSERT INTO metrics_5m (metric_name, tags, bucket_start, min_value, max_value, avg_value, count_value)
SELECT
metric_name,
tags,
date_trunc('minute', time) - (extract(minute from time)::int % 5) * interval '1 minute' AS bucket_start,
MIN(value),
MAX(value),
AVG(value),
COUNT(*)
FROM metrics_raw
WHERE time < now() - interval '1 day'
GROUP BY metric_name, tags, bucket_start;
Then delete raw data older than your raw-retention window.
If you tell me what system you’re using (Prometheus, InfluxDB, TimescaleDB, SQL, Elasticsearch, etc.), I can give you the exact setup and example configuration.