Apache Kafka with Avro and Schema Repo - where in the message does the schema Id 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 event streaming platform capable of handling trillions of events a day. Initially conceived as a messaging queue, Kafka is based on an abstraction of a distributed commit log. Since it deals with streams of data, ensuring data integrity and compatibility between the producer (data sender) and consumer (data receiver) is crucial. This is where Apache Avro and Schema Repositories come into play, particularly in managing the evolution of data schemas.
Understanding Apache Avro
Apache Avro is a data serialization system that provides rich data structures and a compact, fast, binary data format. The primary use of Avro is in the context of big data, particularly with Apache Kafka, Apache Cassandra, and Apache Hadoop. Avro's key features include:
- Compactness: Avro uses a binary serialization format which makes it highly efficient and compact, reducing the cost of storage and improving IO performance.
- Interoperability: Avro supports schema evolution - producers and consumers using different versions of the schema can seamlessly interact.
- Dynamic typing: Avro does not require code generation. It uses a JSON format to declare schemas which can be processed on the fly.
Schema Management in Kafka with Avro
In Kafka, data producers send messages to Kafka topics. Each message in Kafka can be a key-value pair, and both keys and values can be serialized using Avro. To maintain schema consistency, a Schema Repository is used. It serves as a central store where all Avro schemas are registered. The most common Schema Repositories compatible with Kafka include:
- Confluent Schema Registry
- Apache's own Kafka Schema Registry
Integrating Schema Repo with Apache Kafka
When Avro is used with Kafka, the typical setup involves storing only the data serialized with Avro on the Kafka topic, while the schema itself is registered and stored in the Schema Repository. The message itself contains only the schema ID rather than the full schema, making the system more efficient:
- Schema Registration: Whenever a new schema is introduced, it is registered in the Schema Registry. This registration process returns a unique schema ID.
- Message Serialization: Each message serialized via Avro references this schema ID usually at the beginning of the message. This ID is then used by consumers to retrieve the schema from the repository.
- Consumer Deserialization: Consumers read the schema ID from the message, retrieve the corresponding schema from the Schema Repository, and deserialize the message using Avro.
Working Example
Here is a simplified example to illustrate this process. Assume a producer sends user data:
- This data structure is first defined as an Avro schema (let's call it Schema V1) and registered in the Schema Repository gaining a unique ID, say
1. - The producer serializes the user data using Avro with Schema V1 and prefixes the serialized data with the schema ID.
- The message looks like this:
[schema ID][data]->[1][...binary data...]. - Consumers read the message, extract the schema ID
1, fetch Schema V1 from the Schema Repository, and deserialize the data.
Benefits of Using Avro with Kafka and a Schema Repo
This approach has several advantages:
- Efficiency: Storing only the schema ID in each message considerably reduces the message size.
- Flexibility: Producers and consumers can evolve independently, using different versions of the schema without disruption.
- Consistency: Centralized schema management ensures all participants in the ecosystem use a consistent data structure.
Summary Table
| Feature | Description |
| Data Compactness | Binary serialization reduces the size and increases efficiency. |
| Schema Evolution | Allows schema changes without breaking deployed systems. |
| Messaging Efficiency | Storing schema ID instead of full schema reduces message size. |
| Centralized Management | Schemas are centrally managed and versioned in Schema Repo. |
| Decoupling | Producers and consumers can operate independently with different schema versions. |
Conclusion
Using Avro with Apache Kafka and a Schema Repository provides a powerful mechanism for efficient and reliable data streaming with rigorous schema management and versioning. This combination is pivotal in systems where data integrity and compatibility over time are critical, as in most real-time data processing pipelines in distributed systems.
Related reading
- Apache Pulsar vs. Apache RocketMQ
- Apache Pulsar vs Kafka - do consumers pull (poll) messages off the topics?
- Apache Spark-Kafka.TaskCompletionListenerException & KafkaRDD$KafkaRDDIterator.close NPE on local cluster(Client Mode)
- Apache Spark Getting a InstanceAlreadyExistsException when running the Kafka producer
- Apply .gitignore on an existing repository already tracking large number of files
- Applying a git post-commit hook to all current and future repositories
- apache spark streaming - kafka - reading older messages
- Apache storm using Kafka Spout gives error IllegalStateException

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.