KafkaAvroSerializer
Avro Serialization
Schema Registry
Data Streaming
Apache Kafka

KafkaAvroSerializer for serializing Avro without schema.registry.url

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 and Avro are widely used technologies in the big data ecosystem, recognized for their capabilities to handle large-scale, real-time data streaming and structured data serialization, respectively. Kafka's integration with Avro serialization typically involves a schema registry to manage the versioning of Avro schemas, ensuring compatibility and integrity of data between producers and consumers. However, it is sometimes desirable or necessary to serialize Avro data without a direct dependency on a schema registry. To address this, one can use KafkaAvroSerializer in a slightly different configuration.

Understanding KafkaAvroSerializer

KafkaAvroSerializer is a part of the Confluent schema registry client, which is generally used together with Confluent's Schema Registry. This serializer allows Kafka producers to send data in Avro format. It typically requires configuration with a schema.registry.url to manage schema validation, storage, and retrieval.

However, using Avro with Kafka doesn't inherently require a Schema Registry. The primary purpose of a Schema Registry is to manage schema versions and ensure that the message producers and consumers are using compatible schema versions.

Serializing Avro Without schema.registry.url

To serialize Avro data without connecting to a Schema Registry, you must handle schemas explicitly within your application code. Here is a general approach:

  1. Embed the Schema in the Producer: Include your Avro schema directly in your producer application. This schema will be used to serialize the data into Avro before sending it to a Kafka topic.
  2. Carefully Manage Schema Evolution: Without a Schema Registry, you must ensure that any changes in schema are backward or forward compatible from your application side. Any change to the schema should be well-coordinated with both producers and consumers to prevent deserialization failures.
  3. Custom Serialization Logic: Implement custom logic or use Avro's GenericRecord to manage serialization without relying on schema registry specifics.

Example Code for Kafka Producer using KafkaAvroSerializer

java
1import org.apache.avro.Schema;
2import org.apache.avro.generic.GenericData;
3import org.apache.avro.generic.GenericRecord;
4import org.apache.kafka.common.serialization.Serializer;
5import io.confluent.kafka.serializers.KafkaAvroSerializer;
6import java.util.Properties;
7import org.apache.kafka.clients.producer.KafkaProducer;
8import org.apache.kafka.clients.producer.ProducerRecord;
9
10public class AvroProducerWithoutSchemaRegistry {
11    public static void main(String[] args) {
12        Properties props = new Properties();
13        // Standard Kafka Producer Properties
14        props.put("bootstrap.servers", "localhost:9092");
15        props.put("key.serializer", "org.apache.kafka.common.serialization.StringSerializer");
16        props.put("value.serializer", KafkaAvroSerializer.class.getName());
17
18        // Manually Configuring Schema
19        String userSchema = "{\"type\":\"record\"," +
20                "\"name\":\"myrecord\"," +
21                "\"fields\":[{\"name\":\"f1\",\"type\":\"string\"}]}";
22        Schema.Parser parser = new Schema.Parser();
23        Schema schema = parser.parse(userSchema);
24
25        GenericRecord avroRecord = new GenericData.Record(schema);
26        avroRecord.put("f1", "value1");
27
28        KafkaProducer<String, GenericRecord> producer = new KafkaProducer<>(props);
29        ProducerRecord<String, GenericRecord> record = new ProducerRecord<>("your-topic", avroRecord);
30        producer.send(record);
31
32        producer.close();
33    }
34}

In this example, the value.serializer is set to KafkaAvroSerializer. However, no schema.registry.url is specified as the schema is embedded directly within the producer application.

Best Practices

  • Schema Management: Even without a schema registry, it's wise to version and manage schema changes judiciously to maintain compatibility.
  • Use Avro-Compatible Data Types: Ensure that your data types in Kafka messages strictly follow what is definable in Avro.

Summary Table

Key ComponentDescriptionImportance
Embedding SchemaDirect inclusion of schema in the producerCritical for serialization without Schema Registry
KafkaAvroSerializerUsed as value.serializerFacilitates Avro serialization format
CompatibilityEnsuring forward and backward compatibility manuallyPrevents potential runtime issues

Conclusion

While most Kafka and Avro integrations leverage a Schema Registry for ease and reliability, situations demanding embedded schemas can utilize the techniques described. This approach demands careful management of schemas and compatibility, ensuring robust and scalable data streaming applications.


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

All Rights Reserved.