Skip to content

Lesson 3 of 9 · 25m

Schema Evolution Without Breaking Consumers

Treat schemas as contracts, classify changes by compatibility, and version data so producers and consumers can move independently.

Schema evolution is not a serialization detail. It is a negotiation between teams that deploy at different times. I have seen a harmless-looking rename — customer to customer_id — cascade through Kafka consumers, dbt models, warehouse tables, and executive dashboards before breakfast. The mistake was not the rename. The mistake was assuming everyone deployed together. They never do.

Schemas are compatibility contracts

A consumer may be old, new, or somewhere in between. Compatibility is directional:

backward compatible    new schema can read data written by old schema
forward compatible     old schema can read data written by new schema
full compatible        both directions hold
breaking               some deployed reader or writer can fail

Adding an optional field is usually safe. Renaming a field, changing its meaning, tightening a type, or reusing a field number is often not. "It is still a string" is not enough if active changed from "yes" to "true".

Prefer additive, explicit evolution

Avro and Protobuf make the rule concrete:

message OrderEvent {
  string order_id = 1;
  string customer_id = 2;
  int64 event_time_ms = 3;
  optional string discount_code = 4;  // additive, optional
}

Never repurpose field number 4 after it ships, even if the field is removed. Reserve it. In JSON, additive optional fields remain the safest pattern because many consumers deserialize loosely but still enforce business expectations downstream.

Registry compatibility is necessary, not sufficient

A schema registry can reject incompatible wire changes:

curl -s -X POST http://schema-registry:8081/config/orders.v1-value -H 'content-type: application/vnd.schemaregistry.v1+json' -d '{"compatibility":"BACKWARD_TRANSITIVE"}'

That only validates mechanical decoding. It cannot know that price_cents switched from net to gross, or that country changed from ISO-2 to a free-form warehouse label. Semantic compatibility requires review, documentation, and often a metric comparing output distributions before rollout.

Migrations need parallel existence

For warehouse schemas, avoid destructive in-place edits when downstream reads matter:

ALTER TABLE analytics.orders ADD COLUMN customer_id_v2 STRING;

-- backfill customer_id_v2
-- migrate consumers one at a time
-- only remove the old column after usage reaches zero

For event streams, a new topic or event version is often cleaner than mutating meaning in place. orders.v2 sounds bureaucratic until the alternative is three consumers interpreting the same bytes differently.

Field notes

  • Default values save migrations; missing defaults force every old record through special-case logic.
  • Type widening is not universally safe. int32 to int64 may be fine in Avro and catastrophic in a language-specific consumer.
  • Enum additions can break exhaustive switch statements even when decoding succeeds.
  • When in doubt, add a new field and deprecate the old one. Renames are cosmetic benefits with migration-sized costs.

Was this lesson helpful?

Sign in to rate