Database Partitioning
When a table outgrows the storage or write capacity of one machine, partitioning (also called sharding) splits the table into smaller subsets spread across multiple servers. Each partition is a self-contained slice with its own rows and indexes, so writes to different keys run in parallel on different nodes.
Partitioning vs. Sharding
Partitioning and sharding refer to the same mechanism: dividing one logical dataset across multiple storage units. Different databases use different names for a single partition:
| Database System | Term for Partition |
|---|---|
| MongoDB, Elasticsearch | Shard |
| Amazon DynamoDB, Apache Kafka | Partition |
| Apache Cassandra, Riak | Partition, vnode |
| Google Cloud Spanner | Split |
| Google Cloud Bigtable | Tablet |
| Apache HBase | Region |
| CockroachDB | Range |
| Redis Cluster | Hash Slot |
| Azure Cosmos DB | Physical Partition |
Why Partition
Replication copies the same dataset onto multiple servers, which helps scale reads because queries can hit nearby or less busy replicas. Every write still lands on the same primary node, and the entire dataset must still fit on one machine. Partitioning addresses those two limits directly:
- Write and storage scalability: Rows are spread across multiple servers, so writes to different keys run in parallel and total storage grows with the number of nodes.
- Fault isolation: If one partition becomes unavailable, requests targeting other partitions continue serving normally instead of taking down the whole table.
How Partitioning Works
A partitioned database splits one logical table into smaller, self-contained partitions. Each partition lives on its own server and maintains its own subset of rows and local indexes. When a read or write arrives, a query router inspects the row's partition key (the column chosen to route data) and forwards the request directly to the partition that owns that key.
Click any user_id below to see which shard handles the query, then toggle Take Shard 2 offline to see fault isolation.
In production, each partition is also replicated onto backup servers so a single machine crash does not lose data (covered in Database Replication).
Managed cloud databases handle partition splitting automatically as storage or throughput grows. For example, Amazon DynamoDB splits partitions around a 10 GB storage limit (so a 10 TB table spans roughly ), Azure Cosmos DB splits physical partitions near 20 GB, and Google Cloud Spanner splits ranges automatically based on both size and CPU load.
Partitioning Strategies
Every write needs a deterministic rule that maps a row's partition key to one partition. Three strategies cover almost all production workloads: key range, key hash, and composite keys. Switch between the three strategies below, run a time-range query, and turn on the celebrity spike (then salting) to watch how each strategy handles writes and queries.
Key Range Partitioning
Key range partitioning assigns contiguous intervals of keys to each partition, similar to volumes of an encyclopedia split by starting letter (A–F, G–M, N–Z). Keeping adjacent keys on the same server makes range queries fast because a query for a date or alphabetical slice reads from one partition.
The trade-off is write hotspots. When rows are partitioned by timestamp, every new write lands on the partition holding the current time window while older partitions sit idle.
Key Hash Partitioning
Key hash partitioning runs a hash function on the partition key and assigns ranges of hash outputs to partitions. A uniform hash spreads sequential keys (such as timestamps one second apart) evenly across every node, removing time-based hotspots.
The trade-off is range queries. Because adjacent keys hash to unrelated partitions, any range query over the hashed key must scatter-gather across every partition and merge the results.
Composite Primary Keys
Databases such as DynamoDB, Cassandra, and Spanner combine both approaches with a composite primary key made of a partition key (hashed) and a sort key (ordered within the partition). For a comments table keyed by (user_id, timestamp), hashing user_id spreads users evenly across servers, while sorting by timestamp keeps each user's comments contiguous in chronological order on one partition.
Skewed Loads and Key Salting
Even with hash partitioning, a single popular key (such as a celebrity account receiving thousands of likes or comments per second) routes all of its traffic to one partition. Adding a small random suffix (called salting), such as user_99#0 through user_99#9, spreads writes across .
The trade-off moves to reads: reading all items for that salted key requires querying all 10 salted keys and merging the results. Apply salting only to known hot keys rather than every row in the table.
Choosing a Partition Key
Selecting the partition key determines whether a partitioned database scales smoothly or bottlenecks on cross-shard queries:
- High cardinality: Pick a column with many distinct values (such as
user_idororder_id) so writes spread across many partitions. - Even write distribution: Avoid monotonically increasing keys (such as raw timestamps) as the sole partition key unless writes are evenly spread across ranges.
- Single-shard query isolation: Match the most frequent read path so common queries hit one partition instead of broadcasting to all shards.
- Composite keys for one-to-many timelines: Pair a high-cardinality hash key with a sort key (such as
(video_id, created_at)) when queries fetch sorted slices per entity.
Partitioning applies beyond relational and NoSQL databases: message queues (such as Kafka topics), distributed caches, and search indexes use the same key-to-partition mechanics.
Key idea. Partitioning scales writes and storage by splitting one table across multiple servers, while replication scales reads and fault tolerance. Key range partitioning supports fast range queries at the risk of write hotspots; key hash partitioning spreads writes evenly at the cost of scatter-gather range queries; composite keys (
hash_key, sort_key) give even distribution across entities with sorted ranges inside each entity.
Further reading
- Choosing the Right DynamoDB Partition Key — AWS — worked examples of partition key design and write salting.
- Schema Design and Hotspot Prevention — Google Cloud Spanner — why sequential primary keys create hotspots and how to avoid them.
- Partitioning and Horizontal Scaling — Azure Cosmos DB — logical vs. physical partitions and automatic splitting.