LOAD-BEARINGfree
FoundationsTier 1 · unit 6 of 6 · 13 min read

Schema evolution: changing data safely

The previous lesson kept your API compatible. This one keeps your data compatible, and it is the harder problem — because an API call lives for a second, while a row you write today may be read in five years by code nobody has written yet.

Old and new always run at the same time

Two things guarantee it. First, a rolling deploy: you never replace every server at once, so for some window old and new code are both live, reading and writing the same store. Second, data outlives code — the rows, events and messages written by last year's version are still sitting there, and they are not going to rewrite themselves.

So you need compatibility in both directions, and they are not the same property:

  • Backward compatible — new code can read old data. This is the one everyone remembers, and it's usually the easy one, because you know exactly what the old shape was.
  • Forward compatible — old code can read new data. This is the one everyone forgets, and it's genuinely harder, because it asks code written in the past to tolerate a field it has never heard of.

What an encoding does with a field it doesn't recognise

That single question is most of the difference between formats.

JSON identifies fields by name. Most parsers quietly ignore unknown keys, which hands you forward compatibility on read — with a sting in the tail we'll get to.

Tag-based binary formats identify fields by number. The name is a label for humans; only the tag goes on the wire. Two consequences fall straight out: renaming a field is free, because the tag never changed — and reusing a retired tag number is catastrophic, because old readers will decode the new field as the old one, silently, with the wrong type.

Schema-registry formats carry no tags at all. The reader fetches the exact schema the writer used and resolves it against its own by field name, so both sides can evolve independently as long as a resolution exists. Tightest bytes on the wire, most infrastructure to run.

Four rules that keep you out of trouble

  • Add fields as optional, with a default. A new required field is incompatible the moment you ship it: old writers don't produce it, so new readers reject data that is perfectly good.
  • Never change a field's type or its meaning. Add a new field and migrate. "It's only int32 to int64" truncates silently on every reader that hasn't been updated, and reusing status to mean something new is the same bug with better camouflage.
  • Never reuse a tag number or an enum value. Retire it and reserve it, so that no well-meaning colleague can claim it next year.
  • Treat removal as two steps. Stop writing the field; wait until nothing reads it; only then delete it.

The order of the rollout is not optional

Adding a field is two deploys, in one specific order:

1. deploy readers that understand the new field
   (and still cope when it is absent)
2. wait for that rollout to finish everywhere
3. deploy writers that start producing it

Reverse those and every reader still on the old build meets data it cannot handle. Removing a field is the same dance backwards: stop writing, drain, then stop reading. The rule worth memorising is readers first when adding, writers first when removing — whichever version can cope with both worlds goes first.

The trap

The read-modify-write hole. Old code reads a record, ignores the new field it doesn't understand, changes one unrelated thing, and writes the whole object back — dropping the field it ignored. Your data is destroyed by a service that is working exactly as designed and reports no error. Either preserve unknown fields across a round trip, or have older versions patch single fields instead of rewriting whole records.

The test for all of this is cheap and almost nobody runs it: take a payload written by the build currently in production, read it with the build you are about to ship, then do it the other way round. Two assertions in CI catch nearly every incident in this lesson.