how to properly register Protobuf schema with Schema Registry / Kafka-Rest
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 that allows you to publish, subscribe to, and process streams of records. As data flows through Kafka, ensuring that the data adheres to a consistent structure can be crucial. This is where the combination of Protocol Buffers (Protobuf) and Schema Registry comes into play.
What are Protocol Buffers and Schema Registry?
Protocol Buffers (Protobuf) is a language-neutral, platform-neutral, extensible mechanism for serializing structured data, similar to XML but smaller, faster, and simpler. It allows you to define how you want your data structure to be, generates source code in various programming languages to easily write and read structured data to and from a variety of data streams and using a variety of languages.
Schema Registry stores a versioned history of all schemas based on a specified subject name strategy, provides multiple compatibility settings, and allows evolution of schemas according to the configured compatibility settings and expanded Avro support.
Why Use Protobuf with Kafka and Schema Registry?
Integrating Protobuf with Kafka and Schema Registry enables:
- Strong Data Typing: Protobuf ensures that the data conforms to a predefined schema, reducing errors.
- Efficiency: Protobuf data uses binary serialization, which is more compact and faster to transmit.
- Schema Evolution: Schema Registry helps in managing changes to the schema without breaking downstream systems.
Setting Up Protobuf with Schema Registry in Kafka
Requirements
- Apache Kafka installation
- Confluent Schema Registry (part of the Confluent platform)
- Protobuf compiler (
protoc) - Kafka Protobuf Serializer and Deserializer Libraries
Steps to Register Protobuf Schema
- Define Your Protobuf Schema: Create your
.protofile. This defines the structure of your data.
- Compile Your Protobuf Schema: Use the
protoccompiler to generate language-specific code.
- Set Up the Kafka Producer: Configure your Kafka Producer to use the Protobuf serializer. This configuration can be done in Java as follows:
- Produce Messages: Create and send Protobuf messages using the Kafka producer.
- Set Up the Kafka Consumer: Similarly, configure your Kafka Consumer to use the Protobuf deserializer.
- Consume Messages: Read the Protobuf messages from Kafka.
Summary Table
| Step | Description | Tools/Commands |
| Define Protobuf Schema | Create .proto file to define data structure | user.proto |
| Compile Protobuf Schema | Generate language-specific code | protoc --java_out=. user.proto |
| Configure Kafka Producer | Set serializer and schema registry URL | Kafka Producer properties |
| Produce Messages | Send messages to Kafka | Kafka Producer code |
| Configure Kafka Consumer | Set deserializer and schema registry URL | Kafka Consumer properties |
| Consume Messages | Receive messages from Kafka | Kafka Consumer code |
Additional Considerations
Schema Compatibility: Schema Registry supports various compatibility settings (e.g., BACKWARD, FORWARD, FULL). Choose an appropriate compatibility mode based on how you intend to evolve your schema.
Security: Always secure your Schema Registry using SSL and Access Control Lists (ACLs) if it is available to multiple teams or externally.
Managing Multiple Schemas: It is common to have multiple topics with different schemas. Design your naming strategy for schemas and topics in such a way that they are clear and consistent.
Using Protobuf with Kafka and Schema Registry can significantly improve the robustness and maintainability of data streaming applications, ensuring that data conforms to predetermined structures and is compatible across different applications and systems.

