What is Kafka Schema Registry?
KafkaΒ Schema Registry is a service that manages and enforces schemas for Kafka messages.
- A schema defines the structure (fields, data types, defaults) of the message.
- Without it, producers and consumers might break if the message format changes.
- The Schema Registry ensures compatibility, validation, and governance of data.
π Itβs most often used with Avro, but also supports JSON Schema and Protobuf.
πΉ Why use Schema Registry?
- Consistency β All producers/consumers agree on message structure.
- Evolution β Allows schema changes (adding/removing fields) without breaking apps.
- Compact messages β Instead of sending full schema each time, Kafka only sends a schema ID; the consumer fetches the actual schema from the registry.
- Governance β Central place to manage versions and compatibility rules.
πΉ How it Works
-
Producer
-
Uses Avro/JSON/Protobuf to serialize data.
-
Sends message to Kafka topic with a schema ID in the header.
-
Schema itself is stored once in Schema Registry.
-
-
Consumer
-
Reads schema ID from message.
-
Fetches the schema from Schema Registry.
-
Deserializes message safely.
-
πΉ Example: Avro + Schema Registry
1. Define a Schema (user.avsc
)
{
"type": "record",
"name": "User",
"fields": [
{"name": "id", "type": "int"},
{"name": "name", "type": "string"},
{"name": "email", "type": ["null", "string"], "default": null}
]
}
2. Producer (Java with Confluent Serializer)
Properties props = new Properties();
props.put("bootstrap.servers", "localhost:9092");
props.put("key.serializer", "org.apache.kafka.common.serialization.StringSerializer");
props.put("value.serializer", "io.confluent.kafka.serializers.KafkaAvroSerializer");
props.put("schema.registry.url", "http://localhost:8081");
Producer<String, GenericRecord> producer = new KafkaProducer<>(props);
Schema schema = new Schema.Parser().parse(new File("user.avsc"));
GenericRecord user = new GenericData.Record(schema);
user.put("id", 1);
user.put("name", "Alice");
user.put("email", "alice@example.com");
producer.send(new ProducerRecord<>("users", "key1", user));
π Here, only the schema ID is sent in Kafka, not the full schema.
πΉ Schema Evolution & Compatibility
Schema Registry enforces compatibility modes when new versions are registered:
-
BACKWARD β New schema can read old data.
-
FORWARD β Old schema can read new data.
-
FULL β Both forward + backward compatible.
-
NONE β No compatibility check.
Example:
-
Add a new optional field β usually backward compatible.
-
Remove a required field β breaks backward compatibility.
πΉ Quick REST API Calls
Check schemas via Schema Registry API:
-
List subjects:
curl http://localhost:8081/subjects
-
Get latest schema:
curl http://localhost:8081/subjects/users-value/versions/latest
πΉ In Short
Kafka Schema Registry = a central service to:
-
Store message schemas (Avro, JSON, Protobuf).
-
Assign schema IDs for compact Kafka messages.
-
Enforce compatibility rules for schema evolution.
π Ensures safe, evolvable, strongly-typed event streams across producers and consumers.