What are sharding and partitioning?
Both split a large dataset into pieces. Partitioning divides a table within one database; sharding distributes data across separate database instances. The distinction matters because one is an optimisation and the other changes your architecture fundamentally.
Partitioning. A single logical table is stored as several physical pieces, by range, by list, or by hash of a column. The database handles it, queries are unchanged, and the benefits are real: partition pruning, where the planner skips partitions that cannot contain matching rows; cheap bulk deletion by dropping a whole partition; and smaller indexes per partition.
Its limit: you are still bound by one machine's capacity.
Sharding. Data is divided across independent databases, each holding a subset, routed by a shard key. This buys horizontal scale — and costs a great deal.
What sharding actually costs:
Cross-shard queries become expensive or impossible. Anything that must touch all shards is now a scatter-gather, and joins across shards generally are not supported.
Transactions across shards require distributed transaction protocols, which are slow and complex — so most systems avoid them by designing so transactions stay within a shard.
The shard key becomes an irreversible decision. Choosing badly produces hot shards where one holds disproportionate traffic, and changing it later requires migrating everything.
Rebalancing is genuinely hard, which is why consistent hashing and virtual shards exist — to move a fraction of data rather than remapping all of it.
Operational burden multiplies: backups, schema migrations, monitoring and failover, per shard.
Global uniqueness and ordering become non-trivial, which is why distributed ID schemes exist.
What to do before sharding, because it is very frequently unnecessary: add indexes, fix the queries, add caching, use read replicas, archive cold data, partition within one database, and buy a larger machine — vertical scaling reaches much further than people assume.
Shard last, when you have measured that nothing else suffices.