LESSON 06 · Schema evolution

Change a schema while old data exists

Old and new code read each other’s records during a rollout, and a log keeps old records for longer, so every schema change needs a compatibility direction and a deployment order.

SYSTEM VIEW

The data flow

Diagram view
Checkout—
Billing—
Search—

Watch an order schema lose its coupon field while old and new records share one log, and see why consumers must upgrade before producers.

  1. Checkout writes order records to the Orders topic in Avro, and Billing and Search read them. Schema v1, kept in the registry, has a coupon field with no default. Order 812 lands in partition 3 at offset 1041.

    Encoding
    Avro
    Schema v1
    coupon, no default

    What changed

    • 812 v1 at offset 1041
  2. Avro writes no tags: to read 812, Billing fetches the writer’s schema, v1, from the registry and matches fields by name. JSON instead repeats field names in every record and checks nothing without a validator; Protobuf writes numeric tags, and a retired tag must never be reused.

    JSON
    names in every record
    Protobuf
    numeric tags
    Avro
    the writer’s schema

    What changed

    • Billing and Search read 812
    • Billing fetches schema v1
    • Fields matched by name
  3. Schema v2, without coupon, is added to the registry. Billing upgrades first while Checkout still writes v1: during a rollout old and new versions run side by side. Billing’s v2 reader skips coupon in order 817 at 1042, so the change is backward compatible: new readers read old data.

    Change
    v2 drops coupon
    Backward
    new reads old: yes

    What changed

    • Schema v2, no coupon
    • Billing reads with v2
    • 817 v1 at 1042, coupon skipped
  4. Checkout ships v2 before Search upgrades, and order 819 lands at 1043 without coupon. Billing reads it. Search’s v1 reader finds no value for coupon and fails: the change is not forward compatible, since old readers can’t read new data.

    Forward
    old reads new: no
    Search
    stuck at 1043

    What changed

    • Checkout writes v2
    • 819 v2 at 1043, no coupon
    • Search fails on 819
  5. Rolling Checkout back to v1 doesn’t help. Order 822 is written with v1 at 1044, but 819 stays in the log until retention expires, and Search fails on it again at every retry, falling further behind while Billing reads on.

    819 stays
    until retention expires

    What changed

    • Checkout back to v1
    • 822 v1 at 1044
    • 819 still there: Search fails again
  6. The safe order is consumers first, then producers. Search upgrades to v2, reads 819, skips coupon in 822 and catches up. Until every consumer reads v2, no producer should write it.

    Order
    consumers, then producers

    What changed

    • Search reads with v2
    • 819 read, 822’s coupon skipped
    • Search caught up
  7. With both readers on v2, Checkout ships v2 again: order 824 lands at 1045 and both read it. Coupon leaves the log only as retention expires the v1 records. A change that is only forward compatible reverses the order: producers first.

    Backward only
    consumers first
    Forward only
    producers first

    What changed

    • Checkout writes v2
    • 824 v2 at 1045
    • Billing and Search read 824

Space plays · ← → step · Home resets

THE IDEA

How it works

During a rolling deployment, old and new versions run side by side, and a log holds old records long after producers change. So a format change has to work across versions. Backward compatibility means new readers can read old data. Forward compatibility means old readers can read new data.

Encodings differ in how they support this. JSON repeats field names in every record, which is readable but larger, and nothing enforces its shape unless you add a validator, so a changed type surfaces at runtime in some consumer. Protobuf writes numeric field tags instead of names, and parsers skip tags they do not recognize, so adding a field is safe; reusing a retired tag makes old data decode as the wrong field. Avro writes no tags: a reader needs the writer’s schema, commonly fetched from a schema registry, and matches fields by name. A new field needs a default so new readers can read old records.

Rollout order follows from compatibility. Take an Avro record whose coupon field has no default, and remove it. A new reader skips coupon in old records, so the change is backward compatible. An old reader meeting a new record has no value for coupon and fails, so it is not forward compatible. Therefore upgrade consumers first, then producers. Ship the producer first and old consumers fail on the first new record. Rolling it back does not help, because those records stay in the log until retention expires. A change that is only forward compatible reverses the order.

DECISION POINT

A Protobuf event has a price field holding dollars as a float. Five teams read it, and the topic keeps 30 days of records. You want integer cents, and a teammate proposes changing the field’s type and units in place, keeping its tag. What do you do instead?

Write a sentence or two to unlock the guidance. Committing first is what makes it stick.

Keep in mind

  • Backward compatible means new readers handle old data; forward compatible means old readers handle new data.
  • A log keeps old records, so rolling back a producer does not undo what it wrote.
  • Rollout order follows from which compatibility direction a change has.
CHECK YOUR UNDERSTANDING

Put it into practice.

1 OF 3
RecallQUESTION 01

What does backward compatibility promise?

Choose an answer. You’ll see the explanation once you get it right.

0 of 3 checkpoints answered correctly to complete this lesson.