Apache Kafka
Avro
Schema Repository
Schema Id
Message Structure

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.

Practice system design

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:

  1. Compactness: Avro uses a binary serialization format which makes it highly efficient and compact, reducing the cost of storage and improving IO performance.
  2. Interoperability: Avro supports schema evolution - producers and consumers using different versions of the schema can seamlessly interact.
  3. 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:

  1. Schema Registration: Whenever a new schema is introduced, it is registered in the Schema Registry. This registration process returns a unique schema ID.
  2. 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.
  3. 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:

json
1{
2  "name": "Jane Doe",
3  "email": "[email protected]"
4}
  1. 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.
  2. The producer serializes the user data using Avro with Schema V1 and prefixes the serialized data with the schema ID.
  3. The message looks like this: [schema ID][data] -> [1][...binary data...].
  4. 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

FeatureDescription
Data CompactnessBinary serialization reduces the size and increases efficiency.
Schema EvolutionAllows schema changes without breaking deployed systems.
Messaging EfficiencyStoring schema ID instead of full schema reduces message size.
Centralized ManagementSchemas are centrally managed and versioned in Schema Repo.
DecouplingProducers 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
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