Protobuf
Schema Registry
Kafka-Rest
Data Serialization
Stream Processing

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

  1. Define Your Protobuf Schema: Create your .proto file. This defines the structure of your data.
protobuf
1   syntax = "proto3";
2
3   package com.example;
4
5   message User {
6     int32 id = 1;
7     string name = 2;
8   }
  1. Compile Your Protobuf Schema: Use the protoc compiler to generate language-specific code.
bash
   protoc --java_out=. user.proto
  1. Set Up the Kafka Producer: Configure your Kafka Producer to use the Protobuf serializer. This configuration can be done in Java as follows:
java
   props.put("key.serializer", "org.apache.kafka.common.serialization.StringSerializer");
   props.put("value.serializer", "io.confluent.kafka.serializers.protobuf.KafkaProtobufSerializer");
   props.put("schema.registry.url", "http://localhost:8081");
  1. Produce Messages: Create and send Protobuf messages using the Kafka producer.
java
   User user = User.newBuilder().setId(1).setName("John Doe").build();
   producer.send(new ProducerRecord<String, User>("your-topic", user));
  1. Set Up the Kafka Consumer: Similarly, configure your Kafka Consumer to use the Protobuf deserializer.
java
   props.put("key.deserializer", "org.apache.kafka.common.serialization.StringDeserializer");
   props.put("value.deserializer", "io.confluent.kafka.serializers.protobuf.KafkaProtobufDeserializer");
   props.put("schema.registry.url", "http://localhost:8081");
  1. Consume Messages: Read the Protobuf messages from Kafka.

Summary Table

StepDescriptionTools/Commands
Define Protobuf SchemaCreate .proto file to define data structureuser.proto
Compile Protobuf SchemaGenerate language-specific codeprotoc --java_out=. user.proto
Configure Kafka ProducerSet serializer and schema registry URLKafka Producer properties
Produce MessagesSend messages to KafkaKafka Producer code
Configure Kafka ConsumerSet deserializer and schema registry URLKafka Consumer properties
Consume MessagesReceive messages from KafkaKafka 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.


Course illustration
Course illustration

All Rights Reserved.