🎓 A1Learner
🔍
0 of 50 lessons completed

Prometheus: Retention and Storage

Retention is how long Prometheus keeps old samples before it deletes them. The default is 15 days.

See the retention setting

The runtime info endpoint reports the retention the server is actually using.

curl -s http://127.0.0.1:19105/api/v1/status/runtimeinfo | python3 -c "import json,sys; print('retention:', json.load(sys.stdin)['data'].get('storageRetention'))"

Measure the disk the lab uses

After about five minutes of scraping three targets, the whole data directory is tiny.

du -sh data

Look inside the data directory

The WAL holds the fresh samples. No sealed block exists yet, because the lab is only minutes old.

du -sh data/*

Example

du -sh data; curl -s "http://127.0.0.1:19105/api/v1/query?query=up" | python3 -c "import json,sys; print([(r['metric'].get('job'),r['value'][1]) for r in json.load(sys.stdin)['data']['result']])"

Key points

  • Retention: the age limit for stored samples, set with --storage.tsdb.retention.time.
  • The default limit is 15 days, so a sample older than that is deleted.
  • Prometheus stores samples in a TSDB, a database built for time-stamped numbers.
  • A TSDB block is a sealed chunk of about two hours of samples, stored on disk.
  • New samples land in the head block, which stays open until it is sealed.
  • The write-ahead log, or WAL, records each sample first so a crash loses nothing.
  • More series and more labels mean more disk, because every series is stored apart.
  • Retention by size is the other limit, set with --storage.tsdb.retention.size.

In this example, the same data directory measured 588K after about fifteen minutes of scraping, up from 184K after five minutes. All four jobs still answer up as 1. The growth is slow because the lab has few series, and the 15 day limit means the directory stops growing once the oldest samples age out.

Note

Retention deletes old data on a schedule. It never asks first, so export anything you must keep.

Try it Yourself

Edit the code, then press Run.

Running is simulated · Rust Playground

Exercise

Your Prometheus data directory keeps growing. What sets the age at which old samples are deleted?