I need to call Kafka consumer in publish/subscribe mode 1000 times. As far as I know for kafka to work in pub/subscribe mode I need to give a new groupId to each consumer( props.put("group.id", String.valueOf(Instant.now().toEpochMilli()));). But when I do this if two consumer threads access consumer at the same millisecond there will be problems. How should this problem be solved?
1 Answers
If you want to spread the messages across the consumers you need to use the same group.id
. If you have 1000 messages and 1000 consumers, then each of the consumer will normally consume one message.
On the other hand, if you want each of the consumer to consume all the messages from the topics, you need to use a different group.id
so that the messages in the topic are consumed by all consumers. If you have a huge number of consumers you can use UUID.randomUUID().toString()
in order to produce a distinct group.id
for each one.
According to the docs:
Consumers label themselves with a consumer group name, and each record published to a topic is delivered to one consumer instance within each subscribing consumer group. Consumer instances can be in separate processes or on separate machines.
If all the consumer instances have the same consumer group, then the records will effectively be load balanced over the consumer instances.
If all the consumer instances have different consumer groups, then each record will be broadcast to all the consumer processes.
UUID.randomUUID().toString()
– Katya Gorshkova