HA & Federation
Redundant pairs, Alertmanager clustering, and federation
A single Prometheus server goes a long way: a well-sized one handles several million active series. It runs out in three ways: one machine isn't enough (too many series), one machine is a single point of failure (it's down during restarts and crashes), and one machine is one place (you have several clusters or regions and want one view). Each has a standard answer.
High availability: run two
Prometheus has no replication. The HA pattern is to run two identical servers with the same config, both scraping the same targets independently.
# prometheus-a # prometheus-b
global: global:
external_labels: external_labels:
cluster: prod-eu cluster: prod-eu
replica: a replica: b # the only difference- Both send alerts to the Alertmanager cluster. Alertmanager deduplicates
them (it ignores the
replicalabel if you configurealert_relabel_configsto drop it, which you should), so you get one notification. - Their data differs very slightly: they scrape at different moments. Grafana can point at either, or at a load balancer in front of both. During a restart of one, the other's data fills the gap.
- Restart them one at a time.
alerting:
alert_relabel_configs:
- action: labeldrop
regex: replica # so alerts from a and b are identical
alertmanagers:
- static_configs:
- targets: [alertmanager-1:9093, alertmanager-2:9093, alertmanager-3:9093]Two independent copies, rather than a replicated cluster, is the boring choice on purpose. There's no consensus protocol to fail, no split-brain, and no coordination traffic. Either server alone gives a complete answer, which is exactly what you want from the system that tells you when other systems are broken.
Alertmanager HA uses a gossip cluster: start each instance with
--cluster.peer= pointing at the others. Every Prometheus sends every
alert to every Alertmanager; the peers share silences and a notification
log so only one sends each notification.
Sharding: split the targets
When one server can't hold all the series, split the targets across
several servers with hashmod relabeling:
relabel_configs:
- source_labels: [__address__]
modulus: 4 # total number of shards
target_label: __tmp_hash
action: hashmod
- source_labels: [__tmp_hash]
regex: "2" # this server is shard 2 of 4
action: keepEach shard sees a quarter of the targets. The catch: no single server can answer a query across all of them. That's what federation, Thanos, and Mimir solve. In practice, most teams shard functionally first: one Prometheus per team, cluster, or region, which is natural and keeps blast radius small.
Federation
Federation lets one Prometheus scrape selected series from another,
through the /federate endpoint. The standard use is hierarchical:
a Prometheus per cluster does the detailed work, and a global Prometheus
federates only aggregated series from each.
scrape_configs:
- job_name: federate
metrics_path: /federate
honor_labels: true
params:
"match[]":
- '{__name__=~"job:.*|job_route:.*"}' # recording-rule outputs only
- 'up'
static_configs:
- targets: ["prometheus:9090"]This is why the recording rule
naming convention matters: job: series are already aggregated and
cheap to federate.
Federating raw series (match[]={__name__=~".+"}) defeats the
purpose: the global server ends up holding everything, the
/federate response gets enormous, and scrapes time out. Federation
is for aggregates. For "every series, globally" use remote write to a
scalable backend.
Lab
The scaling compose profile adds a second, "global" Prometheus on
port 9095 that federates from the main one:
docker compose -f compose_prod.yaml --profile scaling up -d prometheus-global
open http://localhost:9095- In the global server, query
job:http_requests:rate5m. It arrived by federation. - Query
http_requests_total. Nothing: raw series weren't federated. - Compare
count({__name__=~".+"})on both servers. - Look at the federation response directly:
curl -s 'localhost:9090/federate?match[]=job:http_requests:rate5m'.