Every Prometheus server answers PromQL range queries over weeks of data in milliseconds, on a single machine, without an index server or a query coordinator. That is not an accident. It falls out of a storage design that makes one big bet: metrics data is append-only, so the whole database can be built from immutable blocks plus a write-ahead log. Understanding how that design works explains almost every operational quirk you will hit — why disk fills in two-hour spikes, why queries over long ranges slow down, and why deleting data via the API doesn’t free space immediately.
This post walks through the internals of the Prometheus time series database (TSDB): the block model, the compression scheme, the head block and WAL, compaction, and what actually happens when a query runs. If you operate Prometheus, these are the mechanics behind your capacity planning and your backup strategy.
Everything Is a Two-Hour Block
The core structure is simple: ingested samples are grouped into two-hour blocks. Each block is an immutable directory containing a chunks subdirectory (the actual sample data, split into segment files of up to 512 MB by default), an index file that maps metric names and label sets to the series in those chunks, a meta.json with the time range and statistics, and a tombstones file for deletion records. A data directory looks like this:
./data
├── 01BKGV7JBM69T2G1BGBGM6KB12
│ └── meta.json
├── 01BKGTZQ1SYQJTR4PB43C8PD98
│ ├── chunks
│ │ └── 000001
│ ├── tombstones
│ ├── index
│ └── meta.json
├── chunks_head
│ └── 000001
└── wal
├── 000000002
└── checkpoint.00000001
└── 00000000
The block names are ULIDs — the first 48 bits encode a timestamp, so the directory listing itself is a sorted time index. Immutability is the point. Once a block is written, nothing modifies it: no in-place updates, no locks, no torn writes. Deletion via the API only appends to the tombstone file; the samples stay on disk until retention cleanup drops the whole block. This is why DELETE requests don’t reclaim space right away, and why a botched deletion can leave stale data readable until the affected blocks age out.
Compression: Why One Sample Costs About a Byte
Sample data is compressed using the scheme from the Gorilla paper, Facebook’s in-memory TSDB. Timestamps are encoded as deltas-of-deltas: since metrics are scraped on a regular interval, the difference between successive deltas is almost always zero, and it costs a single bit to say so. Float values are stored as XOR against the previous value — consecutive gauge readings usually differ in only a few mantissa bits, so the XOR is sparse and compresses to a handful of bits.
The result is that Prometheus stores an average of 1–2 bytes per sample. That number is the foundation of capacity planning, because the official disk estimate is just multiplication:
needed_disk_space = retention_time_seconds
* ingested_samples_per_second
* bytes_per_sample
Note what the formula says about leverage: the scrape interval is linear, but the number of series is effectively linear too, and series count is where cardinality explosions come from. A single histogram family with default buckets adds roughly 12 series per label combination. Compression works within a series — the deltas-of-deltas trick depends on regular spacing — so fewer, cleaner series compress better than more series scraped faster.
The Head Block and the WAL
Where do samples live during the current two-hour window? In the head block: an in-memory structure holding all active series. The head is not fully persisted as it fills — that would require rewriting files constantly — so durability comes from a write-ahead log. Every incoming sample is appended to the WAL before it is acknowledged. WAL files live in the wal/ directory in 128 MB segments, and Prometheus keeps a minimum of three of them, enough to replay at least two hours of raw data on restart.
WAL segments would grow unboundedly without cleanup, so periodically Prometheus writes a checkpoint that compactly records the still-relevant series state, and older segments are dropped — you can see the checkpoint.00000001 directory in the layout above. Recent chunks also spill to disk through memory mapping: once a chunk is full it is flushed to chunks_head/ and m-mapped, releasing heap while staying readable. This is why the head region (wal/ plus chunks_head/) consumes memory and disk even though it isn’t a “block” yet, and why a crash loses nothing: on startup the WAL is replayed over the checkpoint and the m-mapped chunks to rebuild the head.
One operational consequence: if the WAL gets corrupted or truncated, recent samples in that window are gone. Snapshots are the recommended backup mechanism — a snapshot hard-links all block directories into a new directory. Backing up only the block directories and excluding wal/ and chunks_head/ gives you a coherent backup, at the cost of losing the most recent (up to two-hour) window.
Compaction and Retention
Every two hours, the head block is cut: its data is written out as a persistent block, and the head is drained. Then a background compactor starts merging small blocks into larger ones — two-hour blocks become six-hour, twelve-hour, and eventually day-scale blocks. Compaction has a ceiling: compacted blocks span at most 10% of the retention period, or 31 days, whichever is smaller. Bigger blocks mean fewer directories and a cheaper index per query, but they also delay retention cleanup, since an immutable block can only be deleted when it falls entirely outside the retention window.
Retention defaults to 15 days if neither --storage.tsdb.retention.time nor --storage.tsdb.retention.size is set. The size-based option has a subtlety worth knowing: only persistent blocks are deleted to honor it, but WAL and m-mapped head chunks count toward the total — so the disk must cover the peak of wal/ plus chunks_head/ on top of the retention budget, and that peak recurs every two hours. If compaction and size-retention race, on-disk size can briefly exceed the configured limit until the next cleanup drops the source blocks.
What a Query Actually Does
When PromQL evaluates a range query, the engine computes the set of blocks whose time ranges overlap the query window, loads each block’s index, looks up the matching series through inverted posting lists (label-value → series → chunk references), and then merges results across blocks and the head. Because blocks are immutable and time-partitioned, this merge is straightforward — but it is linear in the number of blocks touched. This explains two familiar symptoms:
First, queries that span many days must scan and merge dozens of blocks, which is why long-range dashboard queries are slower than “last 5 minutes” queries regardless of sample count. Second, tombstones are consulted at query time, so deletion is a read-time filter, not a storage-time operation — another reason API deletions look like they did nothing.
Also worth internalizing: this storage is strictly local and single-node. There is no clustering or replication in the TSDB itself; durability is whatever the disk provides. For longer retention or HA, Prometheus exposes remote read/write APIs that stream samples to external long-term storage, which is the integration path behind projects like Thanos and Cortex. The local database is a deliberate scope decision, not a limitation someone forgot to fix.
Practical Takeaways
A few rules that fall directly out of the design:
- Size your disk for head + blocks. Budget the two-hour peak of
wal/andchunks_head/separately from retention, or you will get periodic ENOSPC surprises. - Back up with snapshots, block directories only. Copying the data directory live can capture a half-written block; snapshots give you a consistent set.
- Treat memory usage as proportional to active series. The head holds every active series in RAM; cardinality reduction is memory reduction.
- Don’t fight immutability. Retention, compaction and deletion are all block-granular operations — time-based deletion requests only change what gets read, not what sits on disk.
The TSDB is a good reminder that the best storage engines are usually the ones with the fewest moving parts: immutable time-partitioned files, a WAL for the tail, and background compaction. Once you can picture the block layout, most of Prometheus’s operational behavior stops being mysterious.