---
title: "Alloy clustering | Database Observability documentation"
description: "Run Grafana Alloy in clustering mode so several Alloy instances share the databases you monitor, with one owner for each database."
---

> For a curated documentation index, see [llms.txt](/llms.txt). For the complete documentation index, see [llms-full.txt](/llms-full.txt).

# Alloy clustering

Grafana Alloy clustering lets several Alloy instances share the databases you monitor with Database Observability. Each database has one owner at a time. The owner connects to the database and collects its telemetry. When an instance leaves the cluster, the remaining instances take over its databases.

Use clustering to spread many databases across Alloy instances, or to keep collecting from a database when an instance fails. This guide shows you how to define several databases in one component, enable clustering in the component, and set the Alloy cluster flags.

This guide requires Alloy 1.22.0 or newer. The minimum cluster size guidance describes Alloy 1.22.0, and cluster behavior can change in later releases. To check for changes, refer to the [Alloy release notes](https://github.com/grafana/alloy/releases).

## What you’ll achieve

In this guide, you:

- Monitor several databases with one `database_observability.*` component.
- Run two or more Alloy instances that share those databases.
- Set the cluster flags and choose a safe minimum cluster size.
- Verify that each database has one owner.

## Before you begin

You need the following:

- **Alloy 1.22.0 or newer on every instance:** To find the latest stable version, refer to [Docker Hub](https://hub.docker.com/r/grafana/alloy/tags). To update, refer to the [Alloy release notes](https://github.com/grafana/alloy/releases).
- **Two or more Alloy instances:** The instances must reach each other. Cluster peers communicate over the Alloy HTTP server, so each instance must accept connections on its HTTP listen address.
- **Network access from every instance to every database:** Ownership moves between instances, so any instance can end up collecting from any database.
- **Credentials for every database on every instance:** For the same reason, every instance holds the credentials for every database. Use the monitoring user from the setup guide for each database, because it has only the privileges that Database Observability needs.
- **Prepared databases:** Each database needs the monitoring user and settings that Database Observability requires. For setup steps, refer to [Set up MySQL](https://grafana.com/docs/grafana-cloud/monitor-applications/database-observability/set-up/mysql/), [Set up PostgreSQL](https://grafana.com/docs/grafana-cloud/monitor-applications/database-observability/set-up/postgres/), or [Set up SQL Server](https://grafana.com/docs/grafana-cloud/monitor-applications/database-observability/set-up/sqlserver/).
- **Grafana Cloud credentials:** You need remote write credentials for Prometheus and Loki.

## Understand how clustering distributes databases

When you enable clustering for a `database_observability.*` component, Alloy divides the databases that the component monitors between the cluster instances. Each `database_instance` block is one unit of distribution.

For each `database_instance` block, Alloy builds a key from the server address and database name in the data source name. It applies consistent hashing to that key to choose one owner in the cluster. Only the owner connects to the database, runs the collectors, and exports the scrape targets for the database. The other instances export no targets for it, so a `prometheus.scrape` component on each instance scrapes only the databases that instance owns.

Keep the following behavior in mind:

- **Identical configuration:** Clustering assumes that every instance runs the same configuration file.
- **Ownership moves:** When an instance joins or leaves the cluster, every instance recalculates ownership. Expect a short gap or a brief duplicate collection for a database while its ownership moves. Size the connection limit of each database for the worst case, because more than one instance can briefly hold connections to the same database.
- **Failover isn’t instant:** After an instance stops, the cluster must detect it, and the new owner must connect and run its first collection. Expect a gap in telemetry for the affected databases.
- **Ownership doesn’t follow database health:** Ownership depends only on cluster membership. If the owner can’t reach a database, the database doesn’t move to another instance.
- **Cluster readiness:** While the cluster isn’t ready, for example, while it waits for the minimum cluster size, the component doesn’t collect from any database.
- **A single database:** Clustering also works with one database. When several instances run the same configuration, one instance collects from the database at a time.

For the full behavior, refer to the `clustering` block in the reference documentation for [`database_observability.mysql`](https://grafana.com/docs/grafana-cloud/send-data/alloy/reference/components/database_observability/database_observability.mysql/#clustering), [`database_observability.postgres`](https://grafana.com/docs/grafana-cloud/send-data/alloy/reference/components/database_observability/database_observability.postgres/#clustering), and [`database_observability.sql_server`](https://grafana.com/docs/grafana-cloud/send-data/alloy/reference/components/database_observability/database_observability.sql_server/#clustering). For background on how Alloy clusters work, refer to [Clustering](https://grafana.com/docs/grafana-cloud/send-data/alloy/get-started/clustering/).

## Define the databases to monitor

Add one `database_instance` block to the component for each database. The block label identifies the database and must be unique in the component. Each block must point to a different database. Alloy rejects two blocks that resolve to the same server and database name.

The **Configuration** page generates one component for each database, and it doesn’t generate `database_instance` or `clustering` blocks. To run a cluster, write the Alloy configuration yourself, as in this section.

When you use `database_instance` blocks, don’t set the top-level `data_source_name`, `targets`, or `cloud_provider` arguments. The component runs the exporter for each database itself, so you don’t add a `prometheus.exporter.*` component. It serves the metrics for each database on a path that includes the block label, and the exported targets already point to those paths.

The following configuration monitors two PostgreSQL databases with one component and opts the component in to clustering:

Alloy ![Copy code to clipboard](https://grafana.com/media/images/icons/icon-copy-small-2.svg) Copy

```alloy
database_observability.postgres "pool" {
  forward_to = [loki.write.grafana_cloud_loki.receiver]

  database_instance "orders" {
    data_source_name = sys.env("ORDERS_DSN")

    rule {
      source_labels = ["instance"]
      target_label  = "dsn"
    }

    rule {
      target_label = "instance"
      replacement  = "orders-db"
    }
  }

  database_instance "billing" {
    data_source_name = sys.env("BILLING_DSN")

    rule {
      source_labels = ["instance"]
      target_label  = "dsn"
    }

    rule {
      target_label = "instance"
      replacement  = "billing-db"
    }
  }

  clustering {
    enabled = true
  }
}

prometheus.scrape "pool" {
  targets    = database_observability.postgres.pool.targets
  forward_to = [prometheus.remote_write.metrics_service.receiver]
}
```

Set the `ORDERS_DSN` and `BILLING_DSN` environment variables to the data source name of each database, on every instance. For the data source name format, refer to the setup guide for your database. Restrict access to wherever you store these values, because they contain credentials. Every instance must use the same data source name host for a database, so that every instance identifies the database in the same way.

The `rule` blocks set the labels on the metrics and logs of one database. The first rule keeps the original `instance` value in a `dsn` label, as the setup guides do. The second rule sets `instance` to a name you choose for the database, such as `orders-db`. Use a name that’s easy to recognize and unique for each database, and keep it the same on every instance. For the labels that Database Observability requires, refer to the [Labels reference](https://grafana.com/docs/grafana-cloud/monitor-applications/database-observability/reference/labels/). To learn more about the `rule` block, refer to the [`database_observability.postgres` reference](https://grafana.com/docs/grafana-cloud/send-data/alloy/reference/components/database_observability/database_observability.postgres/#rule).

Because every instance runs the same configuration, every instance sets the same labels. Don’t add labels that differ between instances, for example, the Pod name. Ownership moves between instances, and a different label value starts a new series.

Don’t add a `clustering` block to the `prometheus.scrape` component that scrapes these targets. The component exports only the targets of the databases that its instance owns, so the targets are already distributed.

### Apply settings to all or one database

Collector settings and exclusions that you set on the component apply to every database in it. The exception is the `cloud_provider` block, which you place inside each `database_instance` block. To use different collector settings for some databases, define a separate component for them, and give it its own `clustering` block.

### Use the same blocks for MySQL and SQL Server

The `database_instance` and `clustering` blocks are the same for every database engine. For MySQL, use the `database_observability.mysql` component, and set the `instance` and `dsn` labels with `rule` blocks in the same way as in the PostgreSQL example:

Alloy ![Copy code to clipboard](https://grafana.com/media/images/icons/icon-copy-small-2.svg) Copy

```alloy
database_observability.mysql "pool" {
  forward_to = [loki.write.grafana_cloud_loki.receiver]

  database_instance "inventory" {
    data_source_name = sys.env("INVENTORY_DSN")

    rule {
      source_labels = ["instance"]
      target_label  = "dsn"
    }

    rule {
      target_label = "instance"
      replacement  = "inventory-db"
    }
  }

  database_instance "shipping" {
    data_source_name = sys.env("SHIPPING_DSN")

    rule {
      source_labels = ["instance"]
      target_label  = "dsn"
    }

    rule {
      target_label = "instance"
      replacement  = "shipping-db"
    }
  }

  clustering {
    enabled = true
  }
}

prometheus.scrape "mysql_pool" {
  targets    = database_observability.mysql.pool.targets
  forward_to = [prometheus.remote_write.metrics_service.receiver]
}
```

For SQL Server, use the `database_observability.sql_server` component in the same way.

Each component opts in to clustering on its own, so add a `clustering` block to every `database_observability.*` component that you want to distribute. Each component also exports its own targets. To scrape the targets of more than one component with one `prometheus.scrape` component, combine them:

Alloy ![Copy code to clipboard](https://grafana.com/media/images/icons/icon-copy-small-2.svg) Copy

```alloy
prometheus.scrape "pool" {
  targets = array.concat(
    database_observability.postgres.pool.targets,
    database_observability.mysql.pool.targets,
  )
  forward_to = [prometheus.remote_write.metrics_service.receiver]
}
```

### Forward PostgreSQL logs

With `database_instance` blocks, each PostgreSQL database has its own logs receiver, keyed by the block label. Forward the logs of each database to its receiver:

Alloy ![Copy code to clipboard](https://grafana.com/media/images/icons/icon-copy-small-2.svg) Copy

```alloy
loki.source.file "orders_logs" {
  targets = [{
    __path__ = "/var/log/postgresql/postgresql-*.log",
    job      = "postgres_logs",
  }]

  forward_to = [database_observability.postgres.pool.logs_receivers["orders"]]
}
```

For the `log_line_prefix` setting and the `logs` block that turn on error processing, refer to the [PostgreSQL setup guide](https://grafana.com/docs/grafana-cloud/monitor-applications/database-observability/set-up/postgres/postgres/#add-processing-of-postgresql-logs-optional).

Every instance exports the logs receivers for all configured PostgreSQL databases. An instance that doesn’t own a database discards the entries sent to that database’s receiver. Every instance must therefore receive each database’s logs. This works when every instance can read the logs, for example, from shared storage. If a source pushes logs through a load balancer, the entries that reach an instance that doesn’t own the database are discarded.

### Add the write configuration

The configuration in this guide sends metrics and logs to Grafana Cloud with the `metrics_service` and `grafana_cloud_loki` components.

Add the Prometheus remote write and Loki write configuration. From Grafana Cloud, open your stack to get the URLs and generate API tokens:

Alloy ![Copy code to clipboard](https://grafana.com/media/images/icons/icon-copy-small-2.svg) Copy

```alloy
prometheus.remote_write "metrics_service" {
  endpoint {
    url = sys.env("GCLOUD_HOSTED_METRICS_URL")

    basic_auth {
      password = sys.env("GCLOUD_RW_API_KEY")
      username = sys.env("GCLOUD_HOSTED_METRICS_ID")
    }
  }
}

loki.write "grafana_cloud_loki" {
  endpoint {
    url = sys.env("GCLOUD_HOSTED_LOGS_URL")

    basic_auth {
      password = sys.env("GCLOUD_RW_API_KEY")
      username = sys.env("GCLOUD_HOSTED_LOGS_ID")
    }
  }
}
```

Replace the placeholders:

- `GCLOUD_HOSTED_METRICS_URL`: Your Grafana Cloud Prometheus remote write URL.
- `GCLOUD_HOSTED_METRICS_ID`: Your Grafana Cloud Prometheus instance ID (username).
- `GCLOUD_HOSTED_LOGS_URL`: Your Grafana Cloud Loki write URL.
- `GCLOUD_HOSTED_LOGS_ID`: Your Grafana Cloud Loki instance ID (username).
- `GCLOUD_RW_API_KEY`: Grafana Cloud API token with write permissions.

## Set the Alloy clustering flags

Clustering needs two settings. The `clustering` block opts a component in, and the Alloy `--cluster.*` flags create the cluster. If Alloy isn’t running in clustered mode, the `clustering` block does nothing and every instance collects from every configured database.

> Caution
> 
> If you don’t set `--cluster.enabled` on every instance, each instance collects from every database. Your databases then handle the connections and queries of every instance.

Set the following flags on every Alloy instance:

Expand table

| Flag                          | Purpose                                                                   | Guidance                                                                                                                                                                                                                                                                                                                    |
|-------------------------------|---------------------------------------------------------------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `--cluster.enabled`           | Starts Alloy in clustering mode.                                          | Set it to `true` on every instance.                                                                                                                                                                                                                                                                                         |
| `--cluster.join-addresses`    | Lists the peers to join.                                                  | Use a comma-separated list of instance addresses. If you omit the port, Alloy uses the HTTP listen port. To discover peers dynamically, use `--cluster.discover-peers` instead. You can’t set both flags.                                                                                                                   |
| `--server.http.listen-addr`   | Sets the address of the Alloy HTTP server, which carries cluster traffic. | The default, `127.0.0.1:12345`, accepts connections only from the same host. Set an address that the other instances can reach. Refer to [Secure cluster traffic](https://grafana.com/docs/grafana-cloud/observe-and-act/monitor-infrastructure/database-observability/configure/alloy-clustering/#secure-cluster-traffic). |
| `--cluster.advertise-address` | Sets the address that other instances use to reach this instance.         | Set it when your network interface isn’t `eth0` or `en0`. Otherwise Alloy can advertise `127.0.0.1`, which other instances can’t reach.                                                                                                                                                                                     |
| `--cluster.wait-for-size`     | Sets the minimum cluster size.                                            | Set it on every instance. Refer to [Choose the minimum cluster size](https://grafana.com/docs/grafana-cloud/observe-and-act/monitor-infrastructure/database-observability/configure/alloy-clustering/#choose-the-minimum-cluster-size).                                                                                     |
| `--cluster.wait-timeout`      | Sets how long to wait for the minimum cluster size.                       | Set it on every instance. Refer to [Choose the minimum cluster size](https://grafana.com/docs/grafana-cloud/observe-and-act/monitor-infrastructure/database-observability/configure/alloy-clustering/#choose-the-minimum-cluster-size).                                                                                     |
| `--cluster.name`              | Names the cluster.                                                        | Set a different name for each cluster when several clusters share a network. Instances only join peers that use the same name.                                                                                                                                                                                              |
| `--cluster.node-name`         | Names the instance.                                                       | The default is the hostname. Make sure every instance has a distinct name, for example, when you clone virtual machines.                                                                                                                                                                                                    |

If you don’t set `--cluster.advertise-address`, Alloy infers an address from `--cluster.advertise-interfaces`, which defaults to `eth0,en0`. If Alloy can’t find an address on those interfaces, it logs a warning and advertises `127.0.0.1`.

For all cluster flags, including the TLS options, refer to the [`alloy run` reference](https://grafana.com/docs/grafana-cloud/send-data/alloy/reference/cli/run/#usage). For the join address format, refer to [Join address format](https://grafana.com/docs/grafana-cloud/send-data/alloy/reference/cli/run/#join-address-format). For peer discovery details, refer to [Configure Alloy for clustering](https://grafana.com/docs/grafana-cloud/send-data/alloy/configure/clustering/configure-alloy/).

### Secure cluster traffic

Cluster peers communicate over the Alloy HTTP server, and the traffic isn’t encrypted unless you enable TLS. The same server serves the Alloy UI, the API, and the `/debug/pprof` profiling endpoints, which are on by default. If you bind the server to `0.0.0.0`, all of them are reachable on every network interface of the host.

> Caution
> 
> Allow traffic to the HTTP port only from other Alloy instances, for example, with a security group, a firewall rule, or a Kubernetes NetworkPolicy. Bind the server to a private interface address where you can, and never expose the port publicly. On networks that you don’t fully trust, enable TLS for cluster traffic with `--cluster.enable-tls` and the `--cluster.tls-*` flags.

### Choose the minimum cluster size

Set `--cluster.wait-for-size` and `--cluster.wait-timeout` on every instance. Until the cluster reaches the minimum size, the component doesn’t collect from any database.

> Caution
> 
> Don’t leave `--cluster.wait-for-size` at the default of `0`. With no minimum size, every instance can connect to every configured database while a new cluster forms. Your databases then handle the connections and queries of every instance.

Alloy 1.22.0 applies the minimum size requirement for as long as an instance runs, not only at startup. When the number of instances in the cluster drops below the minimum size, for example, during a node drain or after a failure, the remaining instances stop collecting from every database. They resume when the cluster grows back to the minimum size, or when `--cluster.wait-timeout` passes again.

Choose a minimum that the cluster stays at or above during the disruptions you expect:

- **Three instances:** Set the minimum to `2`. The cluster tolerates one unavailable instance. If a second instance becomes unavailable at the same time, the remaining instance stops collecting until the cluster recovers or the timeout passes.
- **Two instances, minimum of `2`:** Instances don’t duplicate collection at startup. When one instance stops, the other stops collecting until the timeout passes. After that, it collects from every database.
- **Two instances, minimum of `1`:** When one instance stops, the other keeps collecting. At startup, both instances can briefly collect from the same databases.

The timeout starts when Alloy starts, and it restarts each time the cluster drops below the minimum. If too few instances join, no database is collected for up to the whole timeout. For example, with a timeout of `10m`, a cold start where too few instances join gives you up to 10 minutes without telemetry. Choose a timeout that gives your environment enough time to start the minimum number of instances, and that you accept as a gap in telemetry.

When the timeout passes, an instance collects with the cluster it has. An instance that couldn’t join its peers then collects every database, and duplicates the other instances. An instance that couldn’t join logs `failed to connect to peers; bootstrapping a new cluster`. To detect instances that aren’t ready, alert on the `cluster_ready_for_traffic` metric from Alloy when it’s `0`.

### Run Alloy on hosts or virtual machines

To run a three-instance cluster on hosts or virtual machines, start Alloy on each instance with the same configuration file and flags. Only the instance address differs. Set the `ORDERS_DSN` and `BILLING_DSN` environment variables for the Alloy process:

sh ![Copy code to clipboard](https://grafana.com/media/images/icons/icon-copy-small-2.svg) Copy

```sh
alloy run /etc/alloy/config.alloy \
  --server.http.listen-addr=<INSTANCE_ADDRESS>:12345 \
  --cluster.enabled=true \
  --cluster.join-addresses=<ALLOY_HOST_1>,<ALLOY_HOST_2>,<ALLOY_HOST_3> \
  --cluster.wait-for-size=2 \
  --cluster.wait-timeout=10m
```

Replace the following:

- *`<INSTANCE_ADDRESS>`* : The private address of the instance that runs the command.
- *`<ALLOY_HOST_1>`* , *`<ALLOY_HOST_2>`* , and *`<ALLOY_HOST_3>`* : The addresses of your Alloy instances.

### Run Alloy on Kubernetes

To run Alloy on Kubernetes, deploy it with the Alloy Helm chart and enable clustering. For the steps, refer to [Configure Alloy for clustering](https://grafana.com/docs/grafana-cloud/send-data/alloy/configure/clustering/configure-alloy/#configure-clustering-with-a-helm-chart). Use the flag values from [Set the Alloy clustering flags](https://grafana.com/docs/grafana-cloud/observe-and-act/monitor-infrastructure/database-observability/configure/alloy-clustering/#set-the-alloy-clustering-flags), including the minimum cluster size, and give every instance the data source names as the environment variables that your configuration reads.

## Change the configuration or credentials

Every instance must run the same configuration, so roll out changes to all instances quickly. When an instance loads a new configuration, it reconnects to the databases it owns. During a rollout, instances on the old and new configuration can disagree about the databases. For example, a database that exists only in the new configuration can be assigned to an instance that still runs the old one, and then no instance collects it.

Changing the host or port in a data source name changes the ownership key of that database, so the database can move to another instance. When you rotate credentials, keep both the old and new credentials valid until every instance runs the new configuration. If you scale the cluster below the minimum size, the behavior described in [Choose the minimum cluster size](https://grafana.com/docs/grafana-cloud/observe-and-act/monitor-infrastructure/database-observability/configure/alloy-clustering/#choose-the-minimum-cluster-size) applies.

## Verify the cluster

After you start the instances, verify that they formed one cluster and that each database has one owner:

1. Open the Alloy UI on any instance.
2. Click **Clustering** in the navigation bar, and confirm that every instance appears in the table.
3. On each instance, open the `database_observability` component in the Alloy UI, and review its exported `targets`. Each database appears on one instance only. An instance that owns no databases reports a healthy status with the message `clustering is enabled and no databases are currently owned by this node`. That’s expected for a spare instance.
4. In Database Observability, open **Configuration** and confirm that each database reports telemetry. For details, refer to [Verify telemetry status](https://grafana.com/docs/grafana-cloud/monitor-applications/database-observability/configure/verify-telemetry-status/).

To test failover, stop one instance. The remaining instances recalculate ownership and take over its databases. Expect a gap in telemetry for those databases while they move.

## Troubleshoot clustering

If databases are collected more than once, or not at all, check the following:

- **Every instance collects from every database:** Confirm that `--cluster.enabled` is set on every instance and that every component has a `clustering` block with `enabled = true`. Confirm that the instances joined one cluster on the **Clustering** page. An instance that runs alone owns every database once it’s ready, which is immediately without `--cluster.wait-for-size`, or after `--cluster.wait-timeout`.
- **An instance doesn’t join the cluster:** Look for the `failed to connect to peers; bootstrapping a new cluster` log line. Check the join addresses, firewall rules for the HTTP port, that every instance uses the same `--cluster.name`, and that the TLS settings match.
- **An instance advertises localhost:** Look for the `falling back to localhost` log line. Set `--cluster.advertise-address`.
- **No instance collects from a database:** Look for the `cluster is not ready to admit traffic, not collecting from any database` log line, and check whether the `cluster_ready_for_traffic` metric is `0`. The cluster is smaller than `--cluster.wait-for-size`. A database that’s missing from the configuration of the instance that owns it is also never collected.
- **Different results between instances:** Confirm that every instance runs the same configuration file.
- **Gaps or duplicates after a change:** Brief gaps and duplicates are expected while ownership moves after an instance joins or leaves.

For more help, refer to [Troubleshoot Alloy components](https://grafana.com/docs/grafana-cloud/monitor-applications/database-observability/troubleshoot/alloy/) and [Debug clustering issues](https://grafana.com/docs/grafana-cloud/send-data/alloy/troubleshoot/debug/#debug-clustering-issues).

## Next steps

- [Monitor multiple databases](https://grafana.com/docs/grafana-cloud/monitor-applications/database-observability/configure/monitor-multiple-databases/)
- [Tune Alloy collection](https://grafana.com/docs/grafana-cloud/monitor-applications/database-observability/configure/tune-alloy-collection/)
- [Verify telemetry status](https://grafana.com/docs/grafana-cloud/monitor-applications/database-observability/configure/verify-telemetry-status/)
- [Clustering in Alloy](https://grafana.com/docs/grafana-cloud/send-data/alloy/get-started/clustering/)
- [Distribute workload across Alloy instances](https://grafana.com/docs/grafana-cloud/send-data/alloy/configure/clustering/distribute-workload/)
