Kafka
Consumer Group
Golang
Programming
Software Development

How to create a kafka consumer group in Golang?

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

Apache Kafka is a popular distributed messaging system that provides efficient, reliable queuing of messages between microservices, data centers, and workloads. In Kafka, the consumers of messages subscribe to topics and consume the published messages by pulling data from the brokers. When multiple consumers are subscribed to a topic and belong to the same consumer group, each consumer within the group reads from a unique partition, which enhances scalability and speed.

Understanding Kafka Consumer Groups

A consumer group in Kafka is a collection of consumers that jointly consume data from one or more topics. The main idea is to allow a pool of processes to cooperate in the consumption of data to achieve parallel processing. Each consumer in the group reads from exclusive partitions of the topic, ensuring efficient distribution of processing.

Setting Up Kafka and Golang Environment

Before you begin creating Kafka consumers in Golang, ensure you have both Kafka and Golang installed:

  1. Apache Kafka: Install Apache Kafka and make sure it is running. You can download it from https://kafka.apache.org/downloads.
  2. Go Environment: Install Go (any latest version) from https://golang.org/dl/ and set up the environment.

Integrating Kafka with Golang

In Golang, the sarama library is widely used to interact with Kafka. It provides straightforward ways to create producers and consumers, supports Kafka 0.8 and newer, and is highly configurable.

Installing Sarama

To install the Sarama library, use go get:

bash
go get github.com/Shopify/sarama

Creating a Kafka Consumer Group in Golang

We will now walk through the steps to create a consumer group in Golang using the Sarama library.

Step 1: Create a Consumer Group Handler

First, define a struct that implements the ConsumerGroupHandler interface provided by Sarama. This interface requires you to implement three methods: Setup, Cleanup, and ConsumeClaim.

go
1package main
2
3import (
4    "github.com/Shopify/sarama"
5    "log"
6)
7
8type ExampleConsumerGroupHandler struct{}
9
10func (ExampleConsumerGroupHandler) Setup(_ sarama.ConsumerGroupSession) error   { return nil }
11func (ExampleConsumerGroupHandler) Cleanup(_ sarama.ConsumerGroupSession) error { return nil }
12func (h ExampleConsumerGroupHandler) ConsumeClaim(session sarama.ConsumerGroupSession, claim sarama.ConsumerGroupClaim) error {
13    for message := range claim.Messages() {
14        log.Printf("Message claimed: value = %s, timestamp = %v, topic = %s", string(message.Value), message.Timestamp, message.Topic)
15        session.MarkMessage(message, "")
16    }
17    return nil
18}

Step 2: Initialize and Consume Messages

Once the handler is set up, create a consumer group, connect it to Kafka brokers, and subscribe to topics.

go
1func main() {
2    config := sarama.NewConfig()
3    config.Version = sarama.V2_5_0_0 // specify appropriate version
4    config.Consumer.Return.Errors = true
5    brokers := []string{"localhost:9092"}
6
7    // Create new consumer group
8    group, err := sarama.NewConsumerGroup(brokers, "my_consumer_group", config)
9    if err != nil {
10        panic(err)
11    }
12    defer func() { _ = group.Close() }()
13
14    // Track errors
15    go func() {
16        for err := range group.Errors() {
17            log.Println("ERROR", err)
18        }
19    }()
20
21    // Consume messages
22    ctx := context.Background()
23    for {
24        topics := []string{"my_topic"}
25        handler := ExampleConsumerGroupHandler{}
26        err := group.Consume(ctx, topics, handler)
27        if err != nil {
28            panic(err)
29        }
30    }
31}

Summary Table of Key Components

ComponentDescription
ConsumerGroupHandlerInterface to handle consumer functions like Setup, Cleanup, Consume.
ConsumeClaimMethod where actual message consumption takes place.
NewConsumerGroupFunction to create a new consumer group.
group.ConsumeMethod to start consuming with specified topics and handler.

Additional Details

Handling failures and implementing retries can add resilience to your Kafka consumer group. Sarama also provides a way to handle offsets, which can be crucial for ensuring that messages are not lost or consumed multiple times.

The scalability of a Kafka consumer group can be influenced by the number of partitions of a topic. More partitions allow more consumers in a group to read data in parallel, thus improving the throughput.

By leveraging consumer groups and distributing the load among multiple consumers, Kafka enables large-scale, high-throughput message consumption that is crucial for many high-performance applications.


Related reading
Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

All Rights Reserved.