Overview
Sharding splits a table’s rows across servers to scale storage and compute; each shard usually has replicas (ch-replication-keeper). The Distributed engine is a routing layer: it stores no data, fans queries out to every shard, merges the partial results, and splits inserts by a sharding key.
Core concepts
- A cluster is declared in
remote_serversconfig: shards, each with one or more replicas. ENGINE = Distributed(cluster, database, local_table, sharding_key). Reads query one replica per shard in parallel and merge; the initiator node does the final merge.- Inserts into the Distributed table are routed by
sharding_key(for examplecityHash64(user_id)), asynchronously by default;distributed_foreground_insert = 1makes them synchronous. Many deployments insert directly into local tables for efficiency. - Co-location: if two tables are sharded by the same key, a join on that key can run locally (
distributed_product_mode,GLOBALjoins otherwise broadcast the right side). Prefer sharding by the column you group or join on. ON CLUSTERDDL runs a statement on all nodes (needs Keeper for the DDL queue).- Resharding is manual and costly; start with enough shards, and prefer vertical scaling and replicas until you truly need shards.
Worked example
A two-shard cluster simulated on one server (each “shard” is a different database), kb_cluster:
CREATE TABLE kb_s1.ev_local (user_id UInt32, amount Float64) ENGINE = MergeTree ORDER BY user_id;
CREATE TABLE kb_s2.ev_local (user_id UInt32, amount Float64) ENGINE = MergeTree ORDER BY user_id;
CREATE TABLE ev_dist (user_id UInt32, amount Float64)
ENGINE = Distributed(kb_cluster, '', ev_local, cityHash64(user_id));
INSERT INTO ev_dist SELECT number, 1 FROM numbers(1000000) SETTINGS distributed_foreground_insert = 1;| Place | Rows |
|---|---|
shard 1 kb_s1.ev_local | 499,487 |
shard 2 kb_s2.ev_local | 500,513 |
SELECT count(), sum(amount) FROM ev_dist | 1,000,000 / 1,000,000 |
The hash of the key spread rows almost evenly. EXPLAIN for a GROUP BY showed MergingAggregated over ReadFromRemote: each shard aggregated locally and the initiator merged the partial states.
Pitfalls and production notes
- A poor sharding key causes skew (one hot shard); check row counts per shard.
- Joining two Distributed tables without co-location can transfer huge data; use
GLOBALcarefully or denormalise/dictionary-enrich (ch-dictionaries). - Deduplicating engines need all versions of a key on the same shard (ch-replacing-collapsing-engines).
- Query timeouts and memory are per shard; set
max_execution_timeand memory limits. - Passwords in
remote_serversbelong in secure configuration, not in version control.
Practice questions
- What is the difference between a shard and a replica?
- Why shard by
cityHash64(user_id)rather than by date? - How does a Distributed query compute
avg(x)correctly over several shards?
References
- ClickHouse documentation, Distributed table engine; Scaling out
Related
ch-replication-keeper · ch-dictionaries · ch-query-optimization