# Partitioning and Sharding

> Split big tables and datasets by key so no single table or server has to hold everything. Free notes, key concepts, resources and a mini project.

Canonical: https://www.internhack.in/roadmaps/tracks/backend/sharding-partitioning
Last updated: 2026-11-28

Split big tables and datasets by key so no single table or server has to hold everything.

Advanced · about 4 hours · difficulty 4/5

Learn first: [Replication, Read Replicas and Failover](https://www.internhack.in/roadmaps/tracks/backend/replication-read-replicas), [Indexes and Query Performance](https://www.internhack.in/roadmaps/tracks/backend/indexes-query-performance)

When one table holds billions of rows, or one server cannot handle the write load, you split the data.

### Partitioning within one database

**Table partitioning** divides a large table into smaller physical pieces by a key, while queries still see one table. PostgreSQL supports **range** (by month), **list** (by region) and **hash** partitioning. Benefits: queries that filter by the key touch fewer partitions, and dropping an old month is instant instead of a giant `DELETE`. This is often enough and much simpler than sharding.

### Sharding across servers

**Sharding** puts different subsets of rows on different database servers. Each shard is a full database holding, say, a range of tenant ids.

- **Shard key choice** is everything. A good key spreads load evenly and keeps data queried together on the same shard (tenant id for a B2B SaaS, user id for a social app).
- **Hash-based** sharding spreads evenly; **range-based** keeps ranges together but risks hot spots; a **lookup directory** gives flexibility at the cost of another component.

### What sharding costs you

- Cross-shard joins and transactions become hard or impossible.
- Resharding when a shard gets too big is a major project.
- Every query must know which shard to hit.

Tools like **Vitess** (MySQL) and **Citus** (Postgres) hide much of this, and distributed SQL databases (CockroachDB, YugabyteDB, Spanner) shard automatically.

### Before you shard

Exhaust the cheaper options first: better queries and indexes, caching, read replicas, a bigger machine, table partitioning and archiving cold data.

You are ready to move on when you can partition a large events table by month and argue for a shard key in a multi-tenant app.

## Key concepts

- **Range, list and hash partitioning**
- **Choosing a shard key**
- **Hot spots and skew**
- **Cross-shard queries**
- **Resharding**
- **Vitess, Citus and distributed SQL**

## Mini project

Convert an events table to monthly range partitions in Postgres, measure query plans before and after, and write a script that detaches partitions older than a year.

## Check yourself

Why is created_at usually a poor shard key for a write-heavy table?

## Free resources

- [PostgreSQL: Table Partitioning](https://www.postgresql.org/docs/current/ddl-partitioning.html) (PostgreSQL)
- [Vitess documentation](https://vitess.io/docs/) (Vitess)
- [Citus documentation](https://docs.citusdata.com/) (Citus Data)

Previous: [Replication, Read Replicas and Failover](https://www.internhack.in/roadmaps/tracks/backend/replication-read-replicas) · Next: [CAP, PACELC and Consistency Models](https://www.internhack.in/roadmaps/tracks/backend/cap-consistency-models)

## More in Scaling Your Database

- [Connection Pooling](https://www.internhack.in/roadmaps/tracks/backend/connection-pooling)
- [Replication, Read Replicas and Failover](https://www.internhack.in/roadmaps/tracks/backend/replication-read-replicas)
- [Partitioning and Sharding](https://www.internhack.in/roadmaps/tracks/backend/sharding-partitioning)
- [CAP, PACELC and Consistency Models](https://www.internhack.in/roadmaps/tracks/backend/cap-consistency-models)

## Related

- [Roadmaps](https://www.internhack.in/roadmaps)
- [Backend Developer Roadmap](https://www.internhack.in/roadmaps/tracks/backend)
