Typorithm · medium

ReplicatedMergeTree

replicated-mergetreeclickhousereplicationkeeperhigh-availability
Saved on this device
Full lesson · 8 learning steps Open any exercise for focused practice
1

Step 1 of 8 · intro card

Learn ReplicatedMergeTree

0%

Scale & Ops

ENGINE = ReplicatedMergeTree('/clickhouse/tables/{shard}/events', '{replica}') replicates parts across replicas via ClickHouse Keeper (ZooKeeper) for high availability.

ReplicatedMergeTree is a MergeTree that keeps multiple synchronized copies of each part across replica nodes.

ClickHouse Keeper (or ZooKeeper) coordinates the replicas: it tracks which parts exist and which replicas have them.

The Keeper path and {replica} macro identify the table and the node, so a failed replica can rejoin and catch up automatically.

01

Replicates each part across replica nodes.

02

Keeper (ZooKeeper) coordinates the replicas.

03

Gives high availability and failover.

CREATE TABLE events
(
    event_date  Date,
    user_id     UInt64,
    value       Float64
)
ENGINE = ReplicatedMergeTree(
    '/clickhouse/tables/{shard}/events',
    '{replica}'
)
PARTITION BY toYYYYMM(event_date)
ORDER BY (user_id, event_date);

Read the implementation

Connect each part of the code to the concept before you type.

1. Replicated engine

ReplicatedMergeTree adds replication on top of the normal MergeTree storage.

ENGINE = ReplicatedMergeTree(

2. Keeper path

This Keeper path identifies the replicated table; {shard} keeps each shard's replicas in their own group.

'/clickhouse/tables/{shard}/events'

3. Replica name

The replica macro names this node so its copy is distinct from its peers in the same group.

'{replica}'

4. Same sort key

Replication does not change the MergeTree contract — ORDER BY still defines the sort and sparse index.

ORDER BY (user_id, event_date)

A complete learning loop

01

Understand

Read the concept map and follow the code breakdown.

02

Reconstruct

Type the implementation, repair mistakes and work through every recall exercise.

03

Verify

Use the final check without hints; revisit weak concepts in short practice sessions.

ReplicatedMergeTree: Lesson & Practice · Typorithm | ApplyDjinn