Datamodel compatibility

A datamodel's major version is part of its name: order-v1 is a repository, an artifact, the classes …V1 and the topics …/OrderV1. Every release of order-v1 must therefore read what every other release of order-v1 writes, and the other way round, because the services that produce and consume a topic are deployed in any order and a service may stay on an earlier release for a long time. A change that breaks that is not a new release of order-v1; it is order-v2, a new datamodel next to the old one.

klokDatamodelCompatibilityCheck is what holds that line. For a project with projectType = DATAMODEL the plugin registers it, check and publish depend on it, and the datamodel pipeline runs it on every merge request (klok_datamodel_compatibility, build-ci-templates). It fails the build when the Avro schema the new classes register on the broker is not compatible, in both directions, with the schema of every earlier release of the same artifact.

What passes and what fails

The check compares the wire schema: the schema Pulsar registers for a generated class (Schema.AVRO(clazz), the call the broker client makes), not the XSD or the .avsc generated from it. Pulsar makes every field of a reference type (String, a boxed number, an enum, a record, a list) optional, ["null", T] with the default null, and every Java primitive (int, long, boolean, double) required, with no default. In the XSD, a numeric or boolean element without minOccurs="0" becomes such a primitive; every other element is optional on the wire, whatever its minOccurs.

Change Result Why
add an optional field (any type but a required number or boolean) compatible a reader that lacks it skips it, a reader that expects it finds null
add a numeric or boolean element without minOccurs="0" breaks it has no default, so this build cannot read a message an earlier release wrote
remove a field, optional or required breaks the platform's rule: a service built with an earlier release relies on it. Avro would read around an optional one and hand that service an empty value without a word; a required one fails Avro as well
rename a field breaks it is a removal and an addition, so the same rule applies
change a field's type (string to int, a record to another record) breaks neither side can read the other's value
make an optional field required, or a required one optional breaks one side may write the empty value the other cannot read
add an enumeration value breaks a service on an earlier release fails on the first message that carries it; Pulsar's own FULL_TRANSITIVE rule lets it through, the check does not
remove an enumeration value breaks this build cannot read a message an earlier release wrote with it
remove or rename a class of the model breaks services built with an earlier release still use it
move the classes to another Java package compatible on the wire Avro matches a record by its name without the namespace; the ODL type of an object is its class name, though, so its index needs a migration

"Every earlier release" means all of them, not only the latest: a field that comes back with another type after a retirement (below) is compatible with the releases still compared, and would break a service on any release before them, which is why a retirement is a claim about the running landscape.

For the datamodel author: a failed check

The merge request's klok_datamodel_compatibility job fails with a message like this:

[KlokPlugin - Datamodel compatibility] order-v1 1.0.0-build.261011120000 breaks services built with an earlier release of order-v1.
Producers and consumers are deployed in any order and may stay on an earlier release, so every release of order-v1
must read what every other release writes, both ways. Compared with: 1.0.0-build.260925130959, 1.0.0-build.261010095051.

  dev.klok.acme.datamodel.finance.order_v1.OrderV1
    against 1.0.0-build.260925130959, 1.0.0-build.261010095051:
      breaking: OrderV1.status (OrderStatusType) gained CANCELLED: an earlier release cannot read a message that carries it
    Avro:
      the earlier release cannot read what this build writes: MISSING_UNION_BRANCH at /fields/1/type/1: reader union lacking writer type: ENUM

Each class is listed once per distinct earlier schema, with the releases that had it. The breaking: lines name the field by its path in the record (OrderV1.lines[].quantity); the Avro: lines are Avro's own findings, which decide. The job keeps the same result as build/klok/datamodel/compatibility/compatibility.json, with the wire schemas of every earlier release next to it under releases/<version>/.

There are two ways forward, and the message names both:

  1. Make the change compatible. Add the field as optional, keep the old enumeration and add a string field next to it, or leave a field you no longer need in place, document it as unused and stop filling it. This is almost always the right answer for a change that only adds information.
  2. Make it a new major version. Create the datamodel <name>-v<n+1> from the datamodel template, with its own repository, XSD, classes …V<n+1>, catalog key and topics; klok datamodel names --major <n+1> prints every name. The old major stays as it is and both run side by side, for at least the minimum of the model's class, until every reader has moved (the klok-datamodel skill and the Datamodel classes page of the strategy).

For the datamodel owner: releases nobody uses any more

The check compares with every release it can find, including releases no service has run for months. When a break was released before the check existed, every later change fails against the releases before it, compatible or not. Once no service runs those releases any more, the owner records that in the repository's klok-datamodel-project.json:

{
  "name": "klok-documentation-config-v1",
  "compatibility": {
    "since": "1.0.0-build.260929230654",
    "reason": "klok-documentation-egress has run 1.0.0-build.261010095214 since 2026-10-10 and klok.dev writes the config through the ODL; nothing runs an earlier release"
  }
}

Releases before since are then not compared, and the job says so with the reason. since must name an earlier release of the datamodel, so the latest release is always compared, and reason is required: the entry is a claim about the running landscape, reviewed in the merge request that adds it. It is never a way past a new break. A break in a change that has not been released yet is a new major version.

For the platform maintainer: where it runs and what it needs

  • The earlier releases are the versions of the repository's release tags (v<version>, which the release job creates for every version) plus the version the dependency catalog names for the artifact. The check needs a full clone; in a shallow one it stops and says so. A tag whose version is not published under the current artifact (a release from before the datamodel was renamed) is skipped and named.
  • The jars are downloaded from the project's own Maven repositories with the credentials they are configured with (the job token in CI, klokGitAuthToken by hand), and dumped with Schema.AVRO in a separate JVM, as dumpKlokWireSchemas dumps the build's own classes. A registry that refuses or fails the request fails the check: a release that cannot be read cannot be compared. The catalog's version must be found; if no repository has it, the check fails rather than pass without the most important comparison.
  • By hand, in the generated Java project of a datamodel repository: gradle updateKlokLibsToml and then gradle klokDatamodelCompatibilityCheck, with klokGitAuthToken in ~/.gradle/gradle.properties for the GitLab registries.
  • The check never asks the broker. It is a property of the datamodel's releases, decided before anything reaches a topic, and the same on every CI system.