Schema Evolution And Versioning
Schema evolution is where integration systems often become brittle. Klok uses explicit model versions, compatibility rules, and bridge patterns to keep change manageable.
Compatibility Rules
A change is usually compatible when consumers can ignore it or continue to parse the payload safely.
Compatible examples:
- Adding an optional field.
- Adding documentation or annotations.
Breaking examples:
- Removing a field.
- Renaming a field.
- Changing field meaning.
- Changing type in a way consumers cannot parse.
- Making an optional field required, or a required one optional.
- Adding or removing an enum value. A consumer built with the earlier model decodes the message with Avro, which fails on a value it does not know, and the message goes to the dead letter topic.
How The Pipeline Checks It
Producers and consumers are deployed in any order, and a service may run an earlier release of a model for a long time.
So every release of a major version must read what every other release writes, in both directions. The datamodel
pipeline holds that line: on every merge request, klok_datamodel_compatibility compares the Avro schema the generated
classes register on the broker with that of every earlier release of the model, and fails the merge request when the
change would break a service built with any of them. The job names the field and the releases it breaks.
When it fails, there are two ways forward: make the change compatible (add the field as optional, or keep a field you no longer need and stop filling it), or make it a new major model. The rules, the message and the rare case of releases nobody runs any more are on the Datamodel compatibility page of the Klok Gradle plugin documentation.
Major Versions
The major version belongs in the model or class name, such as InvoiceV1 or InvoiceV2. This lets topics and Java
mappings stay clear.
Do not add a separate semver segment to the topic name. Use model names with major versions and manage detailed artifact versioning in package metadata.
How strict a breaking change is depends on the model's class: a public model runs its old version longest and notifies every reader, a common model is coordinated across domains, a domain model is decided within the domain, and a system model follows its external system. See Creating A Datamodel.
Topic Changes
A breaking model change requires a new model and usually a new topic.
Example:
persistent://acme_prod/finance/InvoiceV1_Created
persistent://acme_prod/finance/InvoiceV2_Created
Consumers can migrate at different times when both topics exist.
Bridge Patterns
Use schema upgrade or downgrade bridges during migration:
- Upgrade bridge: reads old model and publishes new model.
- Downgrade bridge: reads new model and publishes old model for legacy consumers.
Bridge routes should be documented as temporary unless there is a durable business reason for both contracts.
Evolution Checklist
- [ ] Change category is compatible or breaking.
- [ ] The merge request's
klok_datamodel_compatibilityjob passes, or the change is a new major model. - [ ] New major model exists for breaking change.
- [ ] New topics are named correctly.
- [ ] Consumers are identified through Integration Context.
- [ ] Bridge routes are planned if needed.
- [ ] Deprecation timeline is documented.
- [ ] Replay and backfill behavior is understood.