Kafka
Partitioner Class
Message Assignment
Key Usage
Topic Partitioning

KafKa partitioner class, assign message to partition within topic using key

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Apache Kafka is a distributed streaming platform capable of handling trillions of events a day. Initially conceived as a messaging queue, Kafka is based on an abstraction of a distributed commit log. Since Kafka is a distributed system, topics are partitioned and replicated across multiple nodes.

What is a Kafka Partitioner?

A Kafka Partitioner is responsible for determining which partition within a Topic a message is sent to. When producing a message to a Kafka topic, you can specify a key. The partitioner uses this key to assign the message to a specific partition, ensuring that all messages with the same key end up in the same partition. If no key is provided, the partitioner assigns messages in a round-robin fashion to balance the load across partitions.

Why Partition Messages?

Partitioning messages in Kafka has several benefits:

  • Scalability: Partitioning allows Kafka to scale as more partitions can be spread over multiple brokers.
  • Ordering: Within a partition, Kafka guarantees that messages are in the order they were received. This is crucial for certain use cases such as financial transactions or log monitoring.
  • Parallel Processing: Multiple partitions allow for parallel processing of data, which can significantly boost performance.

How Does the Partitioner Work?

When a message is produced, the producer specifies a topic and optionally a key. The default partitioner provided by Kafka works as follows:

  1. If a key is specified: The partitioner applies a hash function to the key and uses this result to determine the partition.
  2. If no key is specified: The messages are assigned in a round-robin manner to balance the load across available partitions.

Custom Partitioner

To implement custom logic for partition assignment, you must implement the org.apache.kafka.clients.producer.Partitioner interface, which includes three methods:

  • configure(Map<String, ?> configs): This method allows you to retrieve any necessary configuration.
  • partition(String topic, Object key, byte[] keyBytes, Object value, byte[] valueBytes, Cluster cluster): This method contains the core logic for assigning a partition based on the key.
  • close(): This is used to clean up resources.

Example of a Custom Partitioner:

Here's an example of how one might implement a custom partitioner that assigns messages to partitions based on the hash code of the key modulo the number of partitions.

java
1public class CustomPartitioner implements Partitioner {
2    private String speedSensorName;
3
4    @Override
5    public void configure(Map<String, ?> configs) {
6        speedSensorName = configs.get("speed.sensor.name").toString();
7    }
8
9    @Override
10    public int partition(String topic, Object key, byte[] keyBytes, Object value, byte[] valueBytes, Cluster cluster) {
11        List<PartitionInfo> partitions = cluster.partitionsForTopic(topic);
12        int numPartitions = partitions.size();
13        
14        // If the key is empty, return random partition
15        if (keyBytes == null) {
16            return ThreadLocalRandom.current().nextInt(numPartitions);
17        }
18        
19        // If the message is from speed sensor, send it to the last partition
20        if (key.equals(speedSensorName)) {
21            return numPartitions - 1;
22        }
23        
24        // Other sensor messages are assigned evenly to the partitions
25        return Math.abs(Arrays.hashCode(keyBytes)) % numPartitions;
26    }
27
28    @Override
29    public void close() {
30    }
31}

In this example, we provide special handling for messages from a specific "speed sensor," which always go to the last partition, while other messages are distributed based on their key hash.

Summary Table

FeatureDescription
ScalabilityKafka partitions enhance scalability by spreading data across brokers.
OrderingMessages in the same partition are guaranteed to be in order.
Parallel ProcessingDifferent partitions can be read in parallel, increasing throughput.
Default PartitioningIf no key is specified, messages are assigned round-robin.
Custom Partitioning LogicAllows implementing complex logic based on scenario needs.

Conclusion

Kafka partitions are a powerful feature for managing data distribution and achieving high throughput and scalability. By using or customizing partitioners, developers can control how messages are distributed across partitions to meet specific requirements of their application’s architecture and performance needs.


Course illustration
Course illustration

All Rights Reserved.