Junior — Middle
How do topics in Kafka interact with subscribers, and how is message exchange carried out between them?
sobes.tech AI
Answer from AI
In Kafka, topics serve as channels for message exchange. Producers publish messages to topics, and subscribers (consumers) read these messages. Each topic is divided into partitions, which allows for scalable processing.
Subscribers can be organized into consumer groups. Each message from a partition is delivered to only one consumer in the group, ensuring load balancing. Message exchange occurs asynchronously: the producer sends a message to the topic, Kafka stores it, and consumers read according to their schedule.
Example in Java using Kafka Consumer:
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> consumer = new KafkaConsumer<>(props);
consumer.subscribe(Arrays.asList("my-topic"));
while (true) {
ConsumerRecords<String, String> records = consumer.poll(Duration.ofMillis(100));
for (ConsumerRecord<String, String> record : records) {
System.out.printf("Received message: key = %s, value = %s, offset = %d\n", record.key(), record.value(), record.offset());
}
}
Thus, topics and subscribers provide an efficient and scalable message exchange in Kafka.