Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How multiple consumer group consumers work across partition on the same topic in Kafka?

I was reading this SO answer and many such blogs.

What I know:

Multiple consumers can run on a single partition when running multiple consumers with multiple consumer group id and only one consumer from a consumer group can consume at a given time from a partition.

My question is related to multiple consumers from multiple consumer groups consuming from the same topic:

  1. What happens in the case of multiple consumers(different groups) consuming a single topic(eventually the same partition)?

  2. Do they get the same data?

  3. How offset is managed? Is it separate for each consumer?

  4. (Might be opinion based) How do you or generally recommended way is to handle overlapping data across two consumers of a separate group operating on a single partition?

Edit: "overlapping data": means two consumers of separate consumer groups operating on the same partition getting the same data.

like image 398
Amit Tripathi Avatar asked Jul 18 '17 19:07

Amit Tripathi


People also ask

Can multiple consumers consume the same message Kafka?

The same message can be consumed several times, either by different consumers, or indeed by the same consumer multiple times. Kafka uses the concepts of topics and partitions, where partitions are the main mechanism used to achieve its impressive scalability characteristics.

How does Kafka handle multiple consumers?

Kafka consumers are typically part of a consumer group . When multiple consumers are subscribed to a topic and belong to the same consumer group, each consumer in the group will receive messages from a different subset of the partitions in the topic.

How does Kafka consumer read from multiple partitions?

The consumers in a group divide the topic partitions as fairly amongst themselves as possible by establishing that each partition is only consumed by a single consumer from the group. When the number of consumers is lower than partitions, same consumers are going to read messages from more than one partition.

What happens if you have more consumers than partitions?

You can have fewer consumers than partitions (in which case consumers get messages from multiple partitions), but if you have more consumers than partitions some of the consumers will be “starved” and not receive any messages until the number of consumers drops to (or below) the number of partitions.


1 Answers

  1. Yes they get the same data. Kafka only stores one copy of the data in the topic partitions' commit log. If consumers are not in the same group then they can each get the same data using fetch requests from the clients' consumer library. The assignment of which partitions each group member will get is managed by the lead consumer of each group. The entire process in detailed steps is documented here https://community.hortonworks.com/articles/72378/understanding-kafka-consumer-partition-assignment.html

  2. Offsets are "managed" by the consumers, but "stored" in a special __consumer_offsets topic on the Kafka brokers.

  3. Offsets are stored for each (consumer group, topic, partition) tuple. This combination is also used as the key when publishing offsets to the __consumer_offsets topic so that log compaction can delete old unneeded offset commit messages and so that all offsets for the same (consumer group, topic, partition) tuple are stored in the same partition of the __consumer_offsets topic (which defaults to 50 partitions)

like image 197
Hans Jespersen Avatar answered Oct 15 '22 02:10

Hans Jespersen