Reputation: 551
Lets say there is a topic in Apache Kafka
with 3 partitions. I need to run 3 consumers inside a consumer group and, according to the documentation, it means each consumer will read data from 1 partition.
Consumers are implemented using Spring Kafka
. As we all know, by default all messages are received in a single thread, but using ConcurrentMessageListenerContainer
should allow us to set up concurrency.
What I want? I want to use server CPU resources efficiently and make each consumer to receive and process messsages in separate threads (3 threads in our case, which is equal to the number of partitions).
As a result - 3 consumers (3 servers) in the consumer group and each consumer receives messages from all 3 partitions.
Is it possible? If yes, will it be enough if I just use ConcurrentMessageListenerContainer
and specify 3 listeners for each partition?
Upvotes: 2
Views: 1071
Reputation: 344
I was little confused by your statement. Just to clarify, in Kafka only one consumer can read from one partition within a consumer group. It is not possible for two consumers in same consumer group to read from same partition.
Within a consumer group,
this code snippet will read from topic named "mytopic" and it will use 3 thread to read from 3 partitions @KafkaListener(topics = "mytopic", concurrency = "3", groupId = "myconsumergroup")
Upvotes: 3