How I Fixed ClickHouse Replication and Data Consistency Issues
How I found and fixed a ClickHouse replication problem that left two nodes out of sync, with the queries and steps I used to diagnose and resolve it.
The problem
I was working on an ETL pipeline that loads e-commerce tracking events into ClickHouse. The events (add to cart, purchase and so on) feed identity resolution, which means working out which events belong to the same visitor. Each record has about 15 fields. I can’t share more than that.
On our Grafana dashboard, one of the two ClickHouse instances took all the traffic while the other did almost nothing, and loads took 30 minutes, sometimes an hour. Two replicas should share the work, so the nodes were out of sync. We were running ClickHouse 26.2.1.

Looking for the cause
I tested one hypothesis at a time.
Keeper came first, since replication depends on it. It was healthy. Then I checked whether the pipeline was sending everything to one node. It was not, and the second node never caught up anyway. CPU, memory, disk and merges on the idle node all looked normal.
So I looked at the replication state directly:
SELECT hostName() AS node, database, table, zookeeper_path, is_readonly
FROM clusterAllReplicas(staging_cluster, system.replicas)
WHERE database LIKE 'reporting_shop%'
ORDER BY database, table, node;Each node had registered a different Keeper path for the same table, and ch02 was read only.
The cause: tables created without ON CLUSTER

Someone had created the tables by hand on each node, without ON CLUSTER. Each node generated its own UUID, and the engine path contains {uuid}, so each one registered its own path in Keeper. The nodes were meant to be replicas, but neither knew about the other. This is a split brain. ch02 held about 91M rows and had gone read only to protect them. ch01 was empty. 88 tables were affected.
Choosing a fix
The usual fix is to rename the tables and run INSERT INTO ... SELECT into new ones. With 91M rows across 88 tables, that means heavy disk I/O and tables that are unavailable or inconsistent while it runs. I wanted to repair the pointers in Keeper and leave the data alone.
There was a catch. ch02 was read only, so inserts were already failing, and repairing it meant running SYSTEM RESTORE REPLICA on the only node with data. I did not want to do that on a node serving production.
So I added a third node first. I created ch03 with correct ON CLUSTER tables and pointed the ETL’s inserts at it. Writes worked again right away. A view per table read old rows from ch02 and new rows from ch03, split at the cutover time, so readers always saw complete data. I backfilled ch02 into ch03 one partition at a time with a bandwidth limit, compared row counts per partition, and moved readers over temporarily. Each batch carried a deduplication token, so a retried insert could not create duplicates.
Repairing the old nodes
With ch01 and ch02 out of traffic, I could finally fix the metadata. Per table:
-- on ch01: remove the empty table and its orphaned Keeper path
DROP TABLE reporting_shop1.events SYNC;
-- on ch02: rebuild the Keeper metadata from local data (clears read only)
SYSTEM RESTORE REPLICA reporting_shop1.events;
-- on ch02: get the schema together with its UUID
SET show_table_uuid_in_table_create_query_if_not_nil = 1;
SHOW CREATE TABLE reporting_shop1.events;
-- on ch01: recreate with the same UUID, so it joins ch02's Keeper path
CREATE TABLE reporting_shop1.events UUID '<uuid from ch02>' (...) ENGINE = ...;Both nodes then pointed to the same path, and ch01 started downloading the parts in the background. I never copied the data by hand.
Eighty-eight tables is too many to type, so I scripted the four steps and wrapped the script in an Ansible playbook, with the password in Ansible Vault. DROP destroys whatever is on ch01, so the script first checks that the table there is empty and that its Keeper path differs from ch02’s. I also capped max_replicated_fetches_network_bandwidth and max_replicated_sends_network_bandwidth, because ch02 was the only source for the download and I wanted it to stay responsive.
One thing to watch. A node that is still downloading parts returns partial results with no error, so I kept ch01 out of read traffic until its replication queue was empty.
SYSTEM RESTORE REPLICA worked on 26.2.1. I did not check which release introduced it, so read the changelog before trying this on an older version.
Checking the result
ch01 went from 0 rows to about 91.5M, matching ch02, and both nodes showed up as active on the dashboard. Row counts alone are weak, so I also ran this, which must return nothing:
SELECT hostName() AS node, database, table, is_readonly, queue_size, absolute_delay, lost_part_count
FROM clusterAllReplicas(staging_cluster, system.replicas)
WHERE database LIKE 'reporting_shop%'
AND (is_readonly = 1 OR queue_size > 0 OR absolute_delay > 5 OR lost_part_count > 0);Retiring ch03
Once ch01 and ch02 were healthy replicas with the same data, ch03 had done its job. Everything written to it since the cutover still had to go back. Because the repaired pair now shared one Keeper path, I only had to insert those rows on one node, and replication copied them to the other. I selected the rows newer than the cutover time from ch03 and inserted them into ch02, with a deduplication token per partition so a rerun could not duplicate anything.

After comparing counts again, I pointed the ETL’s inserts and the readers back at the repaired pair, shut ch03 down, and removed its cluster config. From then on, the two replicas shared the traffic, as they should have from the start.
What I’d do differently
Create replicated tables with ON CLUSTER every time, and check system.replicas for is_readonly = 1 after any manual change. If one node looks idle on a dashboard, compare table UUIDs before tuning anything else.
Comments