System design concepts · Data and storage

Sharding vs Partitioning

Short answer

Partitioning is the general idea of splitting a data set into smaller pieces by a rule, such as a date range, a list of regions or a hash of a key. Sharding is horizontal partitioning where the pieces live on different servers, so each machine stores and serves only its share of the rows. Every shard is a partition, but not every partition is a shard: PostgreSQL table partitioning splits one table into child tables on the same server, which speeds up queries that touch one range and makes dropping old data cheap, but it adds no capacity. Shard when a single machine can no longer hold the data or absorb the write load; partition inside one database when you want pruning and easier maintenance.

Read the video transcript

Partitioning and sharding both split a big table into smaller pieces. The difference is where those pieces live. Partitioning splits one table into parts by a rule, like one partition per month. All of them stay on the same database server. Ask for March orders, and the planner skips every other partition. Deleting last year is just dropping a partition. But it is still one server, with the same disk and the same CPU.

Sharding puts the pieces on different servers. A router hashes each user ID to find the shard that owns it. Now each machine holds only its share of the rows, so storage and write throughput grow with every shard you add.

The price shows up in queries that span shards. A query across all users has to ask every shard and merge the answers, and one hot key can still overload a single machine. So every shard is a partition, but not every partition is a shard. Partition for pruning and easy maintenance. Shard when one machine can no longer hold the data, or absorb the writes.

Sharding vs Partitioning: comparison table

AspectShardingPartitioning
ScopeRows split across separate serversAny split of data, by rows or by columns, on one node or many
Main goalMore storage and write throughput than one machine can offerQuery pruning, cheaper maintenance, data locality
Who routes queriesThe application, a proxy, or the database router maps each key to a shardOften the database planner, invisibly
Joins and transactionsCross-shard joins and transactions are slow or unsupportedStill one server, so joins and transactions work as usual
Adding capacityRequires moving data; consistent hashing or many small virtual shards limit how muchNot the point; add partitions as new ranges arrive
ExamplesMongoDB sharded clusters, Vitess over MySQL, Cassandra token rangesPostgreSQL partitions by month, a table split into hot and cold columns
A query for one month reads only the matching partition and skips the others.

When to use each

Shard when

  • The data set or its write rate no longer fits one primary, even a large one.
  • Most queries hit one key, such as one user, tenant or order, so they stay on one shard.
  • You have already used the cheaper options: a bigger machine, read replicas and caching.

Partition within one database when

  • Queries usually filter on one dimension, like time, and could skip most of the table.
  • You regularly delete old data and want to drop a partition instead of running a huge DELETE.
  • Indexes on the whole table have grown too large to maintain comfortably.
The verdict

Shard when

The data set or its write rate no longer fits one primary, even a large one.

Otherwise

Partition within one database when

Queries usually filter on one dimension, like time, and could skip most of the table.

A query across all users fans out to every shard and merges the results, then one hot key overloads a single shard.

The names change between systems

Products use these words differently. MongoDB and Elasticsearch call their distributed pieces shards. Kafka and Cassandra call them partitions even though they are spread across brokers or nodes. Bigtable and HBase say tablets and regions. In an interview, define the term once ("by sharding I mean splitting rows across servers by a key") and the naming question goes away.

Choosing the key is the whole game

A good shard key has many distinct values, spreads load evenly, and matches the way data is read, so most requests touch one shard. Hashing a user ID spreads load well but makes range scans across users expensive. Range partitioning by time keeps scans cheap but sends all new writes to the newest partition.

hash(user_id) sends each row to the one shard that owns it.

Hot keys break even a good scheme. A celebrity account or a viral product can overload the shard that owns it, so designs add caching in front, split the hot key into sub-keys, or give it dedicated capacity. Secondary indexes add another choice: a local index per shard needs scatter-gather reads, while a global index needs extra writes on every update.

Interview angle

How it comes up in a system design interview

Sharding comes up in nearly every design once the data stops fitting on one machine. The interviewer is not checking that you know the word. They want the shard key, the reason for it, what happens to queries that do not include it, and how you add capacity later.

Where you will meet it

  • Tweets do not fit on one database. What is the shard key, and how do you build a home timeline without querying every shard?
  • How does a key-value store place keys on nodes, and what moves when you add a node?
  • One shard is much hotter than the others. How do you find it and fix it?
  • How do you generate unique IDs once writes are spread across shards?
How a strong answer sounds
“I would shard the messages table by conversation ID with consistent hashing, because almost every read is "recent messages in one conversation", so it stays on one shard. Queries by user go through a separate index keyed by user ID. To add capacity we split virtual shards rather than rehashing everything.”

Common mistakes

  • Sharding too early. It costs you joins, transactions and operational simplicity, so justify it with numbers first.
  • Picking a shard key that does not match the main read path, which turns common queries into scatter-gather.
  • Using a plain hash modulo the node count, which moves almost every key when the cluster grows.

Frequently asked questions

Is sharding the same as horizontal partitioning?

Very nearly. Horizontal partitioning splits rows into groups; sharding is horizontal partitioning where the groups are stored on different servers.

What is vertical partitioning?

Splitting by columns or by table instead of by rows: for example, moving large, rarely read columns to a separate table or store so the common queries scan less data.

What makes a good shard key?

High cardinality, an even spread of reads and writes, and alignment with the main access pattern so that most requests can be answered by one shard.

When should a database be sharded?

When one primary cannot keep up with the write rate or hold the data, after you have tried vertical scaling, read replicas and caching. Sharding adds lasting complexity, so it should be a measured decision.

Learn and practise

Lessons

Problems where this choice matters

Knowing the trade-off is half of it

Practise saying it under follow-up questions: a timed system design interview with an AI interviewer that probes your choices and grades against a rubric.

Start a mock interview