Head, WAL & Blocks
The write path, compaction, and what is on disk
Prometheus's storage engine, the TSDB, is built for one workload: millions of series, each receiving a sample every few seconds, almost always queried over recent time. Its design looks a lot like the LSM-tree storage engines from DDIA chapter 4: buffer writes in memory, log them for durability, flush immutable files, and merge those files in the background.
The head block
All recent data lives in the head block, in memory. For each active series the head holds:
- its label set and an entry in the in-memory inverted index (label → series IDs), used to resolve selectors
- the current chunk: up to 120 samples, compressed as they arrive
Samples compress remarkably well with the Gorilla scheme (from Facebook's 2015 paper): timestamps are stored as delta-of-deltas (a regular 15s scrape compresses to almost nothing), and values as XOR against the previous value (a counter that changes a little, or a gauge that doesn't change, takes a few bits). The typical result is 1-2 bytes per sample instead of 16.
When a chunk is full it's written to chunks_head/ and
memory-mapped, so the OS can page it out. Only the chunk still being
written must stay in RAM.
The write-ahead log
Memory doesn't survive a crash. Every sample (and every new series) is
first appended to the WAL in wal/, in 128 MB segment files. On
restart, Prometheus replays the WAL to rebuild the head.
WAL replay takes time, often minutes on a busy server, and Prometheus
doesn't scrape during it. So a restart creates a gap in every
graph, and alerts using rate() across that gap behave oddly. Plan
restarts, watch replay progress at /api/v1/status/walreplay (or the
"WAL replay completed" log line, which includes the duration), and in
an HA pair, restart the replicas one at a time.
Blocks
Once the head spans more than about 3 hours, Prometheus cuts the oldest 2 hours into a persistent block: a directory with an immutable snapshot of those 2 hours.
01JA7N4K2E6F8T6M6TQ5W2V9XH/ # ULID: sortable by creation time
├── chunks/
│ └── 000001 # compressed samples, up to 512 MB per file
├── index # symbols, series, postings (label -> series), offsets
├── meta.json # minTime, maxTime, stats, compaction level, parents
└── tombstones # deletions waiting for compactionThe block's index holds the postings lists: for every label pair,
the sorted list of series IDs that have it. A selector like
{job="demo-app", status=~"5.."} becomes an intersection of postings
lists, the same idea as a search engine's inverted index.
Compaction
Small blocks are merged in the background into larger ones: three 2-hour blocks into a 6-hour block, then those into bigger ones, up to 10% of the retention period or 31 days, whichever is smaller. Compaction:
- reduces the number of blocks a long query has to open
- deduplicates the index across blocks (each series' labels stored once)
- physically removes data marked in
tombstones - makes retention cheap: deleting old data means deleting whole block directories
Immutable blocks are what make Prometheus's storage simple and robust. A block is never modified after it's written. Backups can copy block directories while Prometheus runs; Thanos and Mimir upload exactly these directories to object storage. Deleting a time range doesn't rewrite anything, it adds tombstones, and the next compaction leaves the deleted data out.
Look at it yourself
The stack's TSDB is in the prometheus_data volume:
docker exec prometheus ls -la /prometheus /prometheus/wal /prometheus/chunks_head
# per-block stats, highest cardinality labels, churn
docker exec prometheus promtool tsdb analyze /prometheus
# list blocks with time ranges and sample counts
docker exec prometheus promtool tsdb list -r /prometheusOn a stack that's been up for less than about 3 hours, analyze
reports no blocks found: everything is still in the head and WAL.
Come back after a few hours to see the first 2-hour block appear, and
after a day to see compacted ones.
TSDB metrics
Prometheus monitors its own storage. These are on the Stack Health dashboard:
prometheus_tsdb_head_series # active series in memory
rate(prometheus_tsdb_head_samples_appended_total[5m]) # ingestion rate
prometheus_tsdb_head_chunks # chunks in the head
prometheus_tsdb_storage_blocks_bytes # disk used by blocks
prometheus_tsdb_wal_storage_size_bytes # disk used by the WAL
rate(prometheus_tsdb_compactions_total[1h])
prometheus_tsdb_compactions_failed_total # should be 0
rate(prometheus_tsdb_head_series_created_total[5m]) # churn: series being createdSnapshots and backups
The admin API (enabled with --web.enable-admin-api, off in this stack)
can snapshot the TSDB using hard links, without stopping Prometheus:
curl -X POST localhost:9090/api/v1/admin/tsdb/snapshot
# -> /prometheus/snapshots/20261012T060000Z-...
# copy that directory somewhere safe; it's a set of ordinary blocksMany teams don't back up Prometheus at all: the data is recent, replaceable operational data, and an HA pair already provides redundancy. If you need durable history, that's a job for long-term storage, not backups.
Lab
- Run
ls -la /prometheus/waltwice, a minute apart. Watch the segment grow. docker restart prometheus, thendocker logs prometheus 2>&1 | grep -i replayto see how long the WAL replay took. Find the gap in the Requests by route graph.- Leave the stack running for 3+ hours, then run
promtool tsdb list -rand read the first block'smeta.json.