Junior — Middle
Can you explain the concept of data partitioning in Kafka and how it is implemented?
sobes.tech AI
Answer from AI
In Apache Kafka, data is organized into topics, which are further divided into partitions. Partitioning allows for scaling message processing and storage.
Each partition is an ordered, immutable log of messages that can be accessed by offset. Messages with the same key are guaranteed to go to the same partition, ensuring order processing for that key.
Implementation:
- When sending a message, the producer specifies a key.
- Kafka uses a partitioning function (usually hashing the key) to select a partition.
- Messages without a key are distributed cyclically (round-robin).
This partitioning allows:
- Parallel reading and writing.
- Ensuring message order with the same key.
- Scaling the system by adding new partitions and brokers.
Example in Java (sending a message with a key):
ProducerRecord<String, String> record = new ProducerRecord<>("topicName", "key1", "message");
producer.send(record);