Learn Labs
Scale & Ops

Replication Lab: Replicas, Quorum, Keeper and Missing Shards

What clients see when a replica, a Keeper majority or a whole shard goes away

labs/replication.py builds a ReplicatedMergeTree table on every node and a Distributed table over it, then takes pieces away.

docker compose -f compose_prod.yaml run --rm labs python replication.py

Tables ON CLUSTER, and where rows land

created on all 4 nodes in 0.53s (one DDL, queued in Keeper)
2,000,000 rows through the Distributed table in 0.2s
rows per node: ch-1 999,206, ch-2 999,206, ch-3 1,000,794, ch-4 1,000,794

The sharding key cityHash64(user_id) splits rows almost evenly, and both replicas of a shard hold the same rows. With internal_replication = true the Distributed table writes each block to one replica; replication copies it to the other.

A replica down during inserts

1,000,000 more rows with ch-2 stopped: 1.1s; ch-1 1,498,551, ch-3 1,501,449, ch-4 1,000,794
ch-2 answered queries 1.0s after start and had all 1,498,551 rows 1.0s after start

Writes didn't notice: the other replica of shard 1 took them. ch-2 read the parts it missed from Keeper's log and fetched them from ch-1.

insert_quorum: an error is not a rollback

insert_quorum = 2 makes an insert wait until two replicas have it. With ch-2 down:

rejected: Code: 319. DB::Exception: Unknown quorum status. The data was inserted in the local replica but we co...
...but rows written anyway: shard 1 (ch-1) +516, shard 2 (ch-3) +484

The client got an error, and all 1,000 rows are in the table: shard 1's half sits on ch-1 waiting for the quorum, shard 2 had both replicas and succeeded. A client that retries on this error writes the rows twice. Retries need idempotence: insert deduplication (same block, same insert_deduplication_token) or a key that a ReplacingMergeTree collapses.

Keeper loses its majority

insert, one Keeper down0.05 s, OKinsert, majority lost142.8 s, then code 242 (read-only)same, insert_keeper_max_retries = 0under 0.1 s, code 242SELECT, majority lost0.04 s, OK

With one Keeper down, nothing changed. With two down, replicated tables went read-only: SELECT kept working, inserts failed with Table is in readonly mode. Each failed insert took exactly 142.8 s to fail. That is insert_keeper_max_retries = 20 with a backoff starting at 100 ms and doubling up to 10 s: 0.1 + 0.2 + ... + 6.4 = 12.7 s, then 13 × 10 s. With insert_keeper_max_retries = 0 the same insert failed at once. Once two Keepers were back, inserts worked again within 3 to 13 s.

The first version of this lab saw Wait for async insert timeout instead, because async_insert (on by default since 26.2) hid the real error. The lab inserts with async_insert = 0.

Reading right after a failure

right after that insert: 3,016,957 rows (a lagging replica answered)
after SYSTEM SYNC REPLICA on every node: 3,022,000

Replicas are eventually consistent. Right after Keeper came back, a query reached a replica still fetching parts and counted 5,043 rows short. Every lab that compares counts runs SYSTEM SYNC REPLICA first. An application that needs read-your-writes can set select_sequential_consistency = 1, at a cost on every read.

Nodes and shards down

one replica of each shard down: 3,022,000 of 3,022,000 rows (0.01s)
shard 2 fully down: Code: 279. DB::Exception: All connection tries failed.
with skip_unavailable_shards=1: 1,508,804 of 3,022,000 rows, silently

One replica per shard is enough for complete answers. With a whole shard gone, queries fail, which is the right default. skip_unavailable_shards = 1 turns that failure into half the data and no error: never set it for anything that sums money.

On this page