Bookmarks

Sharding

Horizontal partitioning of data: a dataset is split by the shard key into disjoint parts that are stored on different nodes of a cluster.

Also: database sharding, horizontal partitioning

checked 9 Nov

All terms

When data outgrows a single server, it is not scaled up but split horizontally: each part — a shard — lives on its own node, and together the parts make up one logical dataset. Both storage capacity and write throughput grow this way: add a node, and part of the data moves onto it.

How it works

ElementRole
Shard keya field or set of fields whose value decides which part a record goes to
Sharda node or group of nodes that holds its own disjoint part of the data
Routeraccepts client requests and sends each to the relevant shard or shards
Balancermoves parts of the data between nodes in the background while requests are being served

Clients do not talk to the nodes directly but to a router: in MongoDB this is the mongos process, in Vitess the vtgate layer. If a request includes the shard key, the router sends it only to the affected shard; if it does not, the router fans it out to every shard and gathers the replies — and the more nodes there are, the more such a query costs.

The shard key is the main decision

The key decides whether data and load are spread evenly and whether a typical query lands on a single shard. A key with monotonically increasing values, such as a date or a counter, sends all writes to one node; high cardinality of a field, that is, many distinct values, does not by itself guarantee an even distribution: how often each value occurs and whether the values grow monotonically matter as well. Changing the key is expensive: re-partitioning a large collection means moving much of the data, so the key is chosen before the load grows.

Where it is used

  • MongoDB — sharded clusters: the key is chosen when a collection is sharded, and the balancer moves key ranges between shards
  • Vitess on top of MySQL — a routing and sharding layer created at YouTube to scale MySQL
  • ClickHouse — a table on a cluster distributes rows across shards, and replicas within a shard keep copies
  • Elasticsearch — an index is divided into shards with replica copies

Sources

the term is written from these pages

Verified 9 Nov

Not to be confused with

what it is mixed up with and how it differs

  • partitioning

    Partitioning splits a table into parts within a single server, while sharding spreads the parts across the nodes of a cluster.

  • replication

    Replication keeps copies of the same data on the nodes, sharding keeps disjoint parts of one dataset.

In depth in the references

where the topic is covered in detail