Dev tools
Sharding vs Partitioning: How Databases Scale
Sharding vs partitioning: partitioning splits a table inside one database, sharding spreads data across servers. How each works and when to use it.
In sharding vs partitioning, the key difference is where the pieces live. Partitioning splits a large table into smaller pieces inside one database server: the database still looks and behaves like one system, and it routes each query to the right partitions. Sharding splits data across several database servers, each holding a subset of rows. Each server is a shard, and something (the application, a proxy or a distributed database) has to route each query to the right one.
Partitioning is mostly about managing big tables: faster queries on a slice of data, easy archiving and simpler maintenance. Sharding is about going beyond what one machine can do, in storage, writes or connections. This guide compares them in a table, shows both in SQL and MongoDB, explains how they relate to clustering and replication, walks through a real database sharding example, and gives a checklist for when to shard, which is later than most teams think.
Sharding vs Partitioning at a Glance
| Partitioning | Sharding | |
|---|---|---|
| Where data lives | One database server | Many database servers |
| Main goal | Manage large tables, speed up queries on slices, easy archiving | Scale writes, storage and connections beyond one machine |
| Who routes queries | The database itself | Application, proxy or distributed database layer |
| Transparency to the app | Fully transparent | Often visible: app must know or pass the shard key |
| Cross-piece queries and joins | Normal SQL | Expensive or limited, may need scatter-gather |
| Transactions across pieces | Normal | Hard; often single-shard only or using two-phase commit |
| Scaling limit | Still one machine's CPU, RAM and disk | Add more machines |
| Operational complexity | Low to moderate | High: rebalancing, backups, schema changes across shards |
| Typical tools | PostgreSQL and MySQL native partitioning, Oracle, SQL Server | MongoDB sharding, Citus, Vitess, CockroachDB, application-level sharding |
A useful way to remember it: all sharding is a form of horizontal partitioning, but not all partitioning is sharding.
Is Sharding the Same as Partitioning?
Is sharding the same as partitioning? No, though the words are often used loosely. "Partitioning" is the general idea of splitting data into pieces. "Sharding" is a specific kind of horizontal partitioning in which the pieces sit on separate servers. Some distributed databases, such as Kafka, Cassandra and DynamoDB, use "partition" to mean what other systems call a shard, which adds to the confusion. When in doubt, ask whether the pieces share one machine (partitioning) or are spread across many (sharding).
What Is Partitioning?
Partitioning divides a table into pieces that the database stores and manages separately but queries as one table. There are two directions:
- Horizontal partitioning splits rows: orders from January in one partition, February in another.
- Vertical partitioning splits columns: frequently read columns in one table, large rarely read columns (such as JSON blobs) in another, joined by key.
Horizontal partitioning is what people usually mean. Databases offer three common schemes:
| Scheme | How rows are assigned | Good for |
|---|---|---|
| Range | By value ranges, such as dates or ID ranges | Time-series data, logs, orders by month |
| List | By explicit values, such as region or country | Data naturally grouped by category |
| Hash | By a hash of a key, spreading rows evenly | Even distribution when there's no natural range |
PostgreSQL has supported declarative partitioning since version 10 and hash partitioning since version 11. A list-partitioned table by region looks like this:
CREATE TABLE orders (
id bigint NOT NULL,
region text NOT NULL,
created_at timestamptz NOT NULL,
total numeric
) PARTITION BY LIST (region);
CREATE TABLE orders_eu PARTITION OF orders FOR VALUES IN ('eu');
CREATE TABLE orders_us PARTITION OF orders FOR VALUES IN ('us');The application keeps querying orders. Thanks to partition pruning, which is on by default in PostgreSQL, a query with WHERE region = 'eu' only scans the EU partition. Old time-based partitions can be removed in one fast step with ALTER TABLE ... DETACH PARTITION and then archived or dropped, instead of running a slow bulk DELETE.
Partitioning helps when queries naturally target a slice, such as recent data or one region, and when you need to drop or move old data cheaply. It doesn't add CPU, memory or disk beyond the single server, and queries that touch every partition can even get slower.
What Is Sharding?
Sharding spreads rows across separate database servers using a shard key. Each row's shard key decides which shard stores it, and every query that includes the key can go straight to one shard.
Three parts make it work:
- The shard key, such as
customer_idortenant_id. - A mapping from key to shard: by range, by hash, or through a lookup directory.
- A router that sends each query to the right shard. This can be your application code, a proxy such as Vitess for MySQL, or a distributed database's coordinator.
In MongoDB, sharding a collection by a hashed customer ID is one command once the cluster is set up:
sh.shardCollection("shop.orders", { customer_id: "hashed" })With the Citus extension for PostgreSQL, the equivalent turns a regular table into a distributed one:
SELECT create_distributed_table('orders', 'customer_id');The hard parts come afterwards:
- Cross-shard queries. A report over all customers has to ask every shard and merge the results.
- Cross-shard transactions. Updating rows on two shards atomically needs distributed transaction support, which is slower and more complex.
- Rebalancing. When a shard gets too big or too busy, data has to move without downtime.
- Operations. Backups, schema migrations, monitoring and failover now happen across many databases.
Sharding vs Partitioning vs Clustering vs Replication
Sharding vs partitioning vs clustering is a common three-way confusion, and replication usually joins it. They solve different problems and are often combined:
| Technique | What it does | Solves | Doesn't solve |
|---|---|---|---|
| Partitioning | Splits a table into pieces on one server | Large table management, slice queries, archiving | Single-server limits |
| Sharding | Splits data across servers | Write throughput, storage and connection limits | Read-heavy load on hot rows, failover by itself |
| Replication | Copies the same data to other servers | Read scaling, high availability, backups | Write scaling, storage size |
| Clustering | Groups servers to act as one system (term varies by product) | Availability and sometimes scaling | Depends on what the cluster does |
A typical large setup uses all of them. Data is sharded by customer, each shard is replicated for failover and read capacity, large tables inside each shard are partitioned by month, and the whole thing is managed as a cluster.
"Clustering" also has another meaning in databases: physically ordering rows on disk by an index (as in PostgreSQL's CLUSTER command or SQL Server's clustered index). That's a storage layout choice, not a scaling method.
A Real Database Sharding Example: Notion
A well-documented database sharding example comes from Notion's engineering blog. Notion stores every piece of content as a "block" in PostgreSQL. As the block table grew, the single database ran into write and storage limits.
Their design choices are a good template:
- Shard key: the workspace ID. Almost all queries are scoped to one workspace, so they hit a single shard, and transactions stay on one host.
- Shard everything related: tables tied to blocks (blocks, collections, spaces and others) were sharded the same way, so joins stay on one shard.
- Logical vs physical shards: they settled on 480 logical shards spread evenly across 32 physical databases, with each logical shard a PostgreSQL schema, 15 per database. Moving a logical shard to a new server later is much easier than splitting data again, and 480 divides evenly into many possible server counts.
- Limits that drove the numbers: they set upper bounds of 500 GB per table and 10 TB per physical database.
The key lessons are to choose a shard key that matches how queries are scoped, keep related data together, and create many small logical shards up front so you can rebalance by moving them.
Choosing a Shard Key
The shard key is the most important and least reversible decision in sharding. A good one has:
- High cardinality: many distinct values, so data spreads across many shards.
- Even distribution: no single value holds a huge share of rows or traffic.
- Query locality: most queries include it, so they hit one shard.
- Stability: rows rarely change their key, because changing it means moving the row.
Watch out for:
- Monotonically increasing keys (timestamps, auto-increment IDs) with range sharding. All new writes go to the last shard, creating a hot spot. Hash them, or use a different key.
- Celebrity tenants. In multi-tenant products, one huge customer can overwhelm its shard. Plan to give very large tenants their own shards. Our guide to multi tenant architecture covers tenant isolation patterns.
- Keys missing from common queries. If your main dashboard query doesn't include the shard key, it becomes a scatter-gather across every shard.
When to Use Database Sharding
Sharding is powerful but expensive to run. Here's when to use database sharding, and what to try first.
Try these first, roughly in order:
- Fix slow queries with indexes and better query plans.
- Add a cache for hot reads.
- Add read replicas for read-heavy load.
- Scale up the server: a bigger machine with more memory and faster disks goes a long way.
- Partition large tables by time or category, and archive old data.
- Move unrelated workloads (analytics, search, logs) to systems built for them.
Shard when one or more of these is true and the steps above aren't enough:
- Write throughput exceeds what the largest practical single server can sustain.
- Data size makes backups, restores, schema changes or vacuuming unmanageable on one server.
- You need data in specific regions for latency or data residency.
- Tenants or customers need strong isolation from each other's load.
If you do shard, consider a database that handles it for you (Citus, Vitess, MongoDB, CockroachDB, YugabyteDB, or a managed equivalent) before building sharding into application code. For choosing the base database in the first place, see Postgres vs MySQL.
Common Mistakes
- Sharding too early. It adds complexity to every feature and migration. Most products never need it.
- Choosing the shard key from today's queries only. Think about how the product's main access patterns will look as it grows.
- Too few shards. Starting with exactly as many shards as servers makes every future expansion a painful split. Use many logical shards.
- Ignoring cross-shard reporting. Plan a separate analytics path, such as a warehouse fed by change data capture, rather than running reports across shards.
- Partitioning without pruning. If queries don't filter on the partition key, every partition gets scanned and performance can get worse.
- Too many tiny partitions. Thousands of partitions add planning overhead. Size partitions so each is meaningful, such as monthly rather than hourly for most workloads.
FAQ
Can you use partitioning and sharding together?
Yes, and large systems often do. Data is sharded across servers by a key such as tenant ID, and large tables inside each shard are partitioned by date for faster queries and easy archiving.
Is partitioning faster than sharding?
For queries that stay on one server, partitioning avoids network hops and cross-shard coordination, so it's simpler and often faster. Sharding is faster overall only when one server can't keep up with the total load.
What is horizontal vs vertical partitioning?
Horizontal partitioning splits a table's rows into groups, such as by date or region. Vertical partitioning splits its columns into separate tables, such as moving large rarely used columns out of a frequently read table.
Does sharding improve read performance?
It can, by spreading reads across servers, but read replicas are usually a simpler way to scale reads. Sharding mainly helps with write throughput and data size.
What is a shard key?
The shard key is the column or set of columns used to decide which shard stores each row, such as customer ID. Queries that include it can go straight to one shard.