Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

If you have less consumers than partitions, what happens?

Tags:

apache-kafka

If you have less consumers than partitions, does that simply mean you will not consume all the messages on a given topic?

In a cloud environment, how are you suppose to keep track how many consumers are running and how many are pointing to a given topic#partition?

What if you have multiple consumers on a given topic#partition? I guess the consumer has to somehow keep track of what messages it has already processed in case of duplicates?

like image 386
cool breeze Avatar asked Nov 30 '16 18:11

cool breeze


People also ask

What happens if there are more partitions than consumers?

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.

What happens if there are more consumers than partitions in Kafka?

More consumers in a group than partitions means idle consumers. The main way we scale data consumption from a Kafka topic is by adding more consumers to a consumer group. It is common for Kafka consumers to do high-latency operations such as write to a database or a time-consuming computation on the data.

Can we have more consumers than partitions in Kafka?

A consumer can be assigned to consume multiple partitions. So the rule in Kafka is only one consumer in a consumer group can be assigned to consume messages from a partition in a topic and hence multiple Kafka consumers from a consumer group can not read the same message from a partition.

How are partitions assigned to consumers?

However, all partitions are assigned to a single consumer at a time. If that consumer fails or is stopped then partitions are all assigned to the next available consumer. Usually, partitions are assigned to the first consumer but for our example we will attach a priority to each of our instance.


1 Answers

In fact, each consumer belongs to a consumer group. When Kafka cluster sends data to a consumer group, all records of a partition will be sent to a single consumer in the group.

If there're more paritions than consumers in a group, some consumers will consume data from more than one partition. If there're more consumers in a group than paritions, some consumers will get no data. If you add new consumer instances to the group, they will take over some partitons from old members. If you remove a consumer from the group (or the consumer dies), its partition will be reassigned to other member.

Now let's take a look at your questions:

If you have less consumers than partitions, does that simply mean you will not consume all the messages on a given topic?

NO. Some consumers in the same consumer group will consume data from more than one partition.

In a cloud environment, how are you suppose to keep track how many consumers are running and how many are pointing to a given topic#partition?

Kafka will take care of it. If new consumers join the group, or old consumers dies, Kafka will do reblance.

What if you have multiple consumers on a given topic#partition?

You CANNOT have multiple consumers (in a consumer group) to consume data from a single parition. However, if there're more than one consumer group, the same partition can be consumed by one (and only one) consumer in each consumer group.

like image 71
for_stack Avatar answered Sep 23 '22 14:09

for_stack