How-To Guide

How to Version Avro Schemas with a Registry

Evolve event schemas safely with a schema registry: compatibility modes, writer and reader schemas, and a CI check before publish.

On this page

  1. Introduction
  2. Why a Registry and Why Avro
  3. The Evolution Rules
  4. Compatibility Modes
  5. Registering and Versioning
  6. CI Checks Before Publish
  7. Handling a Necessary Breaking Change
  8. Common Pitfalls and Conclusion

Introduction

Event-driven systems depend on a shared understanding of each event’s shape. When one producer’s schema changes, every consumer must either tolerate the change or break. A schema registry turns that understanding into a managed contract: schemas are registered and versioned, and compatibility is checked before a producer can publish a schema that would break consumers. Avro, with its writer and reader schema separation, is designed for exactly this evolution, provided the compatibility rules are respected.

Why a Registry and Why Avro

Without a registry, each producer embeds its schema in the message and each consumer reads it as it happens to be, which means a schema change is discovered as a runtime failure rather than a deploy-time check. A registry stores the schemas, assigns ids, and enforces compatibility so a breaking change is rejected before it ships.

Avro is well suited because it separates the writer schema, used when the data was encoded, from the reader schema, used when it is decoded. The reader knows how to project the writer’s data into its own shape, so a reader using an older schema can read newer data within the compatibility rules. The registry supplies the writer schema by id when decoding.

# with a registry
producer -> register schema v2 -> registry checks compatibility
         -> compatible? assign id, allow -> producer sends id + data
consumer -> fetch schema v2 by id -> read with its own reader schema
# incompatible schema => rejected at registration, before deploy
# the registry is the contract; the id links each message to its schema

The Evolution Rules

Avro compatibility is defined between the writer and reader schemas. Adding a field with a default is backward compatible: an old reader using the new data gets the default. Removing a field with a default is forward compatible. Renaming a field breaks both unless you use aliases. Changing a type is usually breaking.

These rules are why defaults matter: a new field must have a default so older readers can decode messages that lack it, and a removed optional field can be defaulted for older data. The registry enforces a chosen compatibility mode against the latest or all previous versions, which turns the abstract rules into a registration-time check.

// Backward compatible: added field has a default.
{
  "type": "record", "name": "OrderPlaced",
  "fields": [
    { "name": "order_id", "type": "string" },
    { "name": "amount",   "type": "long" },
    { "name": "currency", "type": "string", "default": "USD" }  // safe add
  ]
}
// BREAKING: removing a field with no default, or renaming without an alias.

Compatibility Modes

Backward compatibility means new consumers can read old data, which is the common default for a growing consumer base. Forward compatibility means old consumers can read new data. Full compatibility means both directions work, allowing any publisher and any consumer version to coexist. The mode you choose determines what changes are allowed.

Pick a mode that matches your rollout: if consumers upgrade slowly, you want backward and maybe full; if producers and consumers deploy independently and at different times, full is safest. Configure the mode per subject, and document it, because a change to a subject’s mode changes what future publishes are allowed to do.

# registry compatibility modes
NONE          any change allowed (no safety)
BACKWARD      new consumers read old data  (default for many teams)
FORWARD       old consumers read new data
FULL          both  (any producer/consumer version coexist)
# choose per subject from your rollout reality; document it
# changing a subject's mode changes what future publishes may do

Registering and Versioning

A producer registers its schema under a subject, typically the topic name plus a strategy such as key or value. The registry assigns a schema id and a version. Each message carries the schema id, so a consumer decodes with the exact writer schema that produced it, regardless of what the latest version is now.

Versioning is monotonic per subject: a new schema that is compatible becomes the next version. Because messages carry an id rather than a full schema, payloads stay small, and because the id resolves to a specific schema, consumers are not affected by later versions they have not adopted.

# Register a value schema for a topic; the registry checks compatibility.
curl -X POST "$REGISTRY/subjects/orders-value/versions" \
  -H 'Content-Type: application/vnd.schemaregistry.v1+json' \
  -d "{\"schema\": $(jq -Rs . < order-placed.v2.avsc)}"
# => { "id": 12 }  (assigns an id; rejects if incompatible with the mode)

# Check compatibility BEFORE registering, in CI:
curl -X POST "$REGISTRY/compatibility/subjects/orders-value/versions/latest" \
  -H 'Content-Type: application/vnd.schemaregistry.v1+json' \
  -d "{\"schema\": $(jq -Rs . < order-placed.v2.avsc)}"

CI Checks Before Publish

The registry protects at registration, but registration happens at deploy. Add a CI check that posts the proposed schema to the compatibility endpoint and fails the build if it is incompatible, so the producer author sees the failure in a pull request rather than at deploy.

Also validate producer and consumer schemas against a test message in CI, so the round trip is proven before deploy. A schema that passes the registry’s compatibility check can still be wrong in application code, so the CI check complements the registry rather than replacing it.

# CI step: fail the PR if the schema is not compatible.
- name: Check schema compatibility
  run: |
    curl --fail-with-body -sS -X POST \
      "$REGISTRY/compatibility/subjects/orders-value/versions/latest" \
      -H 'Content-Type: application/vnd.schemaregistry.v1+json' \
      -d "{\"schema\": $(jq -Rs . < order-placed.v2.avsc)}"
    # a non-2xx response fails the build -> the author fixes it pre-deploy

Handling a Necessary Breaking Change

Sometimes a breaking change is genuinely needed. Do it with a new subject or topic rather than forcing an incompatible change on the existing one, so old consumers keep working while new producers and consumers adopt the new schema. Migrate consumers over time, then retire the old subject.

Alternatively, use a transformation layer that maps between the old and new shapes, or keep both fields during a transition and deprecate the old one with a deadline. The point is that a breaking change becomes an explicit migration with a plan, not an incompatible registration that fails at deploy and surprises everyone.

# breaking change, done safely
option A: new subject/topic  orders-value-v2
          old consumers keep reading orders-value; new adopt v2
          migrate consumers, then retire orders-value
option B: transition fields   add new field, keep old, dual-write
          deprecate old with a deadline, then remove
# never force an incompatible change into an existing subject

Common Pitfalls and Conclusion

Do not register without a compatibility mode; NONE lets a breaking change through. Do not add a field without a default if backward compatibility is required. Do not rename a field without an alias. Do not rely only on the registration-time check; add a CI check so the author catches it pre-deploy. And do not force a breaking change into an existing subject when a new subject handles it safely.

A schema registry turns event schemas into a managed, versioned contract with compatibility enforced at registration. Adopt Avro for its writer/reader separation, choose a compatibility mode that matches your rollout, keep defaults on added fields, check compatibility in CI, and handle the occasional breaking change as a new subject with a migration. Then producers and consumers can evolve without surprising each other.

Sources: Confluent: Schema Registry, Avro: Schema resolution and compatibility, Confluent: Schema evolution and compatibility.

FAQ

Why does adding a field need a default?
Because an old reader decoding new data that lacks the field must supply something; the default is what it supplies. Without a default, adding a field is not backward compatible, and the registry rejects it under a backward or full mode.
What do I do about a schema change that is genuinely breaking?
Publish it as a new subject or topic so existing consumers keep working, migrate consumers over time, then retire the old subject. Alternatively keep both fields during a transition with a deprecation deadline. Never force an incompatible change into an existing subject.

Related Examples