Consume Kafka Avro messages in go
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Apache Kafka is a popular distributed streaming platform used for building real-time data pipelines and streaming applications. It provides high-throughput, built-in partitioning, replication, and fault tolerance. Kafka uses a binary protocol over TCP, which supports a variety of serialization formats, including Apache Avro.
Apache Avro is a serialization framework that integrates well with Kafka. It uses JSON for defining data structures and protocols, and serializes data in a compact binary format. This makes it highly suitable for Kafka messages due to its efficiency both in terms of storage and speed.
Why Use Avro with Kafka?
- Schema Evolution: Avro supports the evolution of schemas over time. This allows you to update the schema used to write data, without breaking systems that read the data.
- Strong Typing: Avro data is always read with its schema, which facilitates strong typing and schema validation.
- Compact Data: The binary format of Avro uses less space and incurs less overhead when transported or stored.
Setting Up a Go Application to Consume Avro Messages from Kafka
Here’s how you can set up a Go application to consume data serialized in Avro format from a Kafka topic.
Prerequisites
- Kafka broker running
- Avro schema
- Go environment setup
Dependencies
First, install the necessary Go packages. We'll use confluent-kafka-go for Kafka interaction and go-avro for Avro serialization.
Consuming Kafka Avro Messages
Here’s a basic example:
This code does the following:
- Defines an Avro schema inline.
- Initializes a Kafka consumer with a specified topic.
- Continuously reads messages and unmarshals them using the Avro schema.
Table Summary
| Feature | Details |
| Serialization | Avro |
| Language | Go |
| Library | confluent-kafka-go, hamba/avro |
| Kafka Topic | Configurable |
| Schema Evolution | Supported by Avro |
| Error Handling | Basic logging of deserialization and Kafka errors |
Advanced Concepts
Schema Registry
In a production environment, managing Avro schemas directly in your application code can become cumbersome. A schema registry, which provides a RESTful interface for storing and retrieving Avro schemas, simplifies this process. Confluent's Schema Registry is a popular choice.
Handling Offsets
Fine control over how and where from messages are read in Kafka can significantly affect the performance of your application. It's important to handle consumer offsets carefully to ensure no data loss during failures.
Conclusion
Consuming Kafka Avro messages in Go involves parsing the Avro schema, setting up a Kafka consumer, and then deserializing the messages into Go datatypes as they are read from the topics. Kafka and Avro form a powerful combination for robust, scalable, and efficient applications, allowing developers to handle complex data structures and large scales of data efficiently.
Related reading
- Consume Kafka messages from a different container in python
- Consume kafka messages from django app
- Consume messages in batches - RabbitMQ
- Consume messages without committing from Kafka 10 consumer
- Consumer Connection error with django and celery+rabbitmq?
- Consumer group has no active members - Kafka
- Consumer Id and Group Id in Kafka what makes two consumers the same
- Consumer not receiving messages, kafka console, new consumer api, Kafka 0.9

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack 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.