Lesson 6 of 9 · 25m
Schema Registry: Compatibility, Evolution, and Governance
Use Avro, Protobuf, or JSON Schema with a registry as a compatibility contract; learn what changes are safe, unsafe, and operationally painful.
Schemas are not enterprise paperwork. They are how a stream remains readable after the team that wrote the producer has moved on. Without a schema contract, every consumer reverse-engineers payload shape from production traffic, and a harmless-looking JSON rename becomes a distributed outage. The registry's real job is to make incompatible change fail early, before bytes reach production.
Serialization is part of the API
A payload is an API contract just as much as a REST response. With Avro and the Confluent wire format, the serialized value contains a magic byte, a schema ID, and then binary Avro data:
byte 0 magic byte: 0
bytes 1-4 schema ID
bytes 5+ serialized payloadProducer configuration commonly looks like:
key.serializer=org.apache.kafka.common.serialization.StringSerializer
value.serializer=io.confluent.kafka.serializers.KafkaAvroSerializer
schema.registry.url=https://schema-registry:8081
auto.register.schemas=false
use.latest.version=trueI prefer auto.register.schemas=false in production. Schema registration should be reviewed and deployed intentionally, not triggered accidentally by any application that can reach the registry.
Compatibility is directional
Common modes are BACKWARD, FORWARD, FULL, and their transitive variants:
curl -s -X PUT -H 'content-type: application/vnd.schemaregistry.v1+json' --data '{"compatibility":"BACKWARD_TRANSITIVE"}' https://schema-registry:8081/config/payments.authorized-valueBACKWARD means a consumer using the new schema can read data written with the previous schema. FORWARD means an old consumer can read data written with the new schema. Rolling deployments often need careful ordering because producers and consumers are not upgraded atomically.
Safe changes are usually boring changes
In Avro, adding a field with a default is commonly compatible; removing a required field is not. Renaming a field is not magically safe because aliases do not absolve you from thinking through reader versions. Type changes are frequently traps: int to long is different from string to int.
{"type":"record","name":"PaymentAuthorized","namespace":"com.example.payments","fields":[
{"name":"event_id","type":"string"}, {"name":"payment_id","type":"string"},
{"name":"amount_minor","type":"long"}, {"name":"currency","type":"string","default":"USD"}
]}Amounts as integer minor units avoid binary floating-point drift. Time fields should state epoch units explicitly. Enum evolution needs extra care because readers must handle symbols they do not know.
Version the contract, not every implementation detail
A schema with forty nullable fields is usually not flexible; it is evidence that the event means too many things. Prefer focused events and explicit semantic versioning in documentation. PaymentAuthorized and PaymentCaptured are different facts. Do not encode both as type=1 with fields that are meaningless half the time.
Governance is operational, not ceremonial
Decide who can register schemas, whether subjects are topic-scoped or entity-scoped, how deletion is controlled, and how consumers pin versions. Test compatibility in CI. Generate artifacts from the schema rather than copying structs around by hand. Keep payload examples beside the schema so operators can debug production bytes.
Field notes
- Compatibility mode is checked by the registry, but enforcement still depends on clients actually using the registry and correct serializers.
- JSON without schema validation is schema-by-convention. That can work, but do not confuse it with governed evolution.
- Schema IDs are not global semantic versions; the same schema can have different IDs across environments.
- Tombstone records have null values and must remain valid under consumer deserialization logic.
- Test rollback, not only upgrade. A bad producer deploy often forces consumers to read both old and new shapes during rollback.
Was this lesson helpful?