Sobes.tech
Junior — Middle

Are you ready to set up two consumers for one topic so that they work without interruptions, specifying specific partitions for each?

sobes.tech AI

Answer from AI

To configure two consumers in Java that read from the same Kafka topic and operate without interruptions, you can explicitly assign each of them specific topic partitions. This is called manual partition assignment.

Example using KafkaConsumer:

Properties props = new Properties();
props.put("bootstrap.servers", "localhost:9092");
props.put("group.id", "my-group");
props.put("key.deserializer", "org.apache.kafka.common.serialization.StringDeserializer");
props.put("value.deserializer", "org.apache.kafka.common.serialization.StringDeserializer");

KafkaConsumer<String, String> consumer1 = new KafkaConsumer<>(props);
KafkaConsumer<String, String> consumer2 = new KafkaConsumer<>(props);

// Assign specific partitions to each consumer
TopicPartition partition0 = new TopicPartition("my-topic", 0);
TopicPartition partition1 = new TopicPartition("my-topic", 1);

consumer1.assign(Collections.singletonList(partition0));
consumer2.assign(Collections.singletonList(partition1));

// Now consumer1 reads only from partition 0, and consumer2 from partition 1

This approach guarantees that each consumer processes its own partition, and there will be no conflicts or message duplication between them. It is important that the number of consumers does not exceed the number of partitions, otherwise some consumers will be left without data.

Are you ready to set up two consumers for one topic… - sobes.tech