Simple definition#
An event contract is the agreed shape and meaning of an event: field names, types, required fields, semantics and service levels. A schema registry stores every version of that shape and checks that new versions do not break existing producers or consumers.
The problem#
Kafka stores bytes and does not check them. If a producer renames amount to value, every consumer breaks, and it may happen at 2 a.m., replaying data from last month. Contracts and compatibility checks turn that runtime outage into a failed build.
Serialisation formats#
| Avro | Protobuf | JSON Schema | |
|---|---|---|---|
| Encoding | Compact binary; schema needed to read | Compact binary; field numbers | Text JSON |
| Evolution | Excellent, with defaults | Excellent, with field numbers | Weaker and easy to get wrong |
| Tooling | Strong in data and Kafka ecosystems | Strong in gRPC and polyglot services | Human-readable, browser-friendly |
| Typical use | Kafka and data platforms | Service-to-service and event mix | Edge, low-volume, external partners |
Any of the three works with a registry. Pick one default per organisation.
How a registry works#
sequenceDiagram
participant P as Producer
participant R as Schema Registry
participant K as Kafka
participant C as Consumer
P->>R: register or lookup schema (subject payments-value)
R-->>P: schema ID 17 (or reject if incompatible)
P->>K: [magic byte][schema ID 17][binary payload]
C->>K: fetch record
C->>R: get schema 17 (cached)
C->>C: deserialize with writer schema 17 + reader schema
Each message carries only a small schema ID. Consumers fetch and cache the writer’s schema and resolve it against their own reader schema.
Registries include Confluent Schema Registry, Apicurio Registry (open source, by Red Hat), AWS Glue Schema Registry and Azure Schema Registry.
Compatibility modes#
| Mode | Guarantee | Allowed change (Avro) | Upgrade order |
|---|---|---|---|
| BACKWARD (default) | New consumers can read data written with the previous schema | Delete fields; add optional fields with defaults | Consumers first |
| FORWARD | Old consumers can read data written with the new schema | Add fields; delete optional fields | Producers first |
| FULL | Both directions | Add or delete optional fields with defaults | Any order |
*_TRANSITIVE |
The guarantee holds against all previous versions, not just the last | Same, but stricter | For long retention and replay |
Safe and breaking changes#
| Change | Safe? |
|---|---|
| Add an optional field with a default | ✅ |
| Remove an optional field | ✅ (under backward compatibility) |
| Rename a field | ❌ Add the new field, deprecate the old one, remove it later (Avro aliases can help) |
Change a type (int → string) |
❌ (only certain widenings such as int → long are allowed) |
| Change meaning (cents → euros) | ❌ and invisible to tools; always a new version |
| Reuse a Protobuf field number | ❌ Never; mark removed numbers as reserved |
For a breaking change, publish a new major version topic (...v2), run v1 and v2 in parallel while consumers migrate, then retire v1.
Data contracts#
A data contract goes beyond the schema. It is a versioned agreement between a producer and its consumers that covers:
- schema (structure and types)
- semantics (definitions, units, allowed values, PII classification)
- quality rules (not null, ranges, uniqueness, freshness)
- SLOs (latency, availability)
- ownership and support
- policies (retention, access, masking).
Specifications such as the Open Data Contract Standard (ODCS, Bitol project, Linux Foundation) describe contracts in YAML. Some registries attach rules to schemas, for example data-quality conditions or field-level encryption tags enforced in the serialiser.
# Simplified data contract (ODCS-style)
apiVersion: v3.0.0
kind: DataContract
id: payments-authorised
version: 1.2.0
status: active
owner: team-payments
schema:
- name: PaymentAuthorised
properties:
- { name: paymentId, logicalType: string, required: true, unique: true }
- { name: accountId, logicalType: string, required: true, classification: confidential }
- { name: amount, logicalType: number, required: true, quality: [{ rule: "amount > 0" }] }
- { name: currency, logicalType: string, required: true, examples: ["EUR", "INR"] }
slaProperties:
- { property: latency, value: 5, unit: s }
Governance in the delivery pipeline#
flowchart LR
PR["PR changes schema"] --> CI["CI: compatibility check<br/>against registry"] --> LINT["Lint: naming, docs,<br/>PII tags"] --> REV["Owner + consumer review"] --> REG["Register on deploy"]
CI -->|"incompatible"| FAIL["Build fails"]
- Store schemas in Git next to the producer code, and generate classes from them.
- Run compatibility checks in CI with the registry’s Maven or Gradle plugins or its REST API.
- Turn off auto-registration in production producers, so only CI registers schemas.
- Publish AsyncAPI documents so consumers can discover topics, schemas and owners.
Common mistakes#
Quick check#
What does BACKWARD compatibility guarantee? Consumers using the new schema can read data written with the previous schema, so you upgrade consumers first.
Why use a transitive compatibility mode? It checks the new schema against all earlier versions, which matters when consumers replay long history.
How do you rename a field safely? Add the new field (optional, with a default), migrate producers and consumers, deprecate the old field, then remove it in a later version.
What does a data contract add beyond a schema? Semantics, quality rules, SLOs, ownership, classification and policies, agreed between producer and consumers.
Why disable auto-registration in production? So schemas only change through reviewed, compatibility-checked CI pipelines, not at runtime by whichever producer deploys first.
Summary#
- Events are contracts. Use Avro, Protobuf or JSON Schema with a registry.
- Choose compatibility modes deliberately, and use transitive modes for replay.
- Handle breaking changes with new versioned topics and parallel runs.
- Extend schemas to data contracts, and enforce them in CI.
References#
- Confluent, Schema evolution and compatibility.
- Apicurio Registry.
- Bitol, Open Data Contract Standard.
- AsyncAPI specification.