How to Create Consumer-Group Consumer in Golang With Kafka

Consumer group is a set of consumers. Each consumer group is a subscriber to one or more kafka topics.

There is a Kafka library for golang call sarama. I’m using sarama library to build the producer. Read more about sarama library from here.

Consumer group is a set of consumers which has a unique group id. Each consumer group is a subscriber to one or more kafka topics. Each consumer group maintains its offset per topic partition. A record gets delivered to only one consumer in a consumer group. Each consumer in a consumer group processes records and only one consumer in that group will get the same record. Consumers in a consumer group load balance record processing.
This is slightly more complicated, as we want to have our consumer to be a member of a consumer group.

We need to create client and then we initialize consumer group where we create claims and wait for message channel to receive message.

Code is simple and self explanatory. What I’m doing here is initializing ConsumerGroup for given topic .
Running consumer:
$ go run main.go -brokers="" -topics="sarama" -group="example"

. . .


This example shows you how to use the Sarama consumer group consumer. The example simply starts consuming the given Kafka topics and logs the consumed messages.

On a mission to build Next-Gen Community Platform for Developers