Overview
The Replicated*MergeTree engines keep multiple copies of a table in sync. Replicas coordinate through ClickHouse Keeper (a ZooKeeper-compatible service built on Raft) which stores a replication log, the list of parts and the insert-deduplication hashes. Data itself is not sent through Keeper: replicas fetch parts directly from each other.
Core concepts
- Table definition:
ENGINE = ReplicatedMergeTree('/clickhouse/tables/{shard}/db.table', '{replica}'). Replicas with the same Keeper path form one replica set; each needs a unique replica name. Use macros ({shard},{replica}) from the server config. - Multi-master: you can insert into any replica. Each insert is logged in Keeper; others fetch the part. Merges are decided by a leader replica and replayed elsewhere, so replicas end with identical parts.
- Insert deduplication: an identical block inserted again (client retry) is ignored by hash within a window.
- Quorum inserts (
insert_quorum) andselect_sequential_consistencyprovide stronger consistency at a cost; default replication is asynchronous. - Keeper needs an odd number of nodes (3 or 5) on separate hosts; losing quorum makes replicated tables read-only. It can run embedded (testing) or as a separate
clickhouse-keeperservice. - Operations:
SYSTEM SYNC REPLICA,SYSTEM RESTART REPLICA,SYSTEM RESTORE REPLICA(after Keeper metadata loss),system.replicas,system.replication_queue. Kubernetes operators (Altinity ClickHouse Operator, ClickHouse Operator) automate shards and replicas. - ClickHouse Cloud uses
SharedMergeTreeover object storage instead; this is not part of the open-source image.
Worked example
Executed on the test server using its embedded Keeper, with two replicas of one table living on the same server (a mechanics demo, not a real HA setup):
CREATE TABLE rep_a (id UInt32, v String) ENGINE = ReplicatedMergeTree('/clickhouse/tables/kb/rep', 'replica_a') ORDER BY id;
CREATE TABLE rep_b (id UInt32, v String) ENGINE = ReplicatedMergeTree('/clickhouse/tables/kb/rep', 'replica_b') ORDER BY id;
INSERT INTO rep_a VALUES (1,'one'),(2,'two');
SYSTEM SYNC REPLICA rep_b; -- rep_b now has 2 rows
INSERT INTO rep_a VALUES (1,'one'),(2,'two'); -- identical block: ignored, still 2 rowssystem.replicas showed both replicas with is_leader = 1, total_replicas = 2, active_replicas = 2, queue_size = 0. The Keeper path contained log, blocks, block_numbers, leader_election, mutations, replicas (replica_a, replica_b) and more. Then SYSTEM STOP FETCHES rep_b plus an insert into rep_a left rep_b at 2 rows while rep_a had 3; after SYSTEM START FETCHES and SYNC REPLICA it caught up to 3.
Pitfalls and production notes
- Run 3 Keeper nodes on dedicated or at least separate hosts with fast disks; Keeper latency affects insert latency.
- Replication protects against node loss, not against logical errors (a
DROP TABLEreplicates). Keep backups (ch-backup-restore). - Monitor
absolute_delay,queue_sizeandinserts_in_queue(ch-operations-monitoring). - Do not mix up shards and replicas: replicas hold the same data, shards hold different data (ch-sharding-distributed).
- Compare with PostgreSQL streaming replication (pg-replication-failover); ClickHouse replicates parts, not a WAL.
Practice questions
- What does Keeper store, and what does it not store?
- Why do three Keeper nodes tolerate one failure but two tolerate none?
- How do replicas get identical results from independent merges?
References
- ClickHouse documentation, Data replication; ClickHouse Keeper
Related
ch-sharding-distributed · ch-backup-restore · ch-operations-monitoring · pg-replication-failover