[Yandex Cloud documentation](../../index.md) > [Yandex Managed Service for Valkey™](../index.md) > [Step-by-step guides](index.md) > Sharding and fault tolerance > Master failover

# Master failover

In a fault-tolerant multi-host Yandex Managed Service for Valkey™ cluster, you can perform a manual failover from the current master host to a replica host. After the failover, the current master host becomes a replica for the new master.

Master failover normally takes a few minutes, with no downtime for the cluster.

A [sharded cluster](../concepts/sharding.md) contains one or more master hosts: one per [shard](../concepts/sharding.md#valkey-cluster-structure). Master failover in a sharded cluster is performed sequentially for each shard.

{% list tabs group=instructions %}

- Management console {#console}

    To switch the master in a non-sharded cluster:

    1. In the [management console](https://console.yandex.cloud), navigate to the folder with the non-sharded cluster.
    1. Navigate to **Yandex Managed Service for&nbsp;Valkey™**.
    1. Click the name of your cluster and select the **Hosts** tab.
    1. Next to the host with the `MASTER` role, click ![image](../../_assets/console-icons/ellipsis.svg) and select **Switch master host**.
    1. In the window that opens, select **I want to switch the master** and click **Switch**.

    To switch the master in a sharded cluster:

    1. In the [management console](https://console.yandex.cloud), navigate to the folder with the sharded cluster.
    1. Navigate to **Yandex Managed Service for&nbsp;Valkey™**.
    1. Click the name of your cluster and select the **Hosts** tab.
    1. Next to the host with the `MASTER` role, click ![image](../../_assets/console-icons/ellipsis.svg) and select **Switch master host**.
    1. In the window that opens, click **Switch**.

- CLI {#cli}

    If you do not have the Yandex Cloud CLI yet, [install and initialize it](../../cli/quickstart.md#install).

    The folder used by default is the one specified when [creating](../../cli/operations/profile/profile-create.md) the CLI profile. To change the default folder, use the `yc config set folder-id <folder_ID>` command. You can also specify a different folder for any command using `--folder-name` or `--folder-id`. If you access a resource by its name, the search will be limited to the default folder. If you access a resource by its ID, the search will be global, i.e., through all folders based on access permissions.

    To switch the master in a non-sharded cluster, run this command:

    ```bash
    yc managed-redis cluster start-failover \
      --name <cluster_name>
    ```

    To switch the master in a sharded cluster, run this command:

    ```bash
    yc managed-redis cluster start-failover \
      --name <cluster_name> \
      --hostnames <current_master_name>
    ```

    You can get the cluster name with the [list of clusters in the folder](cluster-list.md), and the master name for the shard, with the [list of hosts in the cluster](hosts.md#list).

- REST API {#api}

    1. [Get an IAM token for API authentication](../api-ref/authentication.md) and put it into an environment variable:

        ```bash
        export IAM_TOKEN="<IAM_token>"
        ```

    1. Call the [Cluster.StartFailover](../api-ref/Cluster/startFailover.md) method, e.g., via the following [cURL](https://curl.se/) request:

        To switch the master in a non-sharded cluster:

        ```bash
        curl \
            --request POST \
            --header "Authorization: Bearer $IAM_TOKEN" \
            --url 'https://mdb.api.cloud.yandex.net/managed-redis/v1/clusters/<cluster_ID>:startFailover'
        ```

        To switch the master in a sharded cluster:

        ```bash
        curl \
            --request POST \
            --header "Authorization: Bearer $IAM_TOKEN" \
            --header "Content-Type: application/json" \
            --url 'https://mdb.api.cloud.yandex.net/managed-redis/v1/clusters/<cluster_ID>:startFailover' \
            --data '{
                      "hostNames": [
                        "<current_master_name>"
                      ]
                    }'
        ```

        You can get the cluster ID with the [list of clusters in the folder](cluster-list.md#list-clusters), and the name of the current master, with the [list of hosts in the cluster](hosts.md#list).

    1. Check the [server response](../api-ref/Cluster/startFailover.md#yandex.cloud.operation.Operation) to make sure your request was successful.

- gRPC API {#grpc-api}

    1. [Get an IAM token for API authentication](../api-ref/authentication.md) and put it into an environment variable:

        ```bash
        export IAM_TOKEN="<IAM_token>"
        ```

    1. Clone the [cloudapi](https://github.com/yandex-cloud/cloudapi) repository:
       
       ```bash
       cd ~/ && git clone --depth=1 https://github.com/yandex-cloud/cloudapi
       ```
       
       Below, we assume that the repository contents reside in the `~/cloudapi/` directory.

    1. Call the [ClusterService.StartFailover](../api-ref/grpc/Cluster/startFailover.md) method, e.g., via the following [gRPCurl](https://github.com/fullstorydev/grpcurl) request:

        To switch the master in a non-sharded cluster:

        ```bash
        grpcurl \
            -format json \
            -import-path ~/cloudapi/ \
            -import-path ~/cloudapi/third_party/googleapis/ \
            -proto ~/cloudapi/yandex/cloud/mdb/redis/v1/cluster_service.proto \
            -rpc-header "Authorization: Bearer $IAM_TOKEN" \
            -d '{
                  "cluster_id": "<cluster_ID>"
                }' \
            mdb.api.cloud.yandex.net:443 \
            yandex.cloud.mdb.redis.v1.ClusterService.StartFailover
        ```

        To switch the master in a sharded cluster:

        ```bash
        grpcurl \
            -format json \
            -import-path ~/cloudapi/ \
            -import-path ~/cloudapi/third_party/googleapis/ \
            -proto ~/cloudapi/yandex/cloud/mdb/redis/v1/cluster_service.proto \
            -rpc-header "Authorization: Bearer $IAM_TOKEN" \
            -d '{
                  "cluster_id": "<cluster_ID>",
                  "host_names": [
                    "<current_master_name>"
                  ]
                }' \
            mdb.api.cloud.yandex.net:443 \
            yandex.cloud.mdb.redis.v1.ClusterService.StartFailover
        ```

        You can request the cluster ID with the [list of clusters in the folder](cluster-list.md#list-clusters), and the name of the current master with the [list of hosts in the cluster](hosts.md#list).

    1. Check the [server response](../api-ref/grpc/Cluster/startFailover.md#yandex.cloud.operation.Operation) to make sure your request was successful.

{% endlist %}