Schema evolution and upcasting
Customers can ask for express delivery, and OrderPlaced gains a field. Stored events keep the shape they were written in and are upcast when read; published events change only in ways the schema registry accepts against every earlier version.
Customers can now ask for express delivery, so OrderPlaced gains a shippingPriority. Two kinds of reader already hold the old shape. The aggregate loads OrderPlaced rows that were written before the field existed and can never be rewritten. Every consumer of edp.orders.placed may replay the topic from its first record, and the first records have no shippingPriority either. One order was placed by the previous release, another by the current one, and both have to work.
Learning objectives
- Transform a stored event into the current shape on read without rewriting what was recorded
- Give every added field a default that states an explicit business decision
- Register a schema change against every earlier version the topic can still replay
- Distinguish a compatible field addition from a change that needs a new event type
Why this matters
When a new field is added to an event, the store never rewrites what it already recorded, and some consumers of the public topic may still be running a version deployed weeks or months ago. If the transform is wrong, old records stop being readable at all, breaking the aggregate’s own load path and every downstream service still running the previous release. Ship an incompatible schema, and the cost is a failed command and a service that can no longer read its own history or publish an event.
Pattern: Upcaster
The problem
The event store is append-only. A trigger refuses UPDATE and DELETE. An event is a fact that happened, and the history is worth keeping only while nobody edits it. A new field is then a problem with no obvious place to fix it.
Each way out gives something up. Rewriting the old rows with a migration, Young’s “Copy and Replace”, breaks the rule that made the store trustworthy, and it cannot be undone. Teaching the aggregate every shape, one branch per version in every decision, his “Basic Type Based Versioning”, spreads the history of the schema through code that should only know the order. Making every field optional and defaulting it where it is used, his “Weak Schema”, scatters the same default across the code base.
The solution
An upcaster turns an event stored in an earlier shape into the same event in the current shape, on a copy, when the event is read, so the store keeps what was written and the code sees one shape.
Each row records the schema_version it was written in. The chain holds one step per version, each a small transform from one shape to the next, and a row stored at version 1 passes through every step up to the current version. An unknown version is an error. The default a step supplies is a business decision, the priority an order had before customers could choose, so it is written once, next to the step, and the published contract states the same value.
Upcasting happens on every load and costs a copy of the payload. In exchange the stored history stays exactly as it was recorded, and a bug in a step is fixed in code, with nothing to repair.
How Quarkus and Camel model it
Upcasters is an application-scoped bean. The chain is a map from event type and version to a transform of the JSON node, and toCurrent applies the steps that lie between the stored version and the current one:
private static final Map<String, Map<Integer, UnaryOperator<ObjectNode>>> CHAIN = Map.of(
"OrderPlaced", Map.of(1, node -> node.put("shippingPriority", DEFAULT_PRIORITY)));
ObjectNode node = ((ObjectNode) stored).deepCopy();
for (int from = schemaVersion; from < CURRENT; from++) {
UnaryOperator<ObjectNode> step = CHAIN.getOrDefault(type, Map.of()).get(from);
if (step != null) {
node = step.apply(node);
meters.counter("order.upcasts", "from", String.valueOf(from)).increment();
}
}
EventStore.load upcasts each row and only then maps the node to a record, so the aggregate never sees an old shape. The current record refuses a missing field:
record OrderPlaced(Order order, String shippingPriority) implements OrderEvent {
public OrderPlaced {
Objects.requireNonNull(shippingPriority, "shippingPriority is missing: ...");
}
}
That constructor is what fails when the chain is skipped, with a message that names the cause. append always writes the current schema_version, and a command on a stream that starts with a version 1 row appends version 2 events behind it. A stream can hold both shapes for as long as it lives.
The step is plain Java in the store and no Camel type appears in it. Camel belongs to the wire.
Pattern: Compatible schema evolution
The problem
OrderPlaced also leaves the service. The outbox carries it to edp.orders.placed, and a consumer such as notification-service is deployed on its own schedule. When the producer adds a field, some consumers run the old code and some the new, and the topic holds records of both shapes. A reader must be able to read a record it did not expect.
Documentation and code review do not hold this line. A change that looks harmless to its author can break a reader the author has never seen, and the break shows up in a consumer that fails on a record written months earlier. Bellemare’s chapter “Schemas and Data Contracts” and Lawrence’s “Evolution of Messages and Message Streams” treat the same problem at book length.
The solution
A compatible schema evolution registers every new schema version in a registry that checks it against the earlier versions, so a reader built on the newest schema can read every record ever published.
Avro resolves a record written with one schema into a reader’s schema by field name. A field the writer did not have takes the reader’s default, and a reader field without a default is an error. So the rule for adding a field is that it always has a default. The registry enforces it, using a compatibility rule that the owner of the contract sets in the registry. The producer does not choose it.
The rule has two strengths. BACKWARD checks a new version against the latest one, and BACKWARD_TRANSITIVE checks it against all of them. The second is the one a topic that can be replayed from the start needs, because a replaying reader meets the oldest records.
How Quarkus and Camel model it
The contract is order-placed.avsc. The new field has a default, equal to the one Upcasters uses:
{ "name": "shippingPriority", "type": "string", "default": "STANDARD" }
Quarkus compiles the schema into the OrderPlaced class the code uses. OrderPlacedPublisher serializes it inside the command’s transaction with the Apicurio Avro serializer, which registers the schema in edp.orders.placed-value and writes the schema id into the first bytes of the payload. The outbox row keeps those bytes:
quarkus.debezium-outbox.payload.type=binary
quarkus.debezium-outbox.payload.column-definition=bytea
quarkus.debezium-outbox.additional-fields=topic:string
The connector must not turn the bytes into JSON on the way out. It routes the row by its topic column and passes the payload through:
"value.converter": "io.debezium.converters.BinaryDataConverter",
"transforms.outbox.route.by.field": "topic",
"transforms.outbox.route.topic.replacement": "${routedByValue}"
The serializer runs before the commit, so a registry that refuses the schema throws and the append and the outbox row roll back together. Serializing in the connector or in a consumer would find out later and leave an event nobody can publish.
notification-service reads with the schema it was built on. A SmallRye channel with the generated class as its parameter type is enough, and Quarkus selects the Apicurio deserializer and the specific reader:
@Incoming("placed")
@Blocking
public void on(OrderPlaced placed) {
recorder.record(placed, "smallrye");
}
A record written without the field arrives with STANDARD, filled in by Avro resolution. The Camel route configures the deserializer, the registry and the specific reader explicitly:
from("kafka:" + Topics.ORDERS_PLACED + "?groupId=notification-camel&autoOffsetReset=earliest"
+ "&valueDeserializer=io.apicurio.registry.serde.avro.AvroKafkaDeserializer"
+ "&additionalProperties.apicurio.registry.url=" + registryUrl
+ "&additionalProperties.apicurio.registry.use-specific-avro-reader=true")
.routeId("order-placed-reader")
.autoStartup("{{reader.camel.enabled}}")
.process(exchange -> recorder.record(exchange.getIn().getBody(OrderPlaced.class), "camel"));
Both readers end in the same Recorder. The route starts with the camel-reader profile and has its own consumer group, which reads from the earliest offset. Choose SmallRye when the channel is all the service does, and the route when the record joins other Camel processing. The Avro handling is identical.
Refusing an incompatible version
The next release removes the default from shippingPriority: the field is required, because every order now has one. Against version 2 that is compatible, since a reader on the new schema reads version 2 data, in which the field is present. Against version 1 it is not, because version 1 has no value to give the reader.
So BACKWARD accepts it and BACKWARD_TRANSITIVE refuses it. The registry applies a rule when a version is added, to the versions already registered, so version 1 and version 2 are registered before the check means anything: a first version on an empty artifact is accepted whatever it contains.
The refusal happens in two places. A direct registration of the version answers HTTP 400, and the body names the field and says Incompatible artifact, without naming the rule. And a service that publishes the bad schema fails at the command:
POST /orders -> 503 {"reason": "Incompatible artifact: edp.orders.placed-value [AVRO] ..."}
The order has no event row, there is no outbox row, nothing reaches the topic and the artifact has no new version. A rule set on one artifact overrides the global rule, so verify.sh sets a global rule from REGISTRY_RULE (demo code), and REGISTRY_RULE=BACKWARD changes only that value.
Accepting the broken version has a cost that only shows later. A consumer built on version 3 that rebuilds from the start of the topic meets version 1 records with no shippingPriority and nothing to fill in, and Avro fails with AvroTypeException. A rebuild that reads every record, as a projection rebuild does, is where it surfaces.
Build, run, observe
cd examples/18-schema-evolution-and-upcasting
mvn -B verify
./verify.sh
verify.sh runs the previous, the current and a broken release against a registry that enforces compatibility, then recreates notification-service on the Camel reader. Watch old rows and records stay readable and the broken release get a 503. It prints 21/21 checks passed.
Try one change and rerun:
UPCASTERS=off ./verify.sh(demo code): the chain is skipped and loading a version 1 row fails, because the current record refuses the missing field.REGISTRY_RULE=BACKWARD ./verify.sh(demo code): version 3 is registered and published, and a reader built on it cannot read the version 1 records on the topic.
When not to use it
An upcaster cannot invent a fact. When a change alters what an event means, not only what it carries, a default would state something that was never recorded, and a new event type is the correct answer. A chain that grows a step per release is also a cost, because every load runs every step.
Skip the work where nothing is read back. A snapshot holds derived state and is discarded and rebuilt when its schema changes, so it needs no upcaster.
A registry is one more service to run and to keep up. When producer and consumers always deploy together and nothing replays a topic, a compatibility check in the build can do the same job with less.
Common pitfalls
- Skipping the upcaster chain leaves an old stored row without a field the current record requires, failing the load with a missing-field error.
- Registering a schema under BACKWARD instead of BACKWARD_TRANSITIVE accepts a change that a reader cannot apply to the oldest records once the topic is replayed from the start.
- Removing a field’s default to make it required breaks every consumer that rebuilds from the beginning of the topic, since the earliest records never had a value to give it.
- Treating a changed meaning as a field default invents a fact that was never recorded, when a new event type is the only accurate way to represent it.
🤖 Explore with your agent “In examples/18-schema-evolution-and-upcasting, load an order placed by the previous release, before shippingPriority existed, and one placed by the current release, and find where Upcasters, OrderPlacedPublisher and order-placed.avsc decide what each reader sees. Set REGISTRY_RULE to BACKWARD and state which schema version is then accepted and which reader fails on which record.” Check: stored events are upcast on read and never rewritten, published events change only in ways the registry accepts against every earlier version, and a field added later always has a default.
References
- Greg Young, Versioning in an Event Sourced System, Leanpub: chapters “Why Version?”, “Basic Type Based Versioning”, “Weak Schema” and “Copy and Replace”
- Alex Lawrence, Message- and Event-Driven Systems with CQRS and Event Sourcing, Addison-Wesley, 2026, chapter 8: Evolution of Messages and Message Streams
- Adam Bellemare, Building Event-Driven Microservices, 2nd ed., O’Reilly, 2025, chapter 4: Schemas and Data Contracts
- Martin Kleppmann and Chris Riccomini, Designing Data-Intensive Applications, 2nd ed., O’Reilly, 2026
- Apache Avro, Specification: schema resolution
- Apicurio Registry, content rule reference, schema compatibility modes and Kafka serializers and deserializers
- Quarkus, Using Apache Kafka with Schema Registry and Avro
- Debezium, Outbox Event Router (Avro as the payload format) and Outbox Quarkus extension
- Apache Camel, Kafka component
- Events, commands and documents
- Transactional outbox
- Aggregate root and the event store
- Snapshots
- Aggregate replay or CQRS projection