Sobes.tech
Junior — Middle

How is a consumer group implemented in Apache Kafka?

sobes.tech AI

Answer from AI

In Apache Kafka, a consumer group is implemented as a set of consumers that jointly read data from topics, distributing partitions among themselves. Each consumer in the group receives a unique set of partitions, ensuring parallel message processing without duplication.

When using Go (Golang) with the sarama or confluent-kafka-go library, you can create a consumer group by specifying the same group.id. Kafka will automatically distribute partitions among group members and track offsets.

Example using sarama:

config := sarama.NewConfig()
config.Version = sarama.V2_1_0_0

consumerGroup, err := sarama.NewConsumerGroup([]string{"localhost:9092"}, "my-group", config)
if err != nil {
    panic(err)
}

topics := []string{"my-topic"}

// Implement the ConsumerGroupHandler interface
handler := &MyConsumerGroupHandler{}

for {
    err := consumerGroup.Consume(ctx, topics, handler)
    if err != nil {
        log.Fatal(err)
    }
}

Thus, a consumer group allows scaling message processing and provides load balancing among instances.

How is a consumer group implemented in Apache Kafka… - sobes.tech