Prompt
How do I optimize compression in a time-series database for multi-year retention?
Latest observation
Optimizing compression for multi-year time-series retention usually comes down to 5 things:
- Choose the right data model
- Use good time-based partitioning
- Tune block/chunk sizes and encoding
- Control cardinality and tag growth
- Move old data to colder, more compressed tiers
Here’s a practical checklist.
1) Model the data for compression
Time-series compresses best when values are:
- append-only
- ordered by time
- grouped by series/device/metric
- low entropy where possible
Good practices
- Keep timestamps monotonically increasing within a series.
- Avoid frequent updates/deletes in hot storage.
- Store numeric values as numbers, not strings.
- Separate:
- high-cardinality labels/tags
- from the actual measurements
Reduce cardinality
High cardinality hurts compression and indexing.
- Don’t put user IDs, request IDs, UUIDs, or ephemeral values in tags unless necessary.
- Prefer:
host,region,sensor_type- over
session_id,trace_id
2) Partition by time, and roll partitions forward
For multi-year retention, time partitioning is essential.
Why it helps
- Smaller partitions mean better pruning on queries.
- Old partitions can be compressed differently or moved to cheaper storage.
- Dropping expired data becomes trivial.
Recommended pattern
- Partition by day/week/month depending on ingest rate.
- Use larger partitions for older data if your DB supports it.
- Keep hot data in recent partitions, then compact/archive older ones.
Retention strategy
Example:
- 0–30 days: high-resolution, fast queries
- 30–365 days: compressed/compacted
- 1–5 years: colder storage, coarser resolution if acceptable
3) Tune compression settings
Most time-series databases use combinations of:
- delta encoding
- delta-of-delta for timestamps
- run-length encoding
- dictionary encoding
- bit packing
- ZSTD/LZ4 or similar page/segment compression
Best compression settings depend on your data:
- Regular interval metrics: delta-of-delta works very well
- Slowly changing values: delta or RLE
- Repeated strings/categories: dictionary encoding
- Sparse data: consider storage format that handles nulls efficiently
Chunk/block size
Compression often improves with larger chunks, but only up to a point.
- Too small: overhead dominates
- Too large: worse query latency and more memory use
A common approach:
- Use a moderate chunk size for hot data
- Increase chunk size for cold/archived data if supported
4) Downsample older data
For multi-year retention, keeping every raw point forever is often expensive.
Common downsampling patterns
Store:
- raw 1s data for 7–30 days
- 1m aggregates for 1 year
- 1h aggregates for 5 years
Aggregate by:
avgminmaxsumcountp95/p99if needed
This can reduce storage by orders of magnitude while preserving long-term trends.
Tip
Keep raw and aggregated data in separate retention tiers or tables.
5) Use tiered storage
If your DB supports it, use:
- hot tier: fast SSD, recent data
- warm tier: compressed local/object storage
- cold tier: object storage / archive
This is often better than trying to compress everything aggressively in one tier.
6) Compact and vacuum regularly
Over time, ingest, updates, and deletes create fragmentation.
Actions
- Run compaction/merge jobs regularly
- Reclaim tombstoned data
- Rebuild indexes when necessary
- Keep old shards/segments merged into larger compressed files
This can significantly improve both storage and query efficiency.
7) Choose the right encoding for the workload
For timestamps
- Delta-of-delta is ideal for regular sampling
- For irregular sampling, delta still helps
For values
- Floating-point sensor data often compresses well with Gorilla-style encodings
- Integer counters benefit from delta + bit packing
- Booleans compress extremely well with RLE
For dimensions/tags
- Dictionary encoding is usually best
- Normalize repeated metadata into separate tables if the DB supports joins efficiently
8) Avoid anti-patterns
Common storage killers:
- Writing out-of-order points frequently
- Too many series due to overly granular tags
- Storing JSON blobs in the main measurement table
- High-frequency updates to existing rows
- Keeping raw data forever when aggregates would do
- Using short retention windows for partitions that could have been compacted
9) Validate with real data
Compression effectiveness is very workload-specific.
Measure:
- bytes per point
- compression ratio by metric/series
- ingest throughput
- query latency on recent and historical data
- compaction cost
- storage growth over time
Test on:
- your most common metric patterns
- your worst-case cardinality patterns
- your oldest retention window
10) Practical target architecture
A common multi-year setup:
- Hot: raw data, 1s resolution, 7–30 days
- Warm: compacted raw, 1m/5m chunks, 3–12 months
- Cold: downsampled aggregates, 1–5 years+
- Archive: object storage or cheaper disk, queried rarely
Quick rules of thumb
- Prefer time-based partitions.
- Keep series cardinality low.
- Compress recent data with delta/delta-of-delta and columnar encodings.
- Downsample for long-term retention.
- Compact old partitions.
- Move cold data to cheaper storage.
- Benchmark with your real workload.
If you want, I can tailor this to a specific database like InfluxDB, TimescaleDB, ClickHouse, Prometheus, QuestDB, or Apache Druid, since the best compression strategy differs quite a bit by engine.