Part 6 · 1 chapters · ~8 min
Schemas and Evolution
Why schemas matter for events, Avro, Protobuf and JSON Schema, the Schema Registry and subject naming, compatibility modes (backward, forward, full), safe and unsafe changes, compacted topics as changelogs, event versioning, and data contracts between teams.
8
Contracts for events
code
// Avro schema for TransferCompleted v2: a new optional field with a default is backward compatible
{ "type": "record", "name": "TransferCompleted", "namespace": "bank.transfers",
"fields": [
{ "name": "transfer_id", "type": "string" },
{ "name": "account_id", "type": "string" },
{ "name": "amount_minor", "type": "long" },
{ "name": "currency", "type": "string" },
{ "name": "completed_at", "type": { "type": "long", "logicalType": "timestamp-millis" } },
{ "name": "channel", "type": ["null", "string"], "default": null } // added in v2
] }| compatibility mode | guarantee | allows |
|---|---|---|
| BACKWARD (default) | new consumers can read old data | delete fields; add fields with defaults |
| FORWARD | old consumers can read new data | add fields; delete fields with defaults |
| FULL | both | add or delete only fields with defaults |
The registry checks every new schema against the configured mode at publish time, so a breaking change fails in CI instead of breaking consumers in production. Compacted topics (cleanup.policy=compact) keep only the latest record per key, turning a topic into a changelog that can rebuild a table: the basis of Kafka Streams state stores and CDC snapshots.