Schema Registry: Wire Format and Compatibility Rules
Senior level ยท What is actually inside a serialized record, and which change is allowed to ship.
Prerequisites
- !Avro serialization basics
- !Kafka topic and partition fundamentals
What You'll Learn
- 1Decode the schema id prefix on a serialized record
- 2Explain subject naming strategies and why they differ per side
- 3Select a compatibility level that matches your deploy order
- 4Validate a schema change before any producer sends with it
Why a registry exists at all
Serializing a record with Avro requires a writer schema at the moment of encoding and a reader schema at the moment of decoding. If those two disagree, the consumer either fails outright or, worse, silently produces wrong values. A schema registry removes the argument by storing each schema once, assigning it an integer id, and letting every client resolve that id over HTTP with a local cache so the lookup costs nothing on the hot path.
The registry also becomes the enforcement point. Compatibility is checked when a producer registers a new version, so an incompatible change fails during continuous integration rather than in production at three in the morning. That single property is why most teams treat schema changes as reviewed, versioned events rather than incidental edits.
The wire format, byte by byte
Every record serialized through the registry carries a short header before the payload.
Byte layout of an Avro record on the wire
0 1 2 3 4 5 ...
+--------+--------+--------+--------+------+-------------------+
| 0x00 | schema id (int32, big endian) | Avro payload
+--------+--------+--------+--------+------+-------------------+
0x00 magic byte, constant, identifies a registry-framed record
schema id assigned at registration time, resolved client-side
payload Avro binary, encoded with the writer schema for that id
Consequence: the id travels with every single record, so a consumer can
resolve the writer schema from a cached lookup and never needs a round
trip per message.
The magic byte matters operationally. Anything that is not prefixed with it is rejected by the registry deserializer, which is how you catch a producer that bypassed the registry and wrote plain JSON into a topic everyone else reads as Avro. It also means a consumer that caches id-to-schema mappings in memory only pays a network cost the first time it meets a new version.
Subjects and naming strategies
A schema is registered under a subject, and the subject name decides which records the compatibility rule applies to. The two default strategies are topic name, where the subject matches the topic, and record name, where it matches the fully qualified schema type.
| Strategy | Subject follows | Used for | Failure mode |
|---|---|---|---|
| TopicNameStrategy | Topic name plus key or value | Most topics, mixed producers | One subject covers every writer to the topic |
| RecordNameStrategy | Fully qualified record name | Topics carrying several record types | Two schemas can share a subject unexpectedly |
| TopicRecordNameStrategy | Topic and record name | Shared topics with distinct types | Subject churn when a record is renamed |
TopicNameStrategy is the default and the right starting point because compatibility is a property of a topic, not of a Java class. Switch to record naming only when one topic genuinely carries several schemas, such as a shared event bus, and be aware that renaming a class then changes the subject and starts a fresh version history.
Backward, forward, full: which direction means what
The direction is defined relative to the new schema. Backward compatibility means the new schema can read data written with the old one. Forward compatibility means the old schema can read data written with the new one. Full means both, and it is the strictest of the three.
{
"type": "record",
"name": "Order",
"namespace": "com.example.events",
"fields": [
{ "name": "order_id", "type": "string" },
{ "name": "amount", "type": "bytes", "logicalType": "decimal", "precision": 10, "scale": 2 },
{ "name": "currency", "type": "string", "default": "USD" },
{ "name": "status", "type": { "type": "enum", "name": "OrderStatus",
"symbols": ["PENDING", "CONFIRMED", "SHIPPED", "DELIVERED"], "default": "PENDING" } },
{ "name": "metadata", "type": ["null", "string"], "default": null }
]
}
Adding a field with a default is backward compatible, because a reader using the old schema simply ignores it. Removing a field with a default is forward compatible for the same reason in reverse. Adding an enum symbol without a default breaks backward compatibility, because a reader holding the old schema does not know the new symbol; supplying a default symbol fixes it. Removing a field with no default breaks both directions.
The practical rule for rolling deploys is to register backward compatible versions first, because consumers upgrade at unpredictable times and must be able to read whatever producers have already begun writing.
Validating a change before anyone deploys it
# Set the compatibility level for the subject
curl -X PUT http://schema-registry:8081/config/orders-value \
-H "Content-Type: application/json" \
-d '{"compatibility": "BACKWARD"}'
# Check a candidate schema without registering it
curl -X POST \
http://schema-registry:8081/compatibility/subjects/orders-value/versions/latest \
-H "Content-Type: application/json" \
-d @order-v2.avsc
# Register only after the check passes; the id comes back here
curl -X POST http://schema-registry:8081/subjects/orders-value/versions \
-H "Content-Type: application/json" \
-d @order-v2.avsc | jq .
# Inspect history when a consumer reports an unexpected field
curl -s http://schema-registry:8081/subjects/orders-value/versions | jq '.[].version'
Run the compatibility endpoint in the build pipeline rather than at registration time. A schema that fails there should stop the merge, because the only window in which the change is still safe is before any producer has written a record with it.
โน๏ธTransitive levels exist for a reason
Avro, Protobuf and JSON Schema
Avro writes the writer schema into the record only when schemas are enabled, resolves it by id, and encodes positionally, which makes payloads small and field renames free. Protobuf uses field numbers rather than names, so renaming a field is wire compatible while reusing a number for a different type is not; optional fields and defaults are explicit in the definition.
syntax = "proto3";
message Order {
string order_id = 1;
string user_id = 2;
double amount = 3;
int64 timestamp_ms = 4;
OrderStatus status = 5;
string currency = 6; // added later, defaults to an empty string
}
enum OrderStatus {
ORDER_STATUS_UNKNOWN = 0; // proto3 requires a zero value
ORDER_STATUS_PENDING = 1;
ORDER_STATUS_CONFIRMED = 2;
}
JSON Schema is self-describing and human readable, so no registry lookup is needed to decode, but every record carries its full structure and validation runs on read. Choose it when other systems consume the topic directly without a Kafka client; choose Avro or Protobuf when payload size and encode speed dominate.
Migrating safely across a rolling deploy
Never remove a field and add its replacement in the same version. Ship the addition first with a default, deploy consumers that read both fields and prefer the new one, then deploy producers that write it, and only in a later release remove the old field. Three small, compatible steps always beat one clever incompatible step, and each step can be rolled back independently because the registry never allowed a version that breaks the readers still in service.
The reason this works is that the compatibility check runs against the registered subject history, not against the producer's intent. A registry set to BACKWARD will accept a new schema that can read every previously registered version, which means a consumer still on the previous version can parse the new producer's records through the reader schema it already holds. A registry set to FORWARD will accept a new schema that every previous reader can still parse, which is the direction you need when consumers cannot be upgraded in lockstep with producers. FULL requires both, and is the safest default for a topic with multiple independent consumer teams.
The practical migration sequence for a breaking change is always the same shape. Register a schema that adds the new field with a default and keeps the old field. Deploy consumers that read both and prefer the new one. Deploy producers that write the new field while still writing the old. Confirm in monitoring that no records are arriving with only the old field. Then register a final schema that removes the old field, and only after that deploy consumers that no longer expect it. Each stage is independently reversible because the registry's compatibility check guarantees that no stage produces a record the current readers cannot parse.
One subtlety that catches experienced teams: a field rename is not a rename to Avro. Avro resolves fields by name, so renaming a field is functionally a remove of the old name and an add of the new one, and if the new field has no default, the change is rejected as backward incompatible. The safe path is to add the new field with a default, dual-read and dual-write through a transition release, then remove the old field in a separate release after every consumer has upgraded.
Common Mistakes
Registering a schema without checking compatibility against the subject first
Fix: Call the compatibility endpoint in CI. Registration is the commit; validation is the review, and doing them in that order lets an incompatible change reach a running producer.
Removing and renaming fields in one release
Fix: Add the new field with a default, dual-read, switch the writer, then remove the old field in a separate release after all consumers have upgraded.
Assuming a field rename is harmless because the type is unchanged
Fix: Avro resolves by field name, so a rename drops the value for readers using the previous schema. Add a default and treat it as an add plus a remove.
Using RecordNameStrategy by default to be flexible
Fix: Topic naming keeps one compatibility history per topic, which matches how consumers actually read. Record naming splits history per type and hides conflicts until a shared topic appears.
Best Practices
- Set BACKWARD as the subject default and reserve transitive levels for topics with long consumer rollouts
- Resolve schemas with a local cache so the registry is not on the per-message path
- Keep schemas in version control and register them from the build, never by hand from a laptop
- Prefer logical types such as decimal and timestamp over raw bytes or longs so semantics survive evolution
- Log the schema id alongside the payload in your dead letter queue so failures remain decodable later
Interview Questions
Q1: What is on the wire in front of an Avro payload?
A magic byte of zero followed by a four byte big endian schema id. The consumer resolves that id through a cached lookup to fetch the writer schema, so no schema travels with each record.
Q2: Backward or forward compatibility for a topic where consumers upgrade slowly?
Backward, and transitive if consumers can lag several versions. Backward means the new schema reads old data, so a consumer still on the previous version keeps working while producers move ahead.
Q3: Why does adding an enum symbol without a default break compatibility?
A reader holding the earlier schema has no representation for the unknown symbol. Providing a default symbol lets the reader fall back, which restores backward compatibility.
Q4: What happens when a producer registers an incompatible schema?
Registration is rejected with a 409 and the version is not created, so no record can be written with it. The failure surfaces at deploy time rather than at read time, which is the entire point of the registry.
Q5: When would you use JSON Schema instead of Avro?
When consumers are not Kafka clients and need to inspect records directly, or when validation on read matters more than payload size. The cost is a larger record and no id-based resolution.
Key Takeaways
- The registry stores schemas once and puts only an integer id on the wire, resolved through a client-side cache
- Compatibility is checked at registration, which moves schema failures from production into the build
- Backward means new reads old, forward means old reads new, and rolling deploys want backward
- Defaults are what make additions and removals safe; a change without a default is usually a breaking change
- Migrate in three steps: add, dual-read and write, then remove