guide
guide9 min read

Data Contracts with Kafka: Schema Registry, Enforcement and Breaking-Change Checks

A step-by-step tutorial for enforcing producer-consumer contracts on Kafka topics and catching breaking changes before deploy

To implement data contracts with Kafka, register a schema for every topic in a schema registry, set a compatibility mode on each subject, and run a compatibility check in CI before any producer change merges. The Confluent Schema Registry documentation defines the compatibility modes (BACKWARD, FORWARD, FULL and their transitive variants) that make this enforceable, and the registry exposes a REST endpoint that tests a candidate schema against registered versions without registering it.

Key Takeaways

  • •A Kafka data contract has three parts: a schema in a registry, a compatibility mode per subject, and a check that runs before the producer deploys.
  • •Schema compatibility only catches structural changes: removed fields, changed types, new required fields. It does not catch a change in what a field means.
  • •Enforce the contract at two points: in CI (before merge) and at runtime (producer serializer or broker-side validation).
  • •BACKWARD_TRANSITIVE is the safest default when consumers replay topics from the beginning, because every historical version has to remain readable.
  • •Semantic breaks, such as a unit change or a redefined status value, need explicit contract rules or tests, not compatibility modes alone.

Data contracts with Kafka: what you are actually enforcing

A data contract is an agreement between a producer and its consumers about the shape, meaning and quality of the data on a topic. On Kafka, the shape half is the easy part because the ecosystem already has the tooling: Avro, Protobuf or JSON Schema definitions stored in a registry, with the schema ID embedded in each message. The meaning and quality halves are harder, and most teams skip them.

We split the contract into three layers, and the tutorial below covers each. Layer one is structure (field names, types, nullability). Layer two is evolution rules (which changes are allowed, expressed as a compatibility mode). Layer three is semantics and quality (units, allowed values, freshness, null-rate limits). Registry compatibility handles layers one and two natively. Layer three needs rules in the contract or checks downstream. This is the same gap that shows up in batch tooling: contract checks on column names and types catch structural drift but miss logic changes, a point raised repeatedly in data engineering communities.

If you want the prerequisites, you need a running Kafka cluster, a schema registry (Confluent Schema Registry or a registry-compatible service), and a Git repository where producer code and schema files live together. The commands below use the registry REST API so they work with any compatible implementation.

Step 1: Define the schema as a file in the producer repository

Keep the schema in the same repository as the producer code, not only in the registry. The registry is the runtime source of truth, but the Git file is what code review and CI act on. A minimal Avro contract for an orders topic looks like this:

  • •Name the record and namespace after the business entity (for example com.example.orders.OrderPlaced), not the service that produces it.
  • •Give every field a doc string. Consumers read these, and they are the first place semantics get written down.
  • •Give new optional fields a default value (null or a sensible constant). Defaults are what make additive changes backward compatible in Avro.
  • •Never reuse a field name with a different meaning. Add a new field and deprecate the old one.

Treat the .avsc (or .proto) file as the contract artifact. Changes to it need review from at least one consumer owner, which you can encode with a CODEOWNERS entry on the schema directory.

Step 2: Register the schema under a subject naming strategy

The registry stores schemas under subjects. The default TopicNameStrategy maps one subject per topic, using the pattern <topic>-value for message values and <topic>-key for keys. That is the right default for data contracts, because it makes the contract and the topic one-to-one. Use RecordNameStrategy only when a topic intentionally carries multiple event types.

Register the schema with a POST to /subjects/orders-value/versions. The registry returns a global schema ID. Producers using the registry-aware serializer write that ID into the message header bytes, and consumers use it to fetch the exact schema that wrote each message. This is what lets old and new messages coexist on one topic.

Step 3: Set the compatibility mode per subject

Compatibility mode is the evolution rule of the contract. Set it explicitly per subject with a PUT to /config/orders-value, rather than relying on the global default, so the rule is visible and auditable. The Confluent schema evolution documentation describes each mode; the practical choices are these:

ModeWhat it guaranteesAllowed changes (Avro)Use when
BACKWARDNew schema can read data written with the previous schemaDelete fields, add optional fields with defaultsConsumers upgrade before producers
BACKWARD_TRANSITIVENew schema can read data from all previous versionsSame as BACKWARD, checked against every versionConsumers replay the topic from the beginning
FORWARDPrevious schema can read data written with the new schemaAdd fields, delete optional fieldsProducers upgrade before consumers
FULLBoth backward and forward against the previous versionAdd or delete optional fields onlyProducers and consumers upgrade independently
NONENo checksAnythingNever, for a contracted topic

Our opinion: start with BACKWARD_TRANSITIVE for any topic that has a replaying consumer (a warehouse sink, a stream processor with state restore, a backfill job), and FULL_TRANSITIVE for topics with many independently deployed consumers. The cost is that you can never make a removal of a required field in one step. That is the point of a contract.

Step 4: Add a breaking-change check to CI

This is the step most teams miss, and it is where the contract starts to protect you. The registry has a compatibility endpoint: POST the candidate schema to /compatibility/subjects/orders-value/versions/latest and it returns {"is_compatible": true} or false, without registering anything. Run that call in the pull request pipeline for every changed schema file.

  • •Detect which schema files changed in the diff and map each to its subject.
  • •POST each candidate to the compatibility endpoint against the latest (or all, for transitive modes) registered versions.
  • •Fail the job on is_compatible false, and print the registry's reported incompatibility so the author sees which field broke.
  • •Register the schema only on merge to main, from the pipeline, never from application startup. Set auto.register.schemas=false on producers in production.

Disabling auto-registration matters. With it on, a producer can register an incompatible-by-intent schema at deploy time, or register a compatible but unreviewed one. With it off, the pipeline is the only path to a new schema version, and review is the gate.

Registry compatibility will not catch a change that keeps the type and renames the meaning. If total_amount moves from dollars to cents, the schema is identical and the check passes. Cover this with a semantic layer: contract rules (the registry supports data quality and migration rules in the data contracts feature of Confluent Platform and Cloud) or consumer-side tests that assert value ranges on a sample of real messages. The Kafka documentation and your registry's docs cover the rule support available in your version.

Step 5: Enforce the contract at the producer and the broker

CI protects the merge. Runtime enforcement protects against everything that bypasses CI: a hotfix, a second producer someone forgot about, a script writing directly to the topic.

  • •Producer side: use the registry-aware serializer with auto.register.schemas=false and use.latest.version=true, so a producer that does not match the registered schema fails at serialization instead of writing bad data.
  • •Broker side: where your Kafka distribution supports broker-side schema validation (Confluent Server does, with confluent.value.schema.validation=true on the topic), turn it on so the broker rejects messages whose schema ID is not registered for the subject. Open-source Apache Kafka brokers do not validate schemas themselves, so on those clusters rely on producer-side enforcement plus access control.
  • •Access control: restrict write ACLs on contracted topics to the owning service accounts. A contract nobody can bypass needs a write path nobody else has.
  • •Dead-letter topic: have consumers route messages that fail deserialization to a dead-letter topic with the schema ID and error attached, rather than crash-looping or silently dropping.

Step 6: Monitor drift and semantic breaks after deploy

Even with strict compatibility, three things still drift: the semantics of a field, the null rate of an optional field, and the timeliness of the stream. Add monitors for the ones your consumers depend on. Examples from the prompts our users run: "What changed in the orders schema since last deploy?" and "Is the customer_id join safe after the migration?"

We built the Streaming Agent for the topic-governance part of this: Kafka, Pulsar and Kinesis topic governance, schema registry checks and exactly-once monitoring, exposed as an MCP server you can call from Claude Code or Cursor. It chains with our Schema Agent, which maps column-level drift to downstream impact. Ask "What's the blast radius if I drop this column?" and the Schema Agent traces the consumers, including the warehouse tables fed by your Kafka sinks. The Data Workers open-source edition (dw-claw) is read-only by default, so you can point it at a registry and a cluster to audit contract coverage without granting write access.

We covered how Data Workers keeps the catalog current in a separate post, which matters here because a contract is only useful if consumers can find it. Our Catalog Agent) links each registered subject to its owner and its downstream tables, so a producer opening a pull request can see who a change affects before CI answers.

Where this stops: the limits of registry-based contracts

We want to be direct about what this setup does not do. A registry compatibility check is a structural check against registered schemas. It does not know about your consumers' actual field usage, so it can block a change no consumer would notice and pass one that quietly breaks a downstream aggregation. It also covers Kafka topics only: the dbt models and warehouse tables fed by those topics need their own contracts. dbt model contracts, described in the dbt docs, enforce column names and data types at build time but, like registry compatibility, do not check what the logic produces.

The practical answer is to chain the checks. The topic contract guards the producer boundary, the dbt contract guards the transformation boundary, and a lineage-aware agent connects the two so a Kafka schema change surfaces the affected dbt models in the same pull request. Our Pipeline Agent can run that chain from your terminal when you ask it to validate a pending schema change against the dbt project that consumes the topic.

Frequently Asked Questions

What is a data contract in Kafka? It is an enforced agreement on the schema, evolution rules and, optionally, quality rules for the messages on a topic. In practice it combines a schema registry entry, a compatibility mode on the subject and a CI check, so a producer cannot ship a change that breaks consumers without the check failing first.

Which compatibility mode should I use for a Kafka data contract? Use BACKWARD_TRANSITIVE if any consumer replays the topic from the beginning, and FULL_TRANSITIVE if many consumers deploy independently of the producer. Avoid NONE on a contracted topic. This is our recommendation; the registry docs describe the trade-offs of each mode.

Does schema compatibility catch breaking changes in what the data means? No. A field that keeps its name and type but changes units, scale or allowed values passes every compatibility mode. Catch those with contract rules, value-range tests on sampled messages, or downstream monitors on distributions and null rates.

Can I enforce data contracts on open-source Apache Kafka? Yes, with limits. The registry compatibility check, CI gating and producer-side serializer settings all work. Broker-side schema validation is not part of Apache Kafka itself, so you rely on producer enforcement, write ACLs and consumer dead-letter handling instead.

How do I stop producers from registering schemas at runtime? Set auto.register.schemas=false on every production producer and register schemas only from the merge pipeline. Combine that with registry ACLs so only the pipeline's service account can write new subject versions.