Lesson 11 of 17 · The ecosystem
Schemas and Schema Registry
Kafka stores bytes; producers and consumers must agree on what they mean. Why schemas matter, how a schema registry works (schema IDs in every record), Avro versus Protobuf versus JSON Schema, the compatibility modes and which changes each allows, the order to upgrade producers and consumers, checking compatibility in CI, and what to do when a change really is breaking.
Kafka only sees bytes
To Kafka, a record's key and value are just bytes. The producer serializes objects into bytes; the consumer deserializes them. If the producer team renames total to amount, or changes it from a number to a string, consumers break, often at 3 a.m., and the bad records are already in the log.
A schema is the contract: field names, types, which fields are optional, defaults. A schema registry stores schemas, gives each one an ID, and refuses changes that would break readers.
Kafka is a post office that delivers sealed envelopes and never looks inside. The schema is the form layout everyone agreed on: "box 1 is your name, box 2 your age". The registry is the office that keeps the official form, and refuses to print a new version where box 2 suddenly means "favourite colour", because everyone reading old forms would get confused.
How a registry works
Producer Schema Registry Consumer
serializer: "register/lookup schema" ──► orders-value v3 = ID 42
record bytes = [magic 0][ID 42][payload] ──► Kafka ──► deserializer reads ID 42
fetches schema 42 (cached) ──► object
- Only a small schema ID (4 bytes plus a marker) travels in each record.
- Schemas are grouped by subject, by default
<topic>-keyand<topic>-value(the TopicNameStrategy). - Every new version of a subject is checked against the subject's compatibility mode before it is accepted.
Common registries: Confluent Schema Registry, Apicurio Registry (open source, Red Hat), AWS Glue Schema Registry, Azure Schema Registry (Event Hubs). Their wire formats differ, so pick one per platform.
Avro, Protobuf or JSON Schema?
| Avro | Protobuf | JSON Schema | |
|---|---|---|---|
| Encoding | Compact binary | Compact binary | Text JSON |
| Schema language | JSON | .proto files |
JSON |
| Evolution rules | Very well defined; defaults matter | Field numbers; unknown fields ignored | Looser; easier to get wrong |
| Ecosystem | Strong in data tooling, Kafka Connect | Strong in gRPC and many languages | Easy for humans and web teams |
| Size | Small | Small | Large |
Avro and Protobuf are the common choices for shared, high-volume topics. JSON Schema is fine for low volume when readability matters more.
An Avro schema:
{
"type": "record",
"name": "OrderPlaced",
"namespace": "com.example.orders",
"fields": [
{ "name": "event_id", "type": "string" },
{ "name": "order_id", "type": "string" },
{ "name": "total", "type": "double" },
{ "name": "currency", "type": "string", "default": "EUR" }
]
}
Compatibility modes
| Mode | Rule | Upgrade order |
|---|---|---|
| BACKWARD (default) | Consumers using the new schema can read data written with the previous one | Consumers first, then producers |
| FORWARD | Consumers using the previous schema can read data written with the new one | Producers first |
| FULL | Both | Any order |
*_TRANSITIVE |
Same, but against every earlier version | Needed when old data is replayed |
| NONE | Anything goes | Only for private, throwaway topics |
Because Kafka keeps history, a consumer replaying a topic may meet every version ever written, so BACKWARD_TRANSITIVE or FULL_TRANSITIVE is a good default for long-retention or compacted topics.
What changes are safe
| Change | BACKWARD | FORWARD | FULL |
|---|---|---|---|
| Add an optional field with a default | ✔ | ✔ | ✔ |
| Add a required field without a default | ✘ | ✔ | ✘ |
| Remove a field that had a default | ✔ | ✔ | ✔ |
| Remove a field without a default | ✔ | ✘ | ✘ |
| Rename a field | ✘ (✔ with an Avro alias) | ✘ | ✘ |
| Change a type (string → int) | ✘ | ✘ | ✘ |
The habit that keeps you safe: give every new field a default, never rename, never change types.
Check compatibility before you deploy
Treat schemas as code: keep them in Git next to the producer, and test every change in CI against the registry:
$ curl -s -X POST -H "Content-Type: application/vnd.schemaregistry.v1+json" \
--data @<(jq -n --arg s "$(cat OrderPlaced.avsc)" '{schema: $s}') \
http://schema-registry:8081/compatibility/subjects/orders-value/versions/latest
{"is_compatible":true}
A failed check fails the pipeline, long before a bad schema reaches production. Build plugins (Maven, Gradle) and registry CLIs do the same. In production, disable automatic registration from applications (auto.register.schemas=false) so only CI can register schemas.
When a change really is breaking
Some changes can't be made compatible (a different meaning, a new key). Then:
- Create a new versioned topic:
orders.placed.v2. - The producer writes both v1 and v2 for a migration window (or a small bridge job converts v1 to v2).
- Consumers move to v2 one by one.
- When no group reads v1 (check
kafka-consumer-groups.sh), stop writing it and delete it after retention.
Versioning in the topic name makes the contract explicit and lets both live side by side.
Try it: break and protect a contract
- Run a schema registry next to the lab cluster (for example Apicurio Registry or Confluent Schema Registry in Docker) and register the
OrderPlacedschema fororders-value. - Test adding a field with a default, then without one, using the compatibility endpoint. Record the answers.
- Change the subject to
FULLand test removingcurrency. - Write the CI step that fails a build when
is_compatibleis false.
Going deeper
- A data contract goes beyond types: owner, meaning of each field, allowed values, PII classification, SLA. Store it with the schema and show it in your developer portal.
- Keys need schemas too, and changing the key format changes partitioning. Treat key changes as breaking.
- Put event metadata (event ID, type, time, source, trace ID) in a standard envelope or headers, for example following CloudEvents, so every topic is consistent.
Recap
- Kafka moves bytes; schemas are the contract between teams.
- A registry stores versions; each record carries only a schema ID.
- BACKWARD (consumers first), FORWARD (producers first), FULL (any order); use transitive modes for replayable topics.
- Add fields with defaults; never rename or change types; check compatibility in CI.
- Truly breaking change → new versioned topic and a migration window.
This site is a public version of my personal engineering knowledge hub. It intentionally excludes confidential company information and internal operational details.