Search code examples
javaapache-kafkamicroserviceskafka-consumer-api

Partition structure for 2 kafka consumers on same topic


If I create

  • 2 kafka consumer instances
  • passing same properties
  • subscribe on same topic

Will these 2 Consumer instances (at diff group Id), have similar partition structures, or could be different ?

i.e, if I do .assignment() will I get same result at both


My actual problem statement, where I will be using this validation

In my application, I am attaining offset of broker, at a particular state(This is being done through my 1st kafka consumer object).

Later, I am creating the 2nd kafka consumer object, and using this to iterate over the topic, seeking from the earlier offset attained.

(So if the supposition mentioned at question is false, my logic will fail)


Solution

  • Let me clear this up.

    Kafka has topics, and the consumers can subscribe to them. Each topic has partitions (which you can define when you create them). When there are multiple partitions for a given topic, each topic partition is assigned to a consumer in a consumer group. If you have more consumers than the number of partitions, those extra consumers become idle.

    If you want your two Kafka consumers to consume messages separately, you have to add them into two different consumer groups. If you have a single Kafka consumer within a Consumer group, all the partitions are assigned to that consumer.

    So if you want to get the same result for the two consumers, you may add them into two different consumer groups.