Prometheus stores data as time series in a custom on-disk time-series database (TSDB) that is optimized for high-dimensional metrics with frequent writes and efficient querying. Each time series is identified by a metric name and a set of key-value labels, and samples are appended in timestamp order, with periodic compaction and retention policies controlling disk usage.
Background: Why Prometheus Has Its Own Storage Engine
Prometheus was designed by SoundCloud in 2012 as an open-source monitoring system for dynamic, container-based environments. The original version relied on an in-memory storage layer with optional persistence, but as usage grew, the need for a durable, scalable local store became clear. In 2016, the Prometheus team introduced the current TSDB (time-series database) as part of version 2.0, replacing the older LevelDB-based approach. The design goals were to handle millions of samples per second, support flexible label-based queries, and operate without external dependencies like a separate database server. This local, embedded storage keeps Prometheus simple to deploy and operate, but it also means data is not shared across multiple Prometheus instances by default.
Core Architecture: Blocks, Chunks, and the WAL
Prometheus stores data in a directory (default data/) that contains several subdirectories and files. The fundamental unit is a block, which is a directory holding samples for a fixed time range (typically two hours). Each block contains multiple chunk files that store the actual sample values, plus an index file that maps metric names and labels to the chunks, and a meta.json file with block metadata. The Write-Ahead Log (WAL) is a separate directory that records incoming samples before they are flushed to blocks, providing crash recovery. On startup, Prometheus replays the WAL to restore any data that was not yet persisted. This architecture allows efficient compaction: older blocks are periodically merged into larger blocks (e.g., from 2-hour blocks to day-long blocks) to reduce overhead and improve query performance.
How Samples Are Written and Read
When a target is scraped (Prometheus pulls metrics via HTTP), the samples are appended to the WAL and then to an in-memory head block. The head block holds recent data and is periodically flushed to disk as a new block. Each sample is a tuple of (timestamp, value) stored in a compressed format using a variant of Gorilla compression, which reduces storage footprint by exploiting patterns in time-series data (e.g., constant or slowly changing values). For reading, Prometheus uses a query engine that first checks the in-memory head for recent data, then searches on-disk blocks. The index file is a reverse index that maps label name-value pairs to series, enabling fast lookups. For example, a query like http_requests_total{status="500"} uses the index to locate all series with that label, then reads the relevant chunks.
Retention, Compaction, and Disk Usage
By default, Prometheus retains data for 15 days, but this is configurable via the --storage.tsdb.retention.time flag. Retention is enforced by deleting entire blocks that are older than the retention period, so data is removed in bulk rather than sample-by-sample. Compaction runs in the background, merging smaller blocks into larger ones and deleting tombstones (markers for deleted series). Disk usage depends on the number of series, the sample frequency, and the compression efficiency. For instance, a single server scraping 10,000 targets every 15 seconds can generate roughly 1–2 GB per day, but this varies widely. You can estimate storage needs by monitoring the prometheus_tsdb_compaction_duration_seconds and prometheus_tsdb_head_series metrics. It is important to monitor disk space because Prometheus does not automatically stop writing when disk is full; it may fail to append new samples and eventually crash.
Common Problems and Misconceptions
- “Prometheus is a database” – It is a monitoring system with an embedded TSDB, not a general-purpose database. It does not support SQL or joins across unrelated metrics.
- “Data is lost on restart” – With the WAL, data is durable; Prometheus replays the WAL on startup. However, if the WAL is corrupted or disk fails, data can be lost.
- “Scraping interval equals storage resolution” – Prometheus stores exactly the samples it scrapes; it does not downsample by default. If you scrape every 15 seconds, you store every 15-second sample. Downsampling is not built-in, so long-term storage can become large.
- “You can query across multiple Prometheus servers” – The local TSDB is per-instance. To query across servers, you need federation or a separate system like Thanos or Cortex.
- “Deleting a series frees disk immediately” – Deletion marks tombstones, but the underlying chunks are only removed during compaction. Space may not be reclaimed until later.
Comparison with Other Storage Backends
Prometheus’s local TSDB is designed for short-to-medium retention (days to weeks) and high-cardinality metrics. For long-term storage or horizontal scaling, external solutions are commonly used. Thanos and Cortex both use Prometheus’s TSDB format but add object storage (e.g., S3) and query federation. VictoriaMetrics is a separate TSDB that is compatible with Prometheus query language but uses a different storage engine with better compression and downsampling. InfluxDB is a general-purpose time-series database that can ingest Prometheus data via a write endpoint, but it has its own query language. The choice depends on your needs: if you only need a few weeks of data and a single instance, Prometheus’s built-in storage is sufficient. For multi-month retention or high availability, Thanos or Cortex are common choices.
How to Choose Storage Settings for Your Use Case
When configuring Prometheus storage, consider the following factors:
- Retention period: How long do you need to keep data? Set
--storage.tsdb.retention.timeaccordingly. For example,30dfor a month. - Scrape frequency: Lower frequency (e.g., 30s vs 15s) halves the number of samples and storage size. Adjust the
scrape_intervalin your config. - Number of series: Each unique combination of metric name and labels is a series. Avoid high-cardinality labels like request IDs, as they explode series count and memory usage.
- Disk type: SSD is recommended for good write and query performance, especially with high throughput.
- Monitoring storage itself: Use built-in metrics like
prometheus_tsdb_head_seriesandprometheus_tsdb_compaction_duration_secondsto track health.
Frequently Asked Questions
Does Prometheus store data in memory or on disk?
Prometheus uses both. Recent samples are kept in an in-memory head block for fast access, but they are also written to the WAL on disk for durability. Older data is stored in on-disk blocks. So data is always persisted to disk, but the most recent data is also available in memory for fast queries.
Can I increase retention without losing old data?
Yes, you can change the retention flag and restart Prometheus. Existing blocks are not deleted immediately; they are only removed when they exceed the new retention period. However, if you reduce retention, blocks older than the new period will be deleted on the next cleanup cycle.
What happens if the disk fills up?
Prometheus will fail to append new samples, and the WAL may become corrupted. In severe cases, the process may crash. To avoid this, monitor disk usage and set up alerts. You can also use a separate disk for the data directory to isolate it from system logs.
How does Prometheus handle duplicate timestamps?
If a sample arrives with a timestamp that is already present for the same series, Prometheus keeps the most recent value. This can happen if you have multiple scrapes with the same timestamp, but it is rare. The TSDB stores only one sample per timestamp per series.
Is Prometheus storage suitable for long-term archival?
Not by default. The local TSDB is optimized for recent data and does not downsample, so long-term storage (years) would require enormous disk space. For archival, use external solutions like Thanos with object storage, or export data to a separate long-term database.
Related
- First Human Skull in France: The Arago Discovery
- Sacred Wellness Collective: What It Is & How It Works
- Rhea Ripley Blonde Hair? The Truth Explained
How pages are sourced and corrected: Sources and editorial policy.