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 modeguaranteeallows
BACKWARD (default)new consumers can read old datadelete fields; add fields with defaults
FORWARDold consumers can read new dataadd fields; delete fields with defaults
FULLbothadd 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.