Kafka Consumer
Apache Kafka
Go Sarama
Programming
Offset Consumption

Kafka Consumer How to programatically consume from specific offset in Go Sarama

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Apache Kafka is a powerful distributed streaming platform capable of handling large volumes of data and allows for high-throughput data pipelines. One of Kafka's fundamental components is the consumer, which reads data from Kafka. Sarama, a Go library for Apache Kafka, provides an API for creating Kafka consumers in the Go programming language.

Understanding Kafka and Consumer Offsets

In Kafka, data within a topic is split across multiple partitions. Each message in a partition is assigned a sequential id called an offset. The consumer reads messages from a specific topic and partition at a given offset. Managing where a consumer starts reading from (e.g., an exact offset, the earliest available, the latest, etc.) is crucial for many applications, especially those dealing with reprocessing of data after a failure or update.

Programmatically Consuming From a Specific Offset Using Sarama

To consume messages from a specific offset, you'll need to utilize Sarama's Consumer API. Here is a step-by-step guide and an example to help you get started:

Required libraries

First, ensure you have Sarama installed:

bash
go get github.com/Shopify/sarama

Basic Configuration

You must configure the Sarama client. Minimal configuration would include setting the version of Kafka you are interfacing with and deciding on the required logging level.

go
config := sarama.NewConfig()
config.Version = sarama.V2_5_0_0 // adjust this to the version of your Kafka cluster
config.Consumer.Return.Errors = true

Creating the Consumer

You have to create a consumer client that connects to the Kafka brokers. Specify the brokers as a list of strings:

go
1brokers := []string{"localhost:9092"}
2consumer, err := sarama.NewConsumer(brokers, config)
3if err != nil {
4  log.Panic(err)
5}
6defer consumer.Close()

Consuming Messages from a Specific Offset

Choose the topic and partition you are interested in, and specify the offset from which you want to start consuming:

go
1topic := "your-topic"
2partition := int32(0) // Partition numbers start at 0
3offset := int64(10) // Change to the desired starting offset
4
5partitionConsumer, err := consumer.ConsumePartition(topic, partition, offset)
6if err != nil {
7  log.Panic(err)
8}
9defer partitionConsumer.Close()

Handling Messages

Now read messages in an endless loop (or any logic suitable for your application):

go
for message := range partitionConsumer.Messages() {
  fmt.Printf("Message claimed: value = %s, timestamp = %v, offset = %d\n", string(message.Value), message.Timestamp, message.Offset)
}

Error Handling

Make sure you handle errors appropriately, especially since you might encounter issues like the specified offset being out of range.

go
1go func() {
2    for err := range partitionConsumer.Errors() {
3        log.Printf("Error occurred at offset %d: %v\n", err.Offset, err.Err)
4    }
5}()

Summary Table

Here is a summary of the key steps involved in setting up a Kafka consumer with Sarama to consume from a specific offset.

StepAction
1Install and import Sarama library.
2Configure Kafka client.
3Create a Kafka consumer client.
4Specify the topic, partition, and offset.
5Start consuming messages from the specified offset.
6Handle incoming messages and potential errors.

Additional Considerations

  • Consumer Group Management: If you are part of a consumer group, the OffsetManager API of Sarama can be utilised for more complex scenarios like committing offsets and handling rebalances.
  • Performance Tuning: Optimize the performance by tweaking configurations like config.ChannelBufferSize.
  • Security: If your Kafka broker setup involves security (like SASL/SSL), make sure the Sarama configuration reflects these settings.

Using this method, your application can have fine-grained control over the consumption of messages from Kafka, which is particularly useful in scenarios that require precise replay or audit capabilities. This approach empowers developers to build robust, resilient Kafka clients in Go.


Course illustration
Course illustration

All Rights Reserved.