The default Kafka partitioner create hash key collision
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Apache Kafka is a distributed streaming platform that facilitates the publishing and subscribing of streams of records. One of the critical aspects of Kafka's design is partitioning, which allows for the parallel processing of data across multiple nodes. By default, Kafka distributes messages to different partitions based on the key of the messages using a default partitioner. However, this default partitioning strategy can lead to hash key collisions, which can impact the uniform distribution of data across partitions.
Understanding Kafka's Default Partitioner
When a producer sends a message to a Kafka topic, the message can optionally contain a key. The default partitioner uses this key to determine which partition the message should be sent to. If the message has a key, the partitioner computes the hash of the key and uses it to assign the message to a specific partition. If the message does not have a key, the partitioner assigns partitions in a round-robin fashion.
The hash function used in Kafka's default partitioner is a non-cryptographic hash (typically MurmurHash). This function maps keys to an integer space, and the modulus of this hash by the number of partitions determines the partition:
Hash Key Collisions
A hash key collision occurs when two different keys produce the same hash value, causing messages with these keys to consistently go to the same partition despite being distinct. While hash functions like MurmurHash are designed to minimize collisions, they are not entirely collision-free.
Causes of Hash Collisions
- Non-uniform Key Distribution: If the distribution of keys is not uniform, certain hash values might become more probable, leading to collisions.
- Limited Hash Space: The finite size of the hash output space compared to potentially larger key spaces also increases collision probability.
- Partition Count Changes: Changes in the number of partitions for a topic can also alter how keys map to partitions, leading potentially to increased collisions after re-partitioning.
Implications of Hash Key Collisions
- Performance Degradation: Collisions can lead to uneven load across partitions. Some partitions might become hotspots, which leads to unbalanced workload across the cluster.
- Skew in Data Processing: In streaming applications where partition keys relate to specific characteristics (like user IDs), collisions can lead to data skew, impacting the fairness and effectiveness of data processing.
Examples
Consider a scenario where a partition key is a user ID converted into a string, and two user IDs 12345 and 54321 might hash to the same partition. The following Java snippet gives an idea:
Mitigation Strategies
- Custom Partitioner: Implementing a custom partitioner that adapts to the specific characteristics of the key space can reduce the likelihood of collisions.
- Increasing Partitions: More partitions can reduce the probability of collisions but need careful management and planning.
- Using UUIDs: If feasible, using universally unique identifiers (UUIDs) as keys can lower collision rates due to their high entropy.
Summary Table
| Factor | Impact | Mitigation |
| Non-uniform Key Distribution | High Collision Probability | Custom Partitioner |
| Finite Hash Space | Inherent Collisions | Increase Partitions |
| Change in Partition Count | Redistribution Collisions | Application Specific Logic |
Conclusion
While Kafka's default partitioner efficiently handles key-based partitioning under typical conditions, understanding potential hash key collisions and preparing strategies to mitigate them is crucial for maintaining balanced partitions and optimal performance in a Kafka-based system. An awareness of key distribution, custom partition solutions, and careful system design can collectively address and reduce the likelihood and impact of collisions.
Related reading
- The group coordinator is not available-Kafka
- The kafka script kafka-consumer-group.sh throws timed out waiting for a node assignment
- Thrift serialization for kafka messages - single topic per struct
- TimeoutException Timeout expired while fetching topic metadata Kafka
- The process could not execute 'sp_replcmds' on 'database_name
- The relationship between Paxos family and data consistency
- TimeoutException Timeout expired while fetching topic metadata Kafka
- TLS-Encrypted Connection with RabbitMQ Using pika

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.