Sharding de Base de Datos
Particionado horizontal
Escrito por Darjus Kazakevic · revisado el
Split the rows across databases; the shard key decides which queries stay fast.
Sharding splits one large table across several databases. Each shard holds a subset of the rows, chosen by a shard key such as tenant_id. Each shard is a full database with its own replicas, so write capacity and storage grow with the number of shards.
Everything depends on the key. A query that includes it goes to one shard. A query without it must ask every shard and merge the results. Joins and transactions across shards become hard, and changing the number of shards means moving data while the system runs. Delay sharding until replicas, caching and a bigger server are not enough.
En esencia — Each shard holds part of the rows. A query that carries the shard key hits one shard; a query without it must ask every shard and merge the answers.
Cuándo recurrir a él
Recurre a él cuando
- Write volume or data size has outgrown the largest server you can run
- Nearly every query already names one key, such as tenant or customer
- A multi-tenant product whose tenants never share data
Reconsidéralo cuando
- Indexes, replicas, caching or a bigger server can still carry the load
- Core queries and reports span all keys, so most of them would fan out
- The team cannot yet run and migrate many databases
Conceptos clave
- Shard key (Partition key)
- The column that decides which shard a row lives on, such as tenant_id or user_id. A good key appears in most queries, has many distinct values, and spreads load evenly.
- Hash, range and directory sharding
- Hash sharding spreads keys evenly but scatters ranges. Range sharding (ids 1–1M on shard A) keeps ranges together, but new rows may all land on the last shard. Directory sharding stores each key's shard in a lookup table.
- Hot spot
- One shard gets far more traffic than the others. Causes: one very large tenant, a key based on time, or one famous user. Fix it by moving that tenant to its own shard or splitting its key.
- Resharding
- Changing the number of shards. With hash mod N, almost every row moves. Many logical shards mapped onto fewer servers let you move whole logical shards instead. Vitess and Citus automate this for MySQL and Postgres.
- Cross-shard queries (Scatter-gather)
- A query without the shard key goes to every shard, and the results are merged. Joins and transactions across shards need application code, two-phase commit or a saga. Reports usually move to a separate analytics store.
Reglas de diseño
- Shard on the key every hot query has
- A query with the shard key hits one shard; a query without it hits all of them and gets slower as shards are added. Pick the key your main queries already carry, such as tenant_id.
- Use many logical shards from day one
- Map, say, 1,024 logical shards onto 8 servers. Growing to 16 servers then moves whole logical shards, not single rows, and the hash function never changes.
- Keep related data on the same shard
- Shard a tenant's users, orders and invoices by the same key. Joins and transactions inside one tenant then still work as they do on a single database.
- Try the reversible options first
- Sharding makes cross-shard joins and transactions hard, and resharding is a live migration. Try better indexes, read replicas, caching and a bigger server first.
Errores habituales
- Sharding by created_at, so every new write lands on the newest shard.
- Using hash mod N with N = physical servers, so adding one server moves almost every row.
- A few huge tenants on one shard at 90% CPU while the other shards idle.
- Auto-increment ids that are only unique per shard, so two rows get the same id.