Lesson 3 of 9 · 23m
Sharding, Replicas, and Distributed Tables
How data spreads across a cluster, what replication protects, what Distributed tables do and do not do, and the operational assumptions that bite new operators.
ClickHouse scales out with a simple but unforgiving division of labor: sharding spreads the data, replication copies each shard, and Distributed tables fan queries out. None of these pieces magically designs the cluster for you. A bad shard key or a lazy Distributed table can make a six-node cluster slower than one well-fed machine.
Shards divide rows
A shard is a horizontal slice of the dataset. Rows are routed according to a sharding key or an explicit routing expression:
CREATE TABLE events_distributed AS events
ENGINE = Distributed('analytics', 'default', 'events', cityHash64(user_id));Choose a key that matches ownership and query shape. If nearly every query filters by tenant_id, sharding by tenant_id keeps a tenant's data together and can reduce cross-shard traffic. If users are roughly uniform, cityHash64(user_id) spreads load evenly. Random sharding spreads data but makes some queries touch every shard even when they did not need to.
Replicas copy shards
Replication is per-shard high availability, not an alternative to sharding. A replicated table normally uses ReplicatedMergeTree plus ClickHouse Keeper or ZooKeeper for coordination:
ENGINE = ReplicatedMergeTree(
'/clickhouse/tables/{shard}/events',
'{replica}'
)The replicas coordinate metadata and part state through Keeper. Each replica still stores the data and performs merges. Replication protects against node loss and lets you query surviving replicas; it does not reduce the write cost of merges, and it does not fix undersized disks.
Distributed tables are query routers
A Distributed table is not another copy of your data. It is a logical table that knows how to reach shard-local tables. Queries against it fan out and aggregate partial results. Inserts can also go through it, in which case rows are forwarded to the owning shard, sometimes through asynchronous local queues.
That indirection is powerful, but it hides failure modes. A down shard can turn a fast dashboard into a partial result or an error depending on settings. A query without a shard-filtering predicate may scatter to every node even when the user only asked for one tenant.
Reads scale differently from writes
ClickHouse can parallelize scans brutally well on one node and across shards. Writes are more delicate because every inserted block becomes parts that replicas must replicate and merge. When people say a cluster is "write hot," they often mean the merge-and-replication pipeline is saturated, not that the SQL parser is busy.
Field notes
- Start with fewer, larger shards unless you have a clear scaling reason. More shards mean more coordination and more operational surface area.
- ClickHouse Keeper is part of the data plane for replicated tables. Treat its health as storage health.
- Use
clusterAllReplicas('analytics', system.parts)or cluster DDL carefully; broad operations make broad outages possible. - Distributed inserts decouple producers from shard topology but add queue behavior you must monitor.
- Design for rebalancing pain. ClickHouse does not automatically reshuffle existing data when you add shards the way some systems do.
Was this lesson helpful?