Sub topic 17
Storage and Retention Planning
Assume you are comfortable with Linux storage, systemd, YAML, and basic Prometheus queries. This chapter focuses on the local Prometheus time-series database, called the TSDB. The TSDB stores samples on disk so that dashboards and alerts can query recent history.
How Prometheus uses disk
Prometheus writes incoming samples to a write-ahead log (WAL) before compacting them into longer-term blocks. The WAL helps recover data after a crash. Compaction merges smaller blocks into larger ones, which improves query performance and reduces overhead.
A retention policy controls how long or how much data Prometheus keeps. You can limit retention by time, by disk size, or by both. Prometheus removes old data when either limit requires cleanup.
Choose limits and leave headroom
Retention time is easy to explain to users. For example, 30d means that normal queries can inspect approximately the last 30 days. Retention size is useful when the disk has a strict capacity limit.
| Setting | Example | Main purpose |
|---|---|---|
| Retention time | `30d` | Keep a predictable amount of history |
| Retention size | `20GB` | Prevent the database from filling a volume |
| Data directory | `/var/lib/prometheus` | Separate metrics from configuration |
Do not allocate the entire filesystem to Prometheus. Leave room for the operating system, logs, temporary files, WAL growth, and compaction. A full filesystem can cause failed scrapes, failed queries, and service instability.
Prometheus storage usage depends on the number of active time series, scrape interval, label cardinality, and retention period. A rough planning process is:
- Measure the current data directory size.
- Record the number of active series and the scrape interval.
- Project storage growth over the desired retention period.
- Add headroom for WAL and compaction.
- Set a size limit below the safe capacity of the filesystem.
High-cardinality labels, such as request IDs or full URLs, can create many unique time series. Avoid them because they increase memory use and storage costs.
Configure retention
Set retention options in the Prometheus service arguments. This example keeps up to 30 days of data but stops at 20 GB:
prometheus \
--config.file=/etc/prometheus/prometheus.yml \
--storage.tsdb.path=/var/lib/prometheus \
--storage.tsdb.retention.time=30d \
--storage.tsdb.retention.size=20GBTreat local retention as query history, not as your only backup. If the host or volume is lost, the local TSDB is lost too. For longer history or disaster recovery, consider remote storage or a separate Prometheus-compatible system. These options add network, cost, and operational complexity.
Exercise
If a Prometheus host has a 100 GB volume, should you set retention size to 100 GB?
Show answer
No. Set it below the volume capacity and reserve space for the operating system, logs, WAL growth, and compaction.