Sharding vs partitioning: splitting your data
Vinnie on rung seven of the scaling ladder: partitioning splits a table inside one database, sharding splits data across many. PostgreSQL's range, list and hash partitions and pruning, choosing a shard key, hot shards and the pain of resharding, how MongoDB, MySQL, SQL Server, Citus and Vitess do it, and the managed versions on AWS, Azure and Google Cloud.
Diagrams in this video · 1Free in archhy
The diagrams, full size
Copy any of them into archhy, free, and change it for your own system.
After this lesson you can
- Tell partitioning apart from sharding
- Partition a table by range in PostgreSQL
- Choose a shard key, and spot a hot one
- Say why resharding hurts, and when not to shard
Transcript
Sharding vs partitioning: splitting your data
Vinnie here. Rung seven. Partitioning and sharding. People use the two words as if they're the same. They aren't. One splits a table inside one database. The other splits your data across many databases. Let's sniff out which one you need.
Partitioning vs sharding
Here are both, side by side. One. Partitioning. One big orders table, split into pieces by month. To your queries, it's still one table. And it's still one database, on one server. Two. The win is pruning. A query that filters on the date skips every partition that can't match. And dropping an old month is dropping a partition, not a giant delete. Three. Sharding. Your data is split across separate databases. A router, or your app, uses the shard key to pick the right one. Four. The shards are spread across servers. So writes and storage scale out, not just up. Five. The catch. Pick the wrong key, and one shard gets all the busy traffic. That's a hot shard, and adding more shards won't cool it down. Change the key, or spread the hot value, instead. Six. And changing your mind is expensive. Resharding copies data between servers, while the app keeps running. In MongoDB, it needs about twice the storage, and blocks writes for up to two seconds.
Partitioning in PostgreSQL
Here's partitioning in PostgreSQL. Create table orders, partition by range, on the created at date. Notice the primary key. On a partitioned table, it has to include the partition key. Here, the date. Each month is a partition of orders. From the first of November, up to but not including the first of December. PostgreSQL also partitions by list, for named values like regions, and by hash, to spread rows evenly. When a month gets old, detach or drop its partition. Far faster than a bulk delete.
Choosing a shard key
The shard key is the decision that matters most. You want lots of distinct values, used evenly, so the load spreads out. And not a key that keeps rising, like a timestamp. Every new write lands on one shard. And you want it in most of your queries. In MongoDB, a query without the shard key goes to every shard, and waits for all of them. For a shop, a customer ID is a common choice. A country usually isn't. Too few values, and a few of them are huge.
How the big databases do it
PostgreSQL, MySQL and SQL Server all partition tables inside one database. In MySQL, a partitioned table can't have foreign keys. SQL Server uses a partition function and a partition scheme. MongoDB has sharded clusters. Mongos routes each query, a balancer moves data between shards, and since MongoDB five point oh, you can reshard a collection onto a new key. For SQL that outgrows one server, there's Citus, an open-source extension for PostgreSQL. And Vitess, a clustering layer in front of MySQL.
In the cloud
And the managed versions, on the three big clouds. On AWS, DynamoDB spreads your data by partition key. Each partition serves up to three thousand read units and one thousand write units a second, so one hot key can hit that ceiling. The fix is write sharding: add a suffix to the key, to spread it. For SQL, Aurora PostgreSQL Limitless Database shards by hash, behind one endpoint. And Aurora DSQL is distributed SQL, with no shards for you to manage. On Azure, Cosmos DB places items by partition key too, and you can't change the key in place. Each logical partition holds up to twenty gigabytes. For PostgreSQL, there are elastic clusters, built on Citus. The older Cosmos DB for PostgreSQL is on a retirement path, and not recommended for new projects. On Google Cloud, Spanner splits rows into ranges of the primary key, and spreads them as the load grows. Bigtable sorts by row key. On both, a key that keeps rising, like a timestamp, piles every new write onto one spot.
When not to shard
When not to shard? Almost always, until you have to. Joins and transactions across shards get hard, or slow. And every query has to think about the key. So try the lower rungs first. Partitioning is cheap by comparison. Sharding is close to the top of the ladder for a reason.
Remember
Let's recap. Partitioning splits a table inside one database. Pruning skips what can't match, and old data drops out cheaply. Sharding splits your data across databases, by a key. Choose it well. Hot keys hurt, and resharding hurts more.
The full article, and what's next
Next time, the top rung. CQRS, command query responsibility segregation, and a database per service. See you there.





