> ## Documentation Index
> Fetch the complete documentation index at: https://clickhouse.com/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# How to throttle replica recovery

> A recovering replica downloads all parts from other replicas without a bandwidth limit. Learn which settings limit the network speed and the number of parallel fetches.

A new or restored replica of a `ReplicatedMergeTree` table in self-managed ClickHouse doesn't repeat the inserts and merges of the past. It downloads (fetches) the existing data parts from another replica. By default the fetches are not limited in bandwidth and up to 16 of them run in parallel, which can saturate the network or the disks of the replicas involved. To throttle the recovery, limit the fetch speed on the recovering replica, the send speed on the source replicas, or the number of parallel fetches.

## What a recovering replica does

When a replica is created empty or restored after data loss (see [Recovery after failures](https://clickhouse.com/docs/reference/engines/table-engines/mergetree-family/replication.md#recovery-after-failures)), it puts one `GET_PART` entry for every part of a source replica into its replication queue. Check the queue on the recovering server:

```sql theme={null}
SELECT
    type,
    count() AS entries,
    countIf(is_currently_executing) AS executing
FROM system.replication_queue
GROUP BY type
ORDER BY entries DESC;
```

During recovery almost all entries are of the type `GET_PART`. A small number of `MERGE_PARTS` or `MUTATE_PART` entries can appear as well.

## Settings that throttle the fetches

| Setting | Where to set it | What it limits | Default |
| - | - | - | - |
| [`max_replicated_fetches_network_bandwidth_for_server`](https://clickhouse.com/docs/reference/settings/server-settings/settings/max-replicated#max_replicated_fetches_network_bandwidth_for_server) | Server configuration of the recovering replica | Speed of all fetches of the server, in bytes per second | 0 (unlimited) |
| [`max_replicated_sends_network_bandwidth_for_server`](https://clickhouse.com/docs/reference/settings/server-settings/settings/max-replicated#max_replicated_sends_network_bandwidth_for_server) | Server configuration of the source replicas | Speed of all parts sent to other replicas, in bytes per second | 0 (unlimited) |
| [`background_fetches_pool_size`](https://clickhouse.com/docs/reference/settings/server-settings/settings/background#background_fetches_pool_size) | Server configuration of the recovering replica | Number of fetches that run at the same time | 16 |
| [`max_replicated_fetches_network_bandwidth`](https://clickhouse.com/docs/reference/settings/merge-tree-settings/max-replicated#max_replicated_fetches_network_bandwidth) | `MergeTree` setting of a table | Fetch speed of one table, in bytes per second | 0 (unlimited) |
| [`max_replicated_sends_network_bandwidth`](https://clickhouse.com/docs/reference/settings/merge-tree-settings/max-replicated#max_replicated_sends_network_bandwidth) | `MergeTree` setting of a table | Send speed of one table, in bytes per second | 0 (unlimited) |

The server settings go to the top level of the server configuration, for example into a file in `config.d`. This example limits the fetches of the server to 50 MiB per second and to four at a time:

```xml theme={null}
<clickhouse>
    <max_replicated_fetches_network_bandwidth_for_server>52428800</max_replicated_fetches_network_bandwidth_for_server>
    <background_fetches_pool_size>4</background_fetches_pool_size>
</clickhouse>
```

The server reads the two bandwidth settings at startup, and `background_fetches_pool_size` can't be lowered while the server runs. Put the limits in place before you start the server that is going to recover, or restart it.

The table-level settings can be given in the `SETTINGS` clause of `CREATE TABLE`, changed with `ALTER TABLE`, or set for all tables in the [`merge_tree`](https://clickhouse.com/docs/reference/settings/server-settings/settings/merge#merge_tree) section of the server configuration:

```sql theme={null}
ALTER TABLE db.events MODIFY SETTING max_replicated_fetches_network_bandwidth = 10485760;
```

```xml theme={null}
<clickhouse>
    <merge_tree>
        <max_replicated_fetches_network_bandwidth>10485760</max_replicated_fetches_network_bandwidth>
    </merge_tree>
</clickhouse>
```

A table reads its limit when it is loaded, so a changed value applies after the next server restart. When both a table limit and a server limit are set, a fetch is slowed down by both. The limits are not followed with perfect accuracy.

## Pause the fetches without a restart

To stop the fetches for a while, for example during peak hours, use [`SYSTEM STOP FETCHES`](https://clickhouse.com/docs/reference/statements/system#stop-fetches) on the recovering replica, for one table or for all tables. The queue entries are kept and are executed after `SYSTEM START FETCHES`:

```sql theme={null}
SYSTEM STOP FETCHES db.events;
SYSTEM START FETCHES db.events;
```

## Monitor the recovery

The fetches that are running now are listed in [`system.replicated_fetches`](https://clickhouse.com/docs/reference/system-tables/replicated_fetches):

```sql theme={null}
SELECT
    database,
    `table`,
    result_part_name,
    round(progress * 100, 1) AS progress_percent,
    formatReadableSize(total_size_bytes_compressed) AS part_size,
    source_replica_hostname,
    round(elapsed) AS elapsed_seconds
FROM system.replicated_fetches
ORDER BY elapsed DESC;
```

Entries that wait because of a limit show the reason in the `postpone_reason` column of [`system.replication_queue`](https://clickhouse.com/docs/reference/system-tables/replication_queue), for example:

```text theme={null}
Not executing fetch of part all_0_0_0 because 4 fetches already executing, max 4.
Not executing fetch of part all_0_0_0 because fetches have already throttled by network settings <max_replicated_fetches_network_bandwidth> or <max_replicated_fetches_network_bandwidth_for_server>.
```

The recovery is finished when the queue is empty:

```sql theme={null}
SELECT
    database,
    `table`,
    queue_size,
    inserts_in_queue,
    absolute_delay
FROM system.replicas
ORDER BY queue_size DESC
LIMIT 10;
```

The fetch pool and the server-wide limits also apply to the regular replication of newly inserted parts. Remove the limits when the replica has caught up, or keep them high enough for the normal insert rate.

## Related pages

* [Data replication](https://clickhouse.com/docs/reference/engines/table-engines/mergetree-family/replication.md)
* [How to restore a replica after storage failure](https://clickhouse.com/docs/resources/support-center/knowledge-base/troubleshooting/restore-replica-after-storage-failure.md)
* [`system.replication_queue`](https://clickhouse.com/docs/reference/system-tables/replication_queue)
