# Shard the database

> Split the Database into shards by a key, so writes divide across several primaries once one box at its largest size is full.

Split one Database into several shards on a key, so each shard takes a share of the writes.

## Move up when

- The primary is at its largest size and still saturates on writes, with replicas already added.
- Disk operations queue on the primary even after you buy faster volumes.
- Vertical growth has run out, so the next step cannot be a bigger box.

## The Design



The App Server calls one Database Node. The Node now stands for several shards, each a copy of its size. A request goes to the shard that owns its key.

## Why it works

Reads and writes scale with the shard count, because each shard sees only its slice. The key decides how even that slice is. A hash of the user ID spreads Users evenly. A range key sends new Users to the last shard, and a tenant key sends a large tenant to one shard, so those leave a hot shard. A region key keeps data near its Users but pays a round trip for far ones.

## What it costs

Every shard bills as a full Database. A report or search touches every shard, so each shard past the first adds CPU and a little latency. A transaction that spans shards commits on each, and any one can fail it. Changing the count or the key later is a resharding that costs Dev Hours and a long copy, so choose the key early. The game offers no consistent-hashing option for the shard map, only these four keys.

## In Quorum

Set **Shards** and **Shard key** on a [Database](https://wiki.playquorum.dev/components/database.md). Research [Sharding](https://wiki.playquorum.dev/research/sharding.md). The [Big tenant hot shard](https://wiki.playquorum.dev/incidents/hot-shard.md) incident shows a bad key. See [sharding](https://wiki.playquorum.dev/patterns/sharding.md), [hot keys and hot shards](https://wiki.playquorum.dev/patterns/hot-keys-and-hot-shards.md) and [Scale the database](https://wiki.playquorum.dev/guides/scale-the-database.md). The next rung, [distributed SQL and leaderless stores](https://wiki.playquorum.dev/patterns/distributed-sql-and-leaderless-stores.md), moves the splitting into the store itself.

Source: https://wiki.playquorum.dev/patterns/shard-the-database
