Sub topic 17
Storage and retention planning
This chapter explains how Prometheus stores samples on local disk, how to size that disk, and how to set retention so the server does not fill its volume. It assumes you have a running Prometheus server managed by systemd, as set up in earlier chapters, and that you can read PromQL.
How the local time series database works
Prometheus stores data in its own time series database (TSDB) on the local filesystem. Every scraped sample goes through these stages:
- The sample is appended to the head block, which lives in memory.
- The same sample is written to the write-ahead log (WAL) on disk. After a crash or restart, Prometheus replays the WAL to rebuild the head.
- Roughly every two hours, the head is written out as an immutable block directory containing chunks, an index, and metadata.
- A background process compacts small blocks into larger ones. This reduces the number of blocks that queries must open.
- Blocks that fall outside the retention limits are deleted.
Because blocks are immutable, retention works at block granularity. Prometheus removes a block only when the whole block is outside the limit. Disk usage can therefore run slightly above the configured retention.
Retention settings
Retention is set with command-line flags, not in prometheus.yml. You can set a time limit, a size limit, or both.
| Flag | Meaning | Default |
|---|---|---|
| `--storage.tsdb.path` | Directory that holds the database | `data/` |
| `--storage.tsdb.retention.time` | Delete blocks older than this age | `15d` |
| `--storage.tsdb.retention.size` | Delete the oldest blocks once storage exceeds this size | none |
If you set both limits, whichever is reached first triggers deletion. Changing a flag requires a restart. A reload does not apply it.
The systemd unit for Prometheus carries the flags:
[Unit]
Description=Prometheus
After=network-online.target
Wants=network-online.target
[Service]
User=prometheus
ExecStart=/usr/local/bin/prometheus \
--config.file=/etc/prometheus/prometheus.yml \
--storage.tsdb.path=/var/lib/prometheus \
--storage.tsdb.retention.time=30d \
--storage.tsdb.retention.size=40GB
Restart=on-failure
[Install]
WantedBy=multi-user.targetAfter you edit the unit, run sudo systemctl daemon-reload && sudo systemctl restart prometheus.
Estimating disk needs
Disk use is roughly:
disk bytes = retention seconds × ingested samples per second × bytes per samplePrometheus compresses samples well. A typical figure is 1 to 2 bytes per sample, and 2 is a safe planning number. Samples per second is the number of active series divided by the scrape interval.
Here is an example with 20 servers, each exposing about 1,000 series through node_exporter, scraped every 15 seconds, and kept for 15 days:
- Active series: 20 × 1,000 = 20,000.
- Samples per second: 20,000 / 15 ≈ 1,333.
- Retention: 15 × 86,400 = 1,296,000 seconds.
- Samples stored: 1,333 × 1,296,000 ≈ 1.73 billion.
- Disk at 2 bytes per sample: about 3.5 GB.
Add 20 to 30% headroom for the WAL and compaction, and plan for about 4.5 GB. Doubling the retention doubles the disk. Halving the scrape interval to 7.5 seconds also doubles it.
Measure your own numbers instead of trusting the typical figures. Run these queries in the Prometheus expression browser:
# Series currently held in the head
prometheus_tsdb_head_series
# Samples ingested per second
rate(prometheus_tsdb_head_samples_appended_total[5m])
# Bytes used by persisted blocks
prometheus_tsdb_storage_blocks_bytes
# Real bytes per sample over the last day (approximate)
rate(prometheus_tsdb_compaction_chunk_size_bytes_sum[1d])
/ rate(prometheus_tsdb_compaction_chunk_samples_sum[1d])The last query uses compaction statistics, so it only returns data after at least one compaction has run. Use it to replace the 2-byte assumption with your measured value.
Memory and cardinality
Disk is usually the easier resource to plan. Memory follows the number of active series, because the head block holds every active series in RAM. As a planning figure, expect several kilobytes of memory per active series, and test against your own workload.
Cardinality is the number of distinct series. It grows whenever a label can take many values, such as a user ID, a request path with embedded IDs, or a container ID that changes on every deploy. Series with short lives still cost memory while they are in the head, and they leave entries in the index. Watch for sudden jumps:
# Top 10 metric names by series count
topk(10, count by (__name__) ({__name__=~".+"}))This query touches every series, so run it sparingly on large servers.
Alerting on storage health
Prometheus exposes its own metrics, so you can alert on it the same way you alert on node metrics. Put this in a rules file:
groups:
- name: prometheus-storage
rules:
- alert: PrometheusDiskFillingUp
expr: predict_linear(node_filesystem_avail_bytes{mountpoint="/var/lib/prometheus"}[6h], 4 * 3600) < 0
for: 30m
labels:
severity: warning
annotations:
summary: "Prometheus data disk on {{ $labels.instance }} is predicted to fill within 4 hours"This rule assumes the data directory is on its own filesystem mounted at /var/lib/prometheus. Adjust the mountpoint matcher to match your layout. A separate volume for the data directory also keeps a full TSDB from taking down the rest of the system.
What local storage does not give you
Prometheus local storage has limits that affect planning:
- It is not replicated. If the disk is lost, the history is lost.
- It does not downsample. A year of data at 15-second resolution costs about 24 times the disk of 15 days.
- It is not supported on network filesystems such as NFS, because the TSDB depends on POSIX filesystem semantics.
You have several options for the gaps:
- Snapshots. Start Prometheus with
--web.enable-admin-api, then callPOST /api/v1/admin/tsdb/snapshot. The snapshot is written under the data directory insnapshots/and uses hard links, so it is cheap. Copy it to other storage afterwards. Restrict access to the admin API, because it also allows deleting series. - Remote write. Configure
remote_writeto send samples to a long-term backend. This is the usual answer when you need a year or more of history. - Recording rules. These reduce query cost, but they do not reduce storage of the raw series.
- Deleting unwanted series. The admin API endpoint
delete_seriesmarks series for deletion, andclean_tombstonesfrees the space.
For a small production setup, local storage with 15 to 30 days of retention, a dedicated volume, and a periodic snapshot or volume backup is a reasonable baseline.
Checking the result
After restarting with new flags, confirm they took effect on the Runtime & Build Information page (/status) or through the flags API:
curl -s http://localhost:9090/api/v1/status/flags | grep -o '"storage.tsdb.retention[^,]*'Then check the disk over several days. prometheus_tsdb_storage_blocks_bytes should level off once the oldest data starts to expire. If it keeps rising after the retention period has passed, check for rising cardinality or a retention flag that did not apply.
Exercise
A Prometheus server scrapes 50 servers every 30 seconds. Each server exposes about 1,200 series. You want 30 days of retention and plan for 2 bytes per sample. Estimate the disk needed before headroom, and name the flag you would set to cap the database size.
Show answer
Active series = 50 × 1,200 = 60,000. Samples per second = 60,000 / 30 = 2,000. Retention = 30 × 86,400 = 2,592,000 seconds. Samples stored = 2,000 × 2,592,000 = 5.184 billion. At 2 bytes per sample this is about 10.4 GB, so plan for roughly 13 GB with 25% headroom. To cap the database size, set --storage.tsdb.retention.size, for example to about 80% of the volume capacity.
Exercise
You change --storage.tsdb.retention.time in the systemd unit file and run systemctl reload prometheus. The old retention still applies. Why, and what should you run instead?
Show answer
Retention flags are read only at startup, and a reload only re-reads prometheus.yml. Run sudo systemctl daemon-reload so systemd picks up the edited unit, then sudo systemctl restart prometheus.