[Yandex Cloud documentation](../../index.md) > [Yandex Managed Service for PostgreSQL](../index.md) > FAQ > High availability and load balancing

# High availability and load balancing

* [How to remove a host from the master selection process?](#excluded-host)

* [Why do I get an error when setting up cascading replication?](#cascade-error)

* [How to ensure I am always connecting to the master host?](#connect-to-master-ha)

* [How to ensure I am always connecting to the most up-to-date replica?](#connect-to-replica-ha)

* [Why did the master and replicas swap roles?](#failover)

* [Can I configure multi-master within a single cluster or between two clusters?](#setting-multimaster)

* [How to configure load balancing so that all read requests are redirected to replicas?](#balancing-replicas)

* [When does automatic failover occur?](#auto-failover)

#### How to remove a host from the master selection process? {#excluded-host}

You can exclude a host from the master selection when replacing the master host automatically or manually. To do this, set up [cascade replication](../concepts/replication.md#replication-manual): [specify](../operations/hosts.md#update) a replication source for the host you want to exclude.

{% note warning %}

To ensure [high availability](../../architecture/fault-tolerance.md#mdb-ha) during maintenance, your cluster must have at least one replica without a replication source.

{% endnote %}

#### Why do I get an error when setting up cascading replication? {#cascade-error}

Error message:

```text
cluster should have at least 2 HA hosts to use cascade host
```

This error occurs if you specify a replication source for the only non-cascading replica.

To ensure [high availability](../../architecture/fault-tolerance.md#mdb-ha), your cluster must have at least one replica without a replication source. This replica will be promoted to master if the master host fails during maintenance.

Learn more about replication in [this section](../concepts/replication.md).

#### How to ensure I am always connecting to the master host? {#connect-to-master-ha}

To connect to the current master host, use a [special FQDN](../operations/connect/fqdn.md#special-fqdns) in the `c-<cluster_ID>.rw.mdb.yandexcloud.net` format. This FQDN supports read and write access.

{% cut "Example command for connecting to the master host" %}

  ```bash
  psql "host=c-<cluster_ID>.rw.mdb.yandexcloud.net \
        port=6432 \
        sslmode=verify-full \
        dbname=<DB_name> \
        user=<username>"
  ```

{% endcut %}

#### How to ensure I am always connecting to the most up-to-date replica? {#connect-to-replica-ha}

To connect to the most up-to-date replica, use a [special FQDN](../operations/connect/fqdn.md#special-fqdns) in the `c-<cluster_ID>.ro.mdb.yandexcloud.net` format. This FQDN only supports read access. 

{% cut "Example command for connecting to a replica" %}

```bash
psql "host=c-<cluster_ID>.ro.mdb.yandexcloud.net \
      port=6432 \
      sslmode=verify-full \
      dbname=<DB_name> \
      user=<username>"
```

{% endcut %}

If there are no active replicas in the cluster, this FQDN will point to the current master host.

#### Why did the master and replicas swap roles? {#failover}

This means the master has [failed over](../../architecture/fault-tolerance.md#mdb-ha) to the replica. Automatic master failover guarantees cluster availability during maintenance or in the event of the master host failure.

To connect to the current master host, use a [special FQDN](../operations/connect/fqdn.md#special-fqdns).

{% note warning %}

Use master host special FQDN-based connections only for processes that can cope with database being unavailable for writing for up to 10 minutes.

{% endnote %}

#### Can I configure multi-master within a single cluster or between two clusters? {#setting-multimaster}

No, Managed Service for PostgreSQL does not support multi-master configuration.

For more on how to achieve high cluster availability, see [High availability managed databases (MDB)](../../architecture/fault-tolerance.md#mdb-ha).


#### How to configure load balancing so that all read requests are redirected to replicas? {#balancing-replicas}

As Managed Service for PostgreSQL does not provide load balancing, you need to configure it in your application backend. To send read requests to replicas, your application must first identify the master and replica hosts, e.g., by using `libpq`. For more information, see [this PostgreSQL article](https://www.postgresql.org/docs/current/libpq-connect.html#LIBPQ-CONNECT-TARGET-SESSION-ATTRS).

Alternatively, use a [special FQDN](../operations/connect/fqdn.md#fqdn-replica) pointing to the most up-to-date replica.

For more information, see [High availability](../concepts/high-availability.md).

#### When does automatic failover occur? {#auto-failover}

The master host can automatically fail over to another host:

* During [cluster updates](../operations/update.md).
* On master host failure.
* Upon cluster recovery.
* During [maintenance](../concepts/high-availability.md#maintenance-settings).

Master failover ensures the cluster works correctly under the listed conditions.

{% note info %}

To ensure [high availability](../concepts/high-availability.md), the cluster always has at least one replica without an explicitly defined [replication source](../concepts/replication.md#replication-manual). This replica can take over as master when needed.

{% endnote %}