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 SystemTerm for Partition
MongoDB, ElasticsearchShard
Amazon DynamoDB, Apache KafkaPartition
Apache Cassandra, RiakPartition, vnode
Google Cloud SpannerSplit
Google Cloud BigtableTablet
Apache HBaseRegion
CockroachDBRange
Redis ClusterHash Slot
Azure Cosmos DBPhysical 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.

One Logical Table Split Across 3 Shards
Click any user_id below to trace how the query router hits one shard
Logical table: users (9 rows)
SELECT * FROM users WHERE user_id = 105→ routed directly to Shard 2
Shard 1Untouched
user_id 101–103 · Local index (3 keys)
101 · Ava
102 · Ben
103 · Cam
Shard 2Queried
user_id 104–106 · Local index (3 keys)
104 · Dan
105 · Eli
106 · Fay
Shard 3Untouched
user_id 107–109 · Local index (3 keys)
107 · Gus
108 · Hana
109 · Ian
Lookup user_id = 105: router inspects partition key 105 and queries Shard 2 only (1 of 3 shards). The other two shards do zero work.

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.

Partitioning Strategies: Range vs. Hash vs. Composite
Watch where 6 sequential comments land across 3 partitions
Partition A0 rows
2021–2022
(idle — no recent writes)
Partition B0 rows
2023 Q1
(idle — no recent writes)
Partition CHOT (6)
2023 Apr (Now)
12:34:54 (u:12)
12:34:55 (u:45)
12:34:56 (u:12)
12:34:57 (u:78)
12:34:58 (u:45)
12:34:59 (u:12)
Range by timestamp: every new write hits the latest partition (Partition C), creating a write hotspot while Partitions A and B sit idle.

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_id or order_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