What you'll learn: By the end of this lesson you will be able to explain why Avro data cannot be read without a schema, describe how a reader schema and a writer schema are resolved against each other, say exactly what BACKWARD, FORWARD, FULL and the transitive variants promise and which side of a topic must upgrade first, predict whether a change such as adding, removing or renaming a field is safe, and explain what a schema registry adds on top, using a Java lab that runs Avro's own compatibility checker on ten real changes.
Picture a restaurant chain where head office sends order cards to every kitchen. Each card is a row of boxes: order number, customer, total. One day head office adds a "currency" box. Kitchens that still hold the old card template must not crash on the new cards, and kitchens that got the new template must still cope with the old cards that are waiting in the pile. Nobody can reprint every template at the same moment, so the rule for changing the card has to work while old and new templates are both in use.
That is schema evolution. A Kafka topic keeps events for days or longer, and the services that write and read them deploy on their own schedules. A message is only bytes, so the "template" has to be agreed somewhere. This lesson is about Avro, the format whose rules for that agreement are the clearest, and about the schema registry that stores the templates and refuses unsafe changes. It builds on the ideas in event streaming and message queues; the lesson on Kafka internals and the lesson on schema evolution in relational databases cover the other halves and are named here in plain text only.
Verified with Java 25, Apache Avro 1.12.2, the Apache Avro specification 1.12.0 and the Confluent Schema Registry documentation, on 2026-12-01.
The core idea
Avro data carries no field names, so a reader always needs the schema the writer used. Schema evolution is the rule for pairing that writer schema with a different reader schema. A change is compatible when the reader can still decode what the writer wrote, and a registry is the shared place that stores every writer schema, gives it an ID, and checks that rule before it accepts a new version.
Three illustrations run through the lesson:
- Minimal example: the
OrderPlacedevent in three versions, with the compatibility result of ten changes and real encoded bytes, from the Java lab. - Realistic example: ShopStream adds a currency to
OrderPlacedwhile three consumers deploy on their own schedules, in "In practice". - Failure example: a field added without a default breaks the first consumer that reads an old event.
Why events need schemas
Kafka stores the bytes a producer hands it. It does not know whether they hold a total in cents or a name. Without an agreement, every consumer guesses, and the first change to a field breaks a consumer somewhere. JSON hides the problem for a while, because field names travel with every message, but it also gives no guarantee: nothing stops a producer from renaming a field.
Avro makes the agreement explicit. The Avro specification says "Binary encoding does not include field names" and that "readers are wholly reliant on the schema used when the data was encoded." It also says "a schema must always be used in order to read Avro data correctly." A record is written as its field values in the order they are declared, with integers and longs in variable-length zig-zag coding and a string as a length followed by that many UTF-8 bytes. The result is compact, and useless without the schema.
Figure 1. The registry sits beside the topic, not in the data path. How to read this: the producer registers its schema and gets back an ID, then sends only the ID and the compact bytes to the topic. A consumer reads the ID, fetches the matching schema from the registry, and decodes. The registry is described from the Confluent documentation below; the lab does not run one.
Writer schema, reader schema, resolution
When data is read, two schemas are in play: the writer's schema, which describes the bytes, and the reader's schema, which describes the shape the consumer wants. Avro resolves one against the other. The specification's rules for records are the heart of schema evolution:
- Fields are matched by name: "the ordering of fields may be different: fields are matched by name."
- A field the writer has and the reader lacks is skipped: "the writer's value for that field is ignored."
- A field the reader has and the writer lacks gets the reader's default: "then the reader should use the default value from its field." If the reader's field has no default, an error is signalled.
- Types may be promoted:
inttolong,floatordouble;longtofloatordouble;floattodouble;stringandbytesto each other. - Enums: if the writer's symbol is not in the reader's enum, the reader's enum default is used when it has one, otherwise an error is signalled.
- Names can be bridged with aliases: "Aliases function by re-writing the writer's schema using aliases from the reader's schema."
A default is only a reading rule. The specification says a field's default is "only used when reading instances that lack the field for schema evolution purposes" and that "the presence of a default value does not make the field optional at encoding time." An optional field is a union with null, and a union's default corresponds to its first branch, so the usual spelling is ["null", "string"] with "default": null.
Compatibility types
A registry turns resolution into a policy by asking one question about a pair of schemas, in one direction or both. The Confluent documentation defines the types in terms of consumers and producers:
| Type | What it promises | Upgrade order |
|---|---|---|
BACKWARD |
A consumer on schema X can process data produced with X or X-1 | Consumers first |
BACKWARD_TRANSITIVE |
The same, for X, X-1 and X-2 and every earlier version | Consumers first |
FORWARD |
Data produced with X can be read by consumers on X or X-1 | Producers first |
FORWARD_TRANSITIVE |
The same across all earlier versions | Producers first |
FULL |
Backward and forward compatible between X and X-1 | Any order |
FULL_TRANSITIVE |
Backward and forward compatible across all versions | Any order |
NONE |
Compatibility checks are disabled | Caution needed |
The default is BACKWARD, not BACKWARD_TRANSITIVE: only the latest earlier version is checked. A Kafka Streams application supports only the backward and full types, and must be upgraded before the upstream producer.
In Avro's vocabulary the two directions are the same check asked with the roles swapped. BACKWARD asks whether a reader on the new schema can decode data written with the old one. FORWARD asks whether a reader on the old schema can decode data written with the new one. The lab below wraps Avro's SchemaCompatibility.checkReaderWriterCompatibility(reader, writer), which its Javadoc describes as validating that the reader schema "can be used to decode avro data written with the provided writer schema."
Figure 2. From upgrade order to compatibility type. How to read this: start at the top and answer the two questions. The last question is the one teams forget: a topic that retains old events, or a consumer that replays from the beginning, needs the transitive variant, because it will meet every old version.
Build it: ten changes through Avro's checker
This is the minimal example, a model you can run. No schema registry server runs here, so the lab uses the Apache Avro library only: it parses real schemas, asks Avro's own checker about each change in both directions, and encodes and decodes real bytes.
Goal. See which changes are BACKWARD, FORWARD or FULL compatible, why the others are not, how a transitive check differs, and what the same rules do to bytes.
Setup. From the repository's examples/spring-labs folder:
cd examples/spring-labs && ./gradlew :labs:avro-schema-compatibility:test
The test run executes 11 tests and compares a report with examples/spring-labs/golden/avro-schema-compatibility.txt. The code is in examples/spring-labs/labs/avro-schema-compatibility/. It needs no Docker and no Kafka, and it does not talk to a registry.
How the model works. OrderSchemas holds the OrderPlaced schema in each version as JSON text. Evolution is the whole compatibility model: BACKWARD is the check with the new schema as reader and the old as writer, FORWARD swaps them, FULL needs both, and the transitive check compares the newest schema with every earlier one.
public static Check backward(Schema oldSchema, Schema newSchema) {
return check(newSchema, oldSchema);
}
public static Check forward(Schema oldSchema, Schema newSchema) {
return check(oldSchema, newSchema);
}
Steps. Run the tests, then read the report. The first block is one change at a time, in both directions.
Expected output, captured from a real run (Apache Avro 1.12.2, Java 25, 2026-12-01):
change (old -> new) BACKWARD FORWARD FULL
add field with default yes yes yes
add field, no default NO yes NO
add optional field (null union) yes yes yes
remove field, no default yes NO NO
remove field that had a default yes yes yes
widen int to long yes NO NO
rename field, no alias NO NO NO
rename field with alias yes NO NO
add enum symbol yes NO NO
change long to string NO NO NO
Why the failing cells fail (Avro incompatibility type and location in the reader/writer pair):
add field, no default, BACKWARD fails: READER_FIELD_MISSING_DEFAULT_VALUE at /fields/3
remove field, no default, FORWARD fails: READER_FIELD_MISSING_DEFAULT_VALUE at /fields/1
widen int to long, FORWARD fails: TYPE_MISMATCH at /fields/1/type
rename field, no alias, BACKWARD fails: READER_FIELD_MISSING_DEFAULT_VALUE at /fields/1
rename field, no alias, FORWARD fails: READER_FIELD_MISSING_DEFAULT_VALUE at /fields/1
rename field with alias, FORWARD fails: READER_FIELD_MISSING_DEFAULT_VALUE at /fields/1
add enum symbol, FORWARD fails: MISSING_ENUM_SYMBOLS at /fields/1/type/symbols
change long to string, BACKWARD fails: TYPE_MISMATCH at /fields/2/type
change long to string, FORWARD fails: TYPE_MISMATCH at /fields/2/type
Read the table row by row:
- Add a field with a default is FULL. Old data gets the default, and an old reader ignores the extra field.
- Add a field without a default is forward only. A new reader meets old data that lacks the field and has nothing to put there, which is
READER_FIELD_MISSING_DEFAULT_VALUE. - Add an optional field (a
nullunion with anulldefault) is FULL, and is the usual way to add something truly optional. - Remove a field with no default is backward only: an old reader still requires the field that new data no longer has.
- Remove a field that had a default is FULL, which is why a default today buys you the right to remove the field later.
- Widen
inttolongis backward only, because a promotion goes one way. - Rename a field breaks both ways. An alias on the new name makes it backward compatible, and only backward: old readers never learn the new name.
- Add an enum symbol is backward only, because an old reader meets a symbol it does not know.
- Change a type to an unrelated one breaks both.
The second block is the transitive case. Each adjacent step is fine, and the chain is not:
Expected output, captured from a real run (Apache Avro 1.12.2, Java 25, 2026-12-01):
v2 reads v1 data (BACKWARD, v1 -> v2): yes
v3 reads v2 data (BACKWARD, v2 -> v3): yes
v3 reads v1 data (BACKWARD, v1 -> v3): NO
BACKWARD for v3 (latest version only): yes
BACKWARD_TRANSITIVE for v3 (all): NO
Version 3 makes couponCode required again by dropping its default. Version 3 can read version 2 data, so a plain BACKWARD check against the latest version passes. It cannot read version 1 events, which have no couponCode, so the transitive check fails. A topic that still holds version 1 events would hand them to the new consumer.
The third block is the same rules on real bytes, written and read through Avro's binary encoding and its resolving reader.
Expected output, captured from a real run (Apache Avro 1.12.2, Java 25, 2026-12-01):
v1 record encoded with v1: 13 bytes, 0c6f2d3130303106632d37c413
v1 bytes, writer v1, reader v2: {"orderId": "o-1001", "customerId": "c-7", "totalCents": 1250, "currency": "EUR"}
v2 record encoded with v2: 17 bytes, 0c6f2d3130303206632d39bc0f06555344
v2 bytes, writer v2, reader v1: {"orderId": "o-1002", "customerId": "c-9", "totalCents": 990}
v1 bytes, writer v1, reader with required couponCode: AvroTypeException: Found shop.events.OrderPlaced, expecting shop.events.OrderPlaced, missing required field couponCode
v1 bytes read as if the writer were v2 (wrong writer schema): EOFException
The first record is 13 bytes: 0c is the length 6 in zig-zag form, then the six bytes of o-1001, then the length and bytes of c-7, then c413, the long 1250. No name appears anywhere. Reading it with the version 2 schema as the reader fills currency with EUR. Reading version 2 bytes with version 1 as the reader drops the field. A required field with no default makes the resolving reader throw. The last line is the failure the whole lesson exists to prevent: decoding bytes with the wrong writer schema. It threw here because the bytes ended early; a wrong schema that happened to fit the bytes would not throw at all.
Break it. In OrderSchemas, add "default": "none" to couponCode in V2_ADD_FIELD_NO_DEFAULT. The tests addingAFieldWithoutADefaultIsForwardOnly and theCheckerPredictsWhatTheResolvingReaderDoes and the golden comparison then fail, because the change has become FULL. Restore the line and run the tests again.
Stretch. Give the enum a default symbol and see whether adding a symbol becomes FORWARD compatible. Add a FORWARD_TRANSITIVE method.
What the model leaves out. A registry server, its REST API, subject naming, schema IDs and the wire format, schema references, Protobuf and JSON Schema, code generation from schemas, logical types, and the serializer classes. The lab checks schemas with the Avro library, not with the registry's implementation, so it shows the Avro rules, not registry output.
The registry: what it adds
What follows comes from the Confluent Schema Registry documentation; none of it was run for this lesson.
The registry is a service for "validating, storing, and retrieving Avro, JSON Schema, and Protobuf schemas." It uses Kafka as its storage backend: "all schemas, subject and version metadata, ID metadata, and compatibility settings are appended as messages to this log." The vocabulary has four terms:
| Term | Meaning (Confluent documentation) |
|---|---|
| Schema ID | A globally unique ID the registry assigns, "monotonically increasing and unique, but not necessarily consecutive" |
| Subject | "A named scope for schema evolution" |
| Version | "A position within a subject's sequence" |
| Subject name strategy | How the subject is derived; TopicNameStrategy is the default, with RecordNameStrategy and TopicRecordNameStrategy as alternatives |
Producers embed the schema ID in each message, and consumers use it to fetch the schema. The documented wire format starts each message with a version byte, 0 for the default format, followed by a 4-byte schema ID and then the serialized data. The serializers register schemas automatically by default (auto.register.schemas), which is convenient in development and worth turning off where schema changes should go through review.
The registry's job is the gate: "When a schema is updated, Schema Registry checks compatibility before accepting the new version." Under a non-transitive type the new schema is checked against the latest version only; under a transitive type, against all registered versions. The registry therefore catches the second block of the lab output at registration time, when a developer is at their desk, instead of at 2 a.m. in a consumer.
Figure 3. The path of one event through a registry. How to read this: the compatibility gate is at registration, before any event is written. The consumer asks the registry for the schema that matches the ID. The sequence is drawn from the documentation, not from a captured run.
Choosing a policy: the trade-offs
| Option | Gain | Cost | Wrong when |
|---|---|---|---|
BACKWARD (default) |
Consumers can upgrade first and read old events | Producers may not add fields without defaults, and old consumers may break on new data | Consumers cannot be upgraded before producers |
BACKWARD_TRANSITIVE |
A consumer can replay every retained version | A stricter gate; some past changes can never be undone | The topic holds only recent, one-version data |
FORWARD |
Producers can upgrade first | New events must stay readable by old consumers | You also need to replay old events |
FULL or FULL_TRANSITIVE |
Any upgrade order, safe replays | The narrowest set of allowed changes: defaults everywhere | Teams will not follow the rules and will bypass the registry |
NONE |
No friction | Nothing protects consumers | Any shared topic |
| Breaking change as a new topic | A clean break with a migration plan | Dual writes and a cut-over | The change is really additive |
In practice: ShopStream adds a currency to OrderPlaced
This is the realistic example, an educational scenario on the course's running example ShopStream; nothing in it describes a real company. Every number is illustrative.
This is a realistic scenario, not a case history.
The situation
ShopStream's order service publishes OrderPlaced events to a topic. Three consumers read it: a notification service, an analytics job that reprocesses the topic from the start each night, and the ledger service from the payments stage. Marketing wants a second currency for a pilot, so the event needs a currency field. The three consumers are owned by three teams and deploy on their own calendars.
Starting architecture
Figure 4. The OrderPlaced path before the change. How to read this: all four services share one schema contract through the registry. The analytics job replays the whole topic, so it will meet every version that is still retained.
Options considered
| Option | Behaviour | Risk |
|---|---|---|
A. Add currency with a default |
FULL: old events read as the default, old consumers ignore the field | The default must be true for every old event |
B. Add currency without a default |
Forward only; the registry under BACKWARD rejects it |
The nightly replay would fail on old events |
C. New topic orders-v2 |
Clean break | Dual writes, migration of three consumers |
The decision
Option A, with a default that is true for every event already written, because the pilot adds a second currency to a system that so far sold in one. The default is recorded in the schema review, and the subject is moved to FULL_TRANSITIVE for the pilot so the analytics replay and any later rollback stay safe. The team writes down the rule the lab table shows: a field is added with a default, and a default is removed only after the old events have expired from the topic.
Rollout and verification
Before deploying, the change is checked in the pipeline against every earlier version, the same question the lab's transitive block asks. The order is the one the documentation gives for the policy: with FULL any order works, so the producer ships first and the consumers pick up the new field when their teams get to it. Verification is a replay of a sample of old events through each consumer's current build, and a count of events whose currency is the default.
The 2 a.m. failure
The page reads: the ledger service is in a crash loop on one partition, with an AvroTypeException about a missing required field. During the pilot a developer added a couponCode field without a default, and, to get past the registry's rejection, set the subject's compatibility to NONE "for one deploy" and never set it back. The new consumer build now fails on every old event, exactly as in the lab's failing decode.
The checks: read the subject's compatibility setting, compare the failing event's schema ID with the registered versions, and find the first version that lacks the field. The mitigation is to roll the consumer back, restore a transitive compatibility setting, and register a corrected schema where the field has a default. The lasting fix is to make compatibility settings changes a reviewed action, and to run the lab-style check on every pull request that touches a schema.
Architect's notes
- Default everything you add. A default is the price of the right to evolve in both directions; an optional field is a union with
nullfirst. - Never rename in place. A rename is an add plus a remove. Add the new field with a default, move readers over, and remove the old one later.
- Pick the policy from the upgrade order and the retention. Retained or replayed data needs the transitive variant.
- Treat
NONEas an incident, not a setting. If it is ever needed, put a date on it. - Check against the real subject. The Avro library is a good build-time safety net, but it is not the registry's answer: also register against a registry in a test environment.
- Check the version. Defaults such as the compatibility type and auto-registration are documented per release.
Common mistakes
- Adding a field without a default and discovering it when old events are read.
- Assuming BACKWARD protects old consumers. It protects new consumers reading old data; protecting old consumers reading new data is FORWARD.
- Using the plain check when events are replayed. The latest-version check passes while version 1 events still break the new reader.
- Renaming a field in one step. It breaks both directions unless an alias is declared, and an alias helps only the reader that carries it.
- Decoding with a guessed schema. The bytes carry no names, so a reader that guesses the writer's schema can fail, or succeed with wrong values.
- Auto-registering schemas from every environment. A developer's laptop can then register the version that production must live with.
Key takeaways
- Avro bytes have no names; the reader needs the writer's schema, and resolution pairs it with the reader's schema.
- Defaults decide almost everything: new fields need them to be backward compatible, removed fields must have had them to be forward compatible.
- BACKWARD means new readers read old data, FORWARD means old readers read new data, FULL means both, and the transitive variants check every earlier version.
- The upgrade order follows the type: consumers first for BACKWARD, producers first for FORWARD, any for FULL.
- A registry gives schemas IDs and subjects, and rejects an incompatible version at registration; it does not replace thinking about the policy.
- Check schemas in the build with the same rules, and treat a disabled compatibility check as an incident.
Practice exercises
Exercise 1 (20 min). For each change in the lab table, write which consumers or producers must upgrade first under BACKWARD, and what breaks if the order is reversed.
Exercise 2 (45 min). Run the lab. Add a new Change that adds an optional giftMessage field, and a second that turns totalCents from long to double. Predict both results, then run.
Exercise 3 (1 hour). Run "Break it". Then create a four-version chain where each adjacent pair is FULL and the first and last are not, and show it with Evolution.
Exercise 4 (2 hours). For ShopStream's OrderPlaced, StockLevelChanged and CourierLocation topics, choose a compatibility type, a subject name strategy and a retention, and write what a breaking change costs each one.
Related reading
- Event Streaming: topics, partitions and consumers.
- Message Queues: the queue side of messaging.
- Synchronous vs Asynchronous Communication: why consumers and producers deploy separately.
- Microservices Decomposition and Data Ownership: who owns an event's contract.
Official documentation
- Apache Avro, Specification 1.12.0: binary encoding, schema resolution, aliases and field defaults.
- Apache Avro, SchemaCompatibility Javadoc: the reader/writer compatibility check the lab calls.
- Confluent, Schema evolution and compatibility: the compatibility types, upgrade order and registration-time checks.
- Confluent, Schema Registry fundamentals: storage, IDs, subjects and versions.
- Confluent, Serializers and deserializers: the wire format and subject name strategies.