Middle
Kafka: how to make sure that a message from a topic is read by two different services?
sobes.tech AI
Answer from AI
For two different services to read the same message from a Kafka topic, these services need to be in different consumer groups. In Kafka, messages are delivered independently to each consumer group, and within a group, each message is assigned to one consumer.
Example:
- Service A — consumer group "serviceA-group"
- Service B — consumer group "serviceB-group"
Both services subscribe to the same topic, but since they are in different groups, each will receive all messages from the topic.
Properties props = new Properties();
props.put("bootstrap.servers", "localhost:9092");
props.put("group.id", "serviceA-group"); // or "serviceB-group" for the second service
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(Collections.singletonList("your-topic"));
// further message processing
Thus, each service receives a copy of all messages from the topic.