ClickHouse High Availability#
Critical Manufacturing MES supports ClickHouse running as a replicated cluster. A replicated cluster keeps a copy of the data on more than one server, so that Data Platform stays available when a server fails.
This page describes how to operate a replicated cluster after installation. For the requirements, see ClickHouse Requirements. For the setup steps, see ClickHouse Connection Setup.
Benefits of Replication#
On a replicated cluster, every server in the shard holds a full copy of the data. ClickHouse Keeper coordinates the replicas and keeps them in agreement about which parts each one holds.
Replication gives you two things:
- Availability - When a ClickHouse server fails, the remaining replicas still hold the data. With a load balancer in front of them, the connections move to a surviving replica.
- Durability across servers - A disk failure on one server does not lose the data, because another replica holds the same parts.
Warning
Replication is not a backup. A statement that drops or corrupts data runs on every replica. Continue to back up the databases as described in ClickHouse Database Backup and Restore.
Deployment Options#
Replication behaves differently in each supported deployment:
- ClickHouse in the MES stack - A cluster with one replica and an embedded Keeper. It uses
Replicated*MergeTreeengines, but has no redundant copy of the data. It requires no load balancer, and you cannot add replicas to it. Move to an external self-managed cluster to use more replicas. Internal ClickHouse is not supported in Staging or Production environments. - External self-managed ClickHouse - Replication through the
Replicated*MergeTreeengine family. It requires ClickHouse Keeper and a load balancer when the cluster has more than one replica. The conversion and add-replica procedures on this page apply to this deployment. - ClickHouse Cloud - The service manages replication internally. You do not set a cluster name, and the sections on the migration and on the load balancer do not apply.
For the requirements of each option, see ClickHouse Requirements.
How the Migration Runs#
An installation that includes the ClickHouseRunMigration step converts existing databases and tables to the configured self-managed cluster. Native restore and a change to Cluster Name do not run this step by themselves.
The conversion runs in these cases:
- On a clean installation, where it creates the objects across the cluster from the start.
- On the restore of a generated backup package, even when the restore does not change the version.
When Cluster Name is empty, the conversion does not run. The log records that the parameter is not configured and that the migration does not run. A self-managed cluster requires a cluster name, even if the server is its only replica. ClickHouse Cloud uses an empty cluster name because the service manages replication.
What the Migration Does#
The conversion works on the metadata of each object. It does not copy or rewrite the data:
- Creates the database on the cluster.
- Creates the table on the cluster, with the engine rewritten to the
Replicated*MergeTreefamily. - Detaches the existing table, then attaches it again as a replicated table.
- Restores the replica and attaches the partitions that already exist on disk.
Ordinary views need only their schema statement. A materialized view with implicit storage also has a backing table. The migration preserves the view and backing-table UUIDs, migrates the backing table first, and attaches the view to that storage. If a failed run already created the backing table on another replica, rerun the migration without deleting or recreating that table. The handler does not repair rows missed by application writes during a failed upgrade. Reconcile the expected data and verify continued view writes before resuming application writers.
After a successful run, the MES records a patch entry named High availability - Enable ClickHouse Replication against each converted database. This entry shows that the conversion already ran.
Re-Running the Migration#
If the installation fails partway through the conversion, run it again. The conversion continues from where it stopped, and it does not repeat the work it already completed.
On a second run, the conversion checks the state of each table and issues only the steps that are still outstanding:
- A table that is already replicated does not go through the detach and attach steps again.
- The conversion restores a replica only when that replica is read-only or its session expired.
- The conversion attaches the partitions only when recoverable parts remain in the detached directory. Parts whose exact detached reason is
ignoredremain on disk and do not trigger repeated attachment. - The conversion also picks up a table that a failed run left detached. A detached table no longer appears in
system.tables, so the conversion finds it insystem.detached_tables.
When a replica recovers on its own between the check and the conversion, the conversion skips the restore and records the reason in the log.
How Long the Migration Takes#
The conversion takes about one second per table, on average.
The size of a table does not change this, because the conversion changes metadata rather than copying data. The total duration depends on the number of tables in the environment, not on the volume of data in them.
Note
Count the tables in the environment to estimate the duration. A 200 GB database and a 20 GB database with the same number of tables take about the same time to convert.
Verifying a Replicated Cluster#
Run these checks after an installation or an upgrade to confirm that the data reaches every replica.
Confirm That the Cluster Is Visible#
SELECT cluster, shard_num, replica_num, host_name
FROM system.clusters
WHERE cluster = '<cluster_name>';
The result lists one row per replica. If the result is empty, the cluster name in the ClickHouse server configuration does not match the value of Cluster Name.
Confirm That the Tables Use a Replicated Engine#
SELECT database, name, engine
FROM system.tables
WHERE database LIKE '<instance>%'
AND engine LIKE '%MergeTree'
AND engine NOT LIKE 'Replicated%';
The result should be empty. Any table listed still uses a non-replicated engine, and its data does not replicate.
Confirm That the Replicas Are in Agreement#
SELECT database, table, is_readonly, absolute_delay, total_replicas, active_replicas
FROM system.replicas
WHERE active_replicas < total_replicas
OR is_readonly = 1;
The result should be empty. A row with is_readonly set to 1 means the table lost its connection to ClickHouse Keeper. A row where active_replicas is lower than total_replicas means a replica is down or is still catching up.
Confirm That the Conversion Ran#
Check the patches of each Data Platform database for the High availability - Enable ClickHouse Replication entry. When the entry is missing and the tables are not replicated, the conversion did not run, which means Cluster Name was empty during the installation.
Load Balancer#
A self-managed ClickHouse cluster with more than one replica requires a load balancer. Configure the MES connection settings to use its endpoint. The single-replica cluster in the MES stack does not require a load balancer. ClickHouse Cloud provides its own endpoint and does not require a customer-managed load balancer.
Configure failover so that another replica takes over when the replica serving a connection fails. If the load balancer distributes connections across replicas, enable sticky sessions based on the source IP address, with a session duration of at least 24 hours. Longer durations are supported. All connections from the same source IP address must reach the same replica during that period.
The Aggregation Engine uses dbt to read back the data it has just written. Sending a subsequent read to a replica that has not caught up can make the aggregation fail.
For the configuration steps, see ClickHouse Connection Setup.
Adding a Replica to an Existing Cluster#
This procedure applies only to an external self-managed cluster. You cannot add replicas to the cluster in the MES stack. To move to an external cluster, back up the source environment and restore it into a clean installation. Do not reuse the container volumes. See ClickHouse High Availability Migration ↗.
ClickHouse Keeper replicates only the objects it already knows about. It does not watch the cluster for new replicas, so a replica added to a running cluster does not start to replicate on its own. The databases, tables, and views have to exist on the new replica first. After they exist, ClickHouse Keeper recognizes that replication is needed and starts to synchronize.
To create the missing objects, explicitly execute the ClickHouseRunMigration installation step for each installed ClickHouse database through a dedicated installation package. The step propagates schemas with their existing UUIDs and replica paths, and migrates legacy access entities. Already replicated healthy tables are not converted again. Redeploying an already installed package does not guarantee that the step runs.
- Stop application writers, and preserve recovery copies of the matching ClickHouse database and Keeper state.
- Install ClickHouse on the new replica with its own replica identity and data directory. Keep it out of the load balancer.
- Add the replica to the cluster configuration on every ClickHouse server, and configure the macros. Apply any required configuration changes to ClickHouse and Keeper.
- Execute
ClickHouseRunMigrationfor each installed database. Include only the databases that exist in the environment. - Wait for the new replica to synchronize. Check the expected data, replica health, users, roles, and materialized-view writes on every replica.
- Add the validated replica to the load balancer, then resume application writers.
Warning
Keep application traffic stopped until schema creation and data validation finish. A load-balancer health check can accept a ClickHouse server that responds to requests but has no MES schemas or is still catching up.
Preserve the existing database UUIDs, replica identities, and matching Keeper state. Do not force the entire MES installation or reset its installation journal to add a replica.
The procedure needs downtime while writers are stopped and the replica is prepared. If migration is interrupted, rerun the migration step and validate the replicas before resuming writers.