Schema registry
Register Avro, Protobuf and JSON Schema in the Red Hat build of Apicurio Registry, and evolve a payment event without breaking consumers.
A JSON string works until a second team adds a field and the first team's deserializer throws. A schema registry stores the contract, stamps each record with the schema it was written with, and refuses a new version that would break the readers you still have.
This lab runs the Red Hat build of Apicurio Registry against the lab Kafka cluster, registers a payment event, and publishes a compatible change. The producer and consumer from producers and consumers are who this contract protects.
Prerequisites
- The
labcluster Ready - The Apicurio Registry operator installed. This lab uses the
ApicurioRegistrycustom resource from the Red Hat build curlandjq
Run the registry on the Kafka cluster
KafkaSQL storage keeps registry data in a Kafka topic, so the registry has the same durability as the cluster and does not need its own database. Create the journal topic first, with no retention limit, or the registry will forget schemas when the topic is cleaned.
apiVersion: kafka.strimzi.io/v1beta2
kind: KafkaTopic
metadata:
name: kafkasql-journal
namespace: kafka
labels:
strimzi.io/cluster: lab
spec:
partitions: 3
replicas: 3
config:
cleanup.policy: delete
retention.ms: "-1"
retention.bytes: "-1"
---
apiVersion: registry.apicur.io/v1
kind: ApicurioRegistry
metadata:
name: registry
namespace: kafka
spec:
configuration:
persistence: kafkasql
kafkasql:
bootstrapServers: lab-kafka-bootstrap.kafka.svc:9092
oc apply -f registry.yaml
oc get apicurioregistry registry -n kafka
The Service is registry-service in kafka once the operand is Ready. Confirm with oc get svc -n kafka. The API root used below is http://registry-service.kafka.svc:8080/apis/registry/v2.
Register three formats
Avro is the default in this series: small on the wire, and the compatibility rules are well understood. Protobuf fits a shop that already speaks it. JSON Schema fits a caller that will keep sending JSON and wants the registry to check it. One registry holds all three. One topic should stick to one.
payment.avsc:
{
"type": "record",
"name": "PaymentCreated",
"namespace": "payments",
"fields": [
{"name": "id", "type": "string"},
{"name": "account", "type": "string"},
{"name": "amount", "type": "long"}
]
}
REG=http://registry-service.kafka.svc:8080/apis/registry/v2
curl -s -X POST "$REG/groups/payments/artifacts" \
-H 'Content-Type: application/json' \
-H 'X-Registry-ArtifactType: AVRO' \
-H 'X-Registry-ArtifactId: payment-created' \
--data-binary @payment.avsc
curl -s -X PUT "$REG/groups/payments/artifacts/payment-created/rules" \
-H 'Content-Type: application/json' \
-d '{"type":"COMPATIBILITY","config":"BACKWARD"}'
Point the serializer at the registry. With Apicurio's Serde on the classpath the producer auto-registers or looks up payments/payment-created, and the bytes on the topic carry the schema id. The consumer asks the registry for that id and decodes. A consumer that does not have the new field yet still decodes, if you followed the rule below.
For Protobuf, send the .proto file with X-Registry-ArtifactType: PROTOBUF and Content-Type: application/x-protobuf. For JSON Schema, send the schema document with X-Registry-ArtifactType: JSON. The compatibility rule is the same call.
Evolve it
BACKWARD means a new schema can read data written by the previous one. Consumers can be upgraded after producers. Adding an optional field with a default is backward compatible:
{"name": "currency", "type": "string", "default": "BDT"}
Post that file as a new version. The registry accepts it. Removing account, or changing amount from long to string, is rejected. That rejection is the point of the registry.
| Change | BACKWARD | FORWARD |
|---|---|---|
| Add a field with a default | Allowed. Old records still decode | The old schema cannot see the new field. Rejected unless you planned a forward-only rollout |
| Remove a field | Rejected. Old records still contain it | Allowed |
| Rename a field | Rejected. A rename is a remove plus an add | Rejected |
| FULL | Both directions, which is what you want once more than one consumer exists |
FORWARD is the rule when producers move first and old consumers must ignore new fields. FULL demands both. Set the rule before the second team starts producing. A registry with compatibility NONE is a file share.
When the registry rejects a version, fix the schema. Do not switch the rule off for the release. Readers you have already shipped are still out there, and delivery guarantees assumed they could decode every record.
What's next
- The bytes this schema describes are what Debezium should publish from the outbox, not an untyped JSON string.
- Producers in producers and consumers swap their JSON serializer for the Apicurio Serde.
- The registry's own topic has to be in the mirror if the DR site will decode, in DC/DR.