Partitioning Strategies in Distributed Systems
Partitioning (also called sharding in databases or topic partitioning in message brokers) is the art of splitting a large dataset or stream into disjoint slices so reads, writes, and storage scale horizontally across many machines.
While Sharding & Replication explains why you split vs. copy data, this guide goes deeper into how to pick the right partitioning strategy, avoid skewed hot partitions, and rebalance partitions safely as your cluster grows.
Range Partitioning vs. Hash Partitioning
Your choice of partitioning strategy is a direct trade-off between fast range queries and even write distribution:
Range-Based Partitioning
Assigns each partition a continuous range of keys (like A–M, N–Z, or monthly date ranges Jan–Apr, May–Aug, Sep–Dec). Keys inside each partition stay sorted.
- Strength: Range scans (
WHERE created_at BETWEEN '2026-11-01' AND '2026-11-26') hit a single partition cleanly. - Danger: Monotonically increasing keys (timestamps, auto-increment IDs) funnel 100% of current writes onto the latest partition, creating a severe hot partition while older partitions sit idle.
Hash-Based Partitioning
Passes the partition key through a fast, deterministic hash function (like MurmurHash3 or MD5) and maps the hash onto a partition — typically via consistent hashing or fixed virtual buckets.
- Strength: Even consecutive keys (
user_101,user_102,user_103) land on completely different shards, eliminating sequential write hotspots. - Danger: Adjacent keys are now scattered across every node, so range queries across keys must perform an expensive scatter-gather to every partition.
Composite (Compound) Partitioning — Best of Both Worlds
Systems like Apache Cassandra and Amazon DynamoDB use a two-part key: (partition_key, sort_key) — for example (user_id, created_at):
- The partition key (
user_id) is hashed to spread users evenly across the cluster. - Within a single user’s partition, rows are sorted by
created_at, so fetching a user’s last 50 orders is a single fast sequential slice on one shard!
| Strategy | Point Lookups | Range Queries | Write Balance | Best Use Case |
|---|---|---|---|---|
| Range | Single partition | Single partition (fast!) | Poor on sequential keys | Time-series archives, ordered lexicons |
| Hash | Single partition | Scatter-gather (all shards) | Excellent across random keys | User profiles, sessions, key-value caches |
Composite (hash, sort) |
Single partition | Fast within one partition key | Excellent (unless one key is a celebrity) | Chat timelines, user activity feeds, IoT sensors |
| List / Geo | Single partition | Per-region | Depends on regional traffic | Data residency (region = EU, US, IN) |
Solving Skewed Data and Hot Keys (Celebrity Problem)
Even with hash partitioning, if one celebrity account (user_id = 1) or viral flash-sale product receives 40% of all traffic, all of that traffic hashes to the exact same partition and melts that single machine.
Three proven fixes:
- Key Salting (Write Sharding): Append a random suffix
0..9to hot keys on write (celebrity_1#0…celebrity_1#9) so writes spread across 10 partitions. Reads merge results from all 10 salted keys (only apply salting to known hot keys, not normal users!). - Read-Heavy Hot Keys → Caching: Put a front-end Redis cache or local in-process cache in front of the shard so 99% of reads never touch the database.
- Dedicated Tenant Shards: In multi-tenant B2B SaaS, use a directory lookup table to isolate massive enterprise customers onto their own dedicated shards while packing small tenants onto shared hash shards.
Rebalancing Partitions When Nodes Join or Leave
Never use naive hash(key) % numNodes — adding one node reshuffles almost everything. Production databases use one of three rebalancing models:
- Fixed Number of Virtual Partitions (vBuckets / Logical Shards): Create far more partitions than physical nodes upfront (e.g.,
1,024logical partitions across8servers =128per server). When you add a 9th server, it steals ~114whole logical partitions from existing nodes — no key re-hashing required! Used by Elasticsearch, Redis Cluster (16,384hash slots), and Riak. - Dynamic Splitting & Merging: When a range partition grows past a size threshold (e.g.,
10 GB), the cluster automatically splits it in half (P1 → P1a + P1b) and moves one half to a less-loaded node. Used by HBase, CockroachDB, and MongoDB. - Consistent Hashing with Virtual Nodes: Places
100–200virtual tokens per machine around a consistent hash ring so new machines smoothly absorb proportional arcs. Used by Cassandra and DynamoDB.
FAQs
How do I choose the best partition key?
Pick a key with high cardinality (millions of distinct values), even access distribution (no single value dominates traffic), immutability (values never change after insert), and query alignment (appears inWHERE clauses for 90%+ of your hottest queries).What happens if a query doesn’t include the partition key?
The coordinator doesn’t know which partition holds the matching rows, so it must broadcast the query to every partition and merge the results (scatter-gather). Tail latency becomes the latency of the slowest shard, and cluster CPU scales poorly — fix with a Global Secondary Index (see Database Indexing) or a materialized read view.How many partitions should I start with in a fixed-partition system?
Choose enough logical partitions to cover your expected 5-year cluster growth (often 10×–50× your initial node count, like 256 or 1,024). Too few limits future scaling; too many adds metadata, file-descriptor, and heartbeat overhead.How does Kafka partitioning relate to database partitioning?
It follows the exact same principles! Kafka hashes each message’s key (hash(orderId) % numPartitions) so all events for the same order land in the same partition (preserving strict per-order ordering), while spreading total throughput across brokers and consumers.