Storage, federation & remote write
|
This content was generated with the assistance of AI and should be verified against the official documentation before being relied on in production. |
A single Prometheus server is deliberately self-contained: it scrapes, stores, and queries its own
samples on local disk, with no clustering and no built-in replication. That design is simple and
fast, but it caps how much history one server can hold and rules it out as a durable archive. This
page covers the on-disk storage engine and its retention flags, the /federate endpoint that lets
one server scrape a filtered subset of another’s series, the remote_write/remote_read APIs that
forward samples to or read them back from an external system, and the long-term storage systems — Thanos, Cortex, and Mimir — that build on those APIs to remove the single-server ceiling.
The examples use one small lab: the orders-api scenario from
Recording & Alerting Rules scraped by one
Prometheus server (the "regional" server), a second server that federates from it, and a third that
receives its remote_write stream. All outputs shown were captured from those servers.
Local TSDB storage
On-disk layout, conceptually
Prometheus’s storage engine (the TSDB) keeps recent samples in an in-memory head block, backed by a write-ahead log (WAL) on disk so an unexpected restart replays and loses nothing. Every two hours the head is flushed into an immutable, self-contained on-disk block — its own directory of chunk files, an index, and metadata — covering that time window. A background compactor periodically merges adjacent blocks into larger ones, reducing per-block index/metadata overhead and reclaiming space from series that have since disappeared. None of this needs to be managed by hand; it is described here only so the retention flags below make sense. For the full picture, read Storage.
append sample"] --> WAL["WAL on disk
replayed after a crash"] SC --> HEAD["Head block
in memory, about 2h"] HEAD -->|"every 2h"| B1["Block
chunks, index, meta.json"] B1 --> CMP["Compaction
merge adjacent blocks"] CMP --> B2["Larger block"] B2 -->|"older than retention"| DEL["Block deleted"] HEAD --> Q["Queries read head and blocks"] B2 --> Q
A freshly started server shows only the head side of this picture. After the lab server had run for a
few minutes its data directory contained a wal directory with the live segment, a chunks_head directory
with memory-mapped head chunks, and a lock file, but no block yet because the first flush happens only
once the head spans about two hours:
/prometheus/data:
chunks_head/ wal/ lock queries.active
|
The official |
A block is an ordinary directory that promtool tsdb can inspect. Backfilling six hours of synthetic
samples for two series with promtool tsdb create-blocks-from openmetrics produced three blocks, which
promtool tsdb list printed with their time ranges and sizes:
BLOCK ULID MIN TIME MAX TIME DURATION NUM SAMPLES NUM CHUNKS NUM SERIES SIZE
01M72VFTTC3P5SZBDJFFCYH7MM 1794296926000 1794297586001 11m0.001s 46 2 2 920
01M72VFTXXK87PTKKP7M91PM0N 1794297616000 1794304786001 1h59m30.001s 480 4 2 1705
01M72VFVA9Z0F22G4VJG3WK21E 1794304816000 1794311296001 1h48m0.001s 434 4 2 1587
Each block directory holds chunks/ (the compressed samples), index, meta.json (the time range,
sample counts and the compaction level and sources) and tombstones (series deletions pending the next
compaction). promtool tsdb analyze <data dir> [block] reports the label pairs and label names that
contribute most to series count, which is the first tool to reach for when cardinality grows.
Retention flags
Retention is bounded by whichever of two flags is hit first — time or disk size — both passed on the command line:
prometheus \
--config.file=prometheus.yml \
--storage.tsdb.path=/prometheus \
--storage.tsdb.retention.time=15d \
--storage.tsdb.retention.size=512GB
# https://prometheus.io/docs/prometheus/latest/storage/
--storage.tsdb.retention.time deletes whole blocks once their samples fall outside the window (the
default is 15d, applied only when neither retention flag is set); --storage.tsdb.retention.size caps
the storage size, which counts the WAL and the memory-mapped head chunks as well as persistent blocks, and,
once reached, drops the oldest blocks to stay under it (only persistent blocks are ever deleted to
enforce the limit, so leave headroom for the WAL). Setting the size limit lets an
operator bound disk usage on a host with a fixed volume without having to reason about ingestion rate
and cardinality to derive a time-based equivalent. A server with the defaults reports its effective
limits through the flags API, which is the quickest way to confirm what a running instance is using:
curl -s localhost:9090/api/v1/status/flags | jq -r '.data | to_entries[] | select(.key | test("retention")) | "\(.key) \(.value)"'
storage.agent.retention.max-time 0s
storage.agent.retention.min-time 0s
storage.tsdb.retention.size 0B
storage.tsdb.retention.time 15d
A rough sizing check uses the server’s own metrics. The lab server held about 750 head series and appended roughly 120 samples per second; at the documented 1 to 2 bytes per compressed sample, 15 days of retention is about 120 samples/s times 86,400 s/day times 15 days times 2 bytes, or 311 MB at the upper end (plus the WAL, which the head holds before the first block is cut):
prometheus_tsdb_head_series
rate(prometheus_tsdb_head_samples_appended_total[5m])
prometheus_tsdb_wal_storage_size_bytes
Why local storage is not for long-term retention
Local TSDB storage is single-node and non-replicated: there is no built-in replication, no cross-server durability, and no support for horizontal scale-out of storage capacity. Losing the disk loses the data; growing beyond one host’s disk and memory means either shortening retention or routing samples elsewhere. It is well suited to the days-to-weeks window operational dashboards and alerting need, and poorly suited to compliance-grade or multi-year retention, multi-tenant querying, or globally deduplicated views across many Prometheus servers. That gap is exactly what federation (read-only, and only over a filtered subset) and the long-term storage systems described below exist to fill.
Federation
The /federate endpoint
Every Prometheus server exposes /federate, which returns the current value of whatever time
series match one or more match[] selectors, in the same exposition format a scrape target would
return. A second Prometheus server scrapes that endpoint like any other target, pulling in only the
series it asked for — typically pre-aggregated recording-rule outputs from several lower-level
servers into one higher-level, global-view server:
scrape_configs:
- job_name: "federate"
scrape_interval: 15s
honor_labels: true
metrics_path: "/federate"
params:
"match[]":
- '{job="node"}'
- '{__name__=~"job:.*"}' # recording-rule outputs, by convention
static_configs:
- targets:
- "prometheus-region-a:9090"
- "prometheus-region-b:9090"
# https://prometheus.io/docs/prometheus/latest/federation/
honor_labels: true is required so the source server’s own instance/job labels are preserved
instead of being overwritten by the scrape. The same query run directly against the endpoint:
curl -G 'http://prometheus-region-a:9090/federate' \
--data-urlencode 'match[]={job="node"}' \
--data-urlencode 'match[]={__name__=~"job:.*"}'
# https://prometheus.io/docs/prometheus/latest/federation/
Against the lab server, that request returned the matching raw series (with the scrape timestamp in milliseconds) and the recorded series (abridged):
# TYPE http_requests_total untyped
http_requests_total{instance="host.docker.internal:27411",job="orders-api",method="GET",status="200"} 4674 1794318544264
http_requests_total{instance="host.docker.internal:27411",job="orders-api",method="POST",status="500"} 15 1794318544264
# TYPE job:http_requests:rate5m untyped
job:http_requests:rate5m{job="orders-api",instance=""} 19.15064404761905 1794318534047
# TYPE job:http_requests_errors:ratio_rate5m untyped
job:http_requests_errors:ratio_rate5m{job="orders-api",instance=""} 0.010444988764209783 1794318534047
Two details are visible here. Every type is reported as untyped (federation exposes values, not metric
metadata, so rate() still works on the federated copy of a counter but the type is lost). And the
recorded series carry an empty instance="" label, which is equivalent to the label being absent. The
second server, configured with the federate job above, then answered an ordinary query for the recorded
series:
job:http_requests:rate5m
job:http_requests:rate5m{job="orders-api"} 19.93202695800932
job:...:rate5m"] end GLOBAL["Global Prometheus
scrapes /federate"] -->|"match[] selects aggregates"| RR GLOBAL --> GD["Cross-region dashboards"]
Scaling limits of federation
Federation is intentionally narrow: it hands over only the series matched by match[], at scrape
time, with no history beyond what the pull captures — it is not a way to replicate a full dataset or
back up historical data. Each additional federating server adds another full evaluation of those
selectors against the source’s local storage, so pulling many or high-cardinality series, or chaining
several layers of federation, pushes load back onto the servers being federated from. The documented,
supported use is aggregating a small number of higher-level, already-aggregated series (job- or
cluster-level recording-rule outputs) into a global view, or occasionally pulling a handful of related
series across the boundary between related services — not shipping raw per-instance metrics or
building durable long-term storage on top of it. See
Federation for the full scoping guidance.
The remote_write/remote_read APIs
Where federation pulls a filtered snapshot, remote_write and remote_read move samples wholesale
between a Prometheus server and an external system, over a standardized HTTP protocol.
remote_write
A remote_write block makes Prometheus forward every scraped sample — after any configured
relabeling — to a remote endpoint, asynchronously and with its own retrying queue, independent of
local retention:
remote_write:
- url: "https://remote-storage.example.com/api/v1/write"
queue_config:
max_samples_per_send: 5000
max_shards: 30
write_relabel_configs:
- source_labels: [__name__]
regex: "go_.*"
action: drop
The receiving system only needs to implement the write side of the protocol — this is exactly the
integration point Thanos receive, Cortex, and Mimir (below) expose. Prometheus can itself be the
receiver when started with --web.enable-remote-write-receiver, which makes it easy to try the
mechanism end to end. In the lab, the regional server above was configured with write_relabel_configs
dropping every go_* series, and the sink server then held the application and recorded series but none of the
dropped ones:
count({__name__=~"go_.*"})
# on the regional (sending) server
{} 142
# on the sink (receiving) server
(empty result)
The sender reports what it did in its own metrics, which is also how you alert on a failing or lagging remote write:
rate(prometheus_remote_storage_samples_total[5m])
prometheus_remote_storage_samples_dropped_total
prometheus_remote_storage_shards
prometheus_remote_storage_samples_pending
In the lab the first query returned about 99 samples per second against roughly 122 appended locally (the
difference is the dropped go_* series), and samples_dropped_total counted the relabel-dropped series
with reason="dropped_series". Rising samples_pending or shards approaching max_shards means the
receiver cannot keep up; a growing gap between the highest appended timestamp and the highest sent
timestamp means the WAL is buffering data (the queue replays from the WAL, so a short receiver outage
loses nothing, but Prometheus keeps unsent data in the WAL only for a limited time, a few hours, so a longer outage does).
Remote write has a stable 1.0 specification and an experimental 2.0 one, and the Prometheus server supports both; use whatever the receiving system documents.
remote_read
A remote_read block does the reverse: PromQL queries evaluated against this server can transparently
pull in matching series from the remote system for data outside local retention, merged with what is
stored locally:
remote_read:
- url: "https://remote-storage.example.com/api/v1/read"
read_recent: false # only fetch from remote what isn't already available locally
The wire format and query semantics are specified in Remote read API.
Long-term storage systems
remote_write/remote_read (plus, for some of these, a sidecar reading blocks directly off disk) are
the integration points several purpose-built systems use to give Prometheus durable, horizontally
scalable, long-term storage beyond a single server’s disk. They are linked here, not documented in
depth:
-
Thanos — a sidecar process attached to each Prometheus server uploads its TSDB blocks to object storage (S3, GCS, Azure Blob, etc.) and serves them back through a Store API that a central Querier component fans queries out to, giving a globally deduplicated view across many Prometheus servers with effectively unlimited retention.
-
Cortex — a horizontally scalable, multi-tenant TSDB that Prometheus servers write into over
remote_write, distributing ingestion and storage across a cluster instead of relying on object-storage sidecars per server. -
Mimir — a newer, also horizontally scalable and
remote_write- compatible long-term store from Grafana Labs, aimed at very high cardinality and scale, sharing much of its lineage with Cortex.
How the pieces fit together
Mimir, Cortex or Thanos receive")] P2["Prometheus B"] -->|"remote_write"| LTS P3["Prometheus C
with Thanos sidecar"] -->|"blocks to object storage"| OBJ[("Object storage")] LTS --> QF["Query frontend
PromQL"] OBJ --> TQ["Thanos Querier"] QF --> G["Grafana"] TQ --> G P1 -.->|"remote_read or local"| G
Choosing between them is mostly operational: a sidecar keeps Prometheus as the source of truth and ships
finished blocks (cheap, but queries over recent data still go to each server), while remote_write moves
every sample to a store that owns replication, compaction and multi-tenancy at the price of an extra
network hop and a service to run. Whichever is used, keep the local retention short and the recording-rule
outputs (small, already aggregated) among the series that are shipped.
Continue with Security & Production Operations for retention and access-control concerns in production, or back to the Prometheus & Monitoring index.