Message Queues & StreamingMessage Ordering & PartitioningHard⏱️ ~2 min

Partition Stability vs Elasticity: Handling Repartitioning

Modulo Hashing Instability: Using hash(key) mod N to map keys to partitions means changing N remaps many keys to different partitions. With 10 partitions growing to 11, roughly 9 out of 10 keys change partition assignment. This disrupts consumer cache locality and creates thundering herd effects.
Modulo Hashing
Fast, but ~90% keys remap on resize
vs
Consistent Hashing
More complex, only ~1/N keys remap
Consistent Hashing Benefits: Map keys to points on a ring (0 to 2^32) and assign partition ownership to ranges. Adding a partition claims only roughly 1/N of the ring, remapping only that fraction of keys. Virtual nodes (each physical partition owns multiple ring positions) smooth distribution. LinkedIn and Amazon use consistent hashing internally for stable partition assignment. Consumer Rebalance Pain: When partitions are added or consumers join/leave, the group coordinator triggers a rebalance. Naive stop-the-world rebalancing pauses all consumers for seconds to minutes. Cooperative rebalancing and sticky assignment reduce disruption but still add tail latency during transitions. Capacity Planning Best Practices: Minimize resize frequency by starting with headroom and scaling in large increments. Prefer doubling partition count (10 to 20 to 40) rather than incremental adds (10 to 11 to 12). LinkedIn aims to keep partitions under 50% sustained utilization to absorb spikes without emergency resizing.
💡 Key Takeaways
Modulo hashing remaps most keys when partition count changes. hash of key mod N means changing N from 10 to 11 remaps roughly 90 percent of keys to different partitions, disrupting caches and storage locality.
Consistent hashing with virtual nodes minimizes remapping. Mapping keys to ring positions and partition ranges means adding one partition remaps only roughly 1 divided by N keys (9 percent for 11 partitions vs 90 percent with modulo).
Consumer rebalances pause processing during partition changes. Stop the world rebalancing halts all consumers for seconds to minutes; cooperative rebalancing reduces but does not eliminate disruption and tail latency spikes.
Scale in large increments to amortize disruption cost. Doubling partition count (10 to 20 to 40) spreads pain over fewer events than incremental adds (10 to 11 to 12 to 13), reducing operational toil and consumer churn.
Provision 30 to 50 percent headroom to avoid emergency resizing. LinkedIn targets under 50 percent sustained partition utilization to absorb traffic spikes without triggering frequent, disruptive repartitioning events.
📌 Interview Tips
1Amazon Kinesis stream resharding: splitting 10 shards to 20 using consistent hashing remaps only 50 percent of keys vs 95 percent with modulo; consumers using KCL (Kinesis Client Library) handle rebalance automatically with cooperative protocol
2LinkedIn Kafka cluster expansion: doubling topic from 64 to 128 partitions triggers rebalance across 100+ consumer instances; cooperative rebalancing limits per consumer pause to under 5 seconds vs 30+ seconds with stop the world
3Azure Event Hubs partition increase: adding partitions to existing hub does not remap existing partition keys (partition key to partition ID mapping is stable), but new keys distribute across expanded partition set
← Back to Message Ordering & Partitioning Overview