Schema Evolution and Backward/Forward Compatibility

How to change the shape of your data while old and new code run side by side: backward and forward compatibility, the rules for Protobuf, Avro and SQL tables, and the order to roll changes out.

Overview

Applications change, and so does the shape of their data: a new field, a field no one needs, a wider number type. In a real system you cannot change every program and every stored record at the same moment. Servers are upgraded a few at a time, mobile apps stay old for months, and data written years ago is still on disk. Schema evolution is the practice of changing a data format so that every version still running can keep working.

Why old and new code meet

During a rolling upgrade, some servers run the new version while others still run the old one. Both read and write the same database and the same message topics. So new code reads data written by old code, and old code reads data written by new code.

The data also outlives the code. A row written five years ago is still read by today's code unless someone rewrote it. So compatibility is not only about deploys; it is about everything ever stored.

Key concepts

  • Backward compatibility
  • Forward compatibility
  • Full compatibility
  • Reserved field
  • Default value
  • Alias
  • Compatibility mode
  • Expand and contract

Interview questions

  • You need to rename a Protobuf field. How do you do it safely?
  • Producers and consumers of a Kafka topic are deployed by different teams. How do you keep schema changes from breaking them?
  • What goes wrong when old code reads and rewrites a record written by new code?
  • How do you add a NOT NULL column to a large PostgreSQL table without downtime?
  • Why is forward compatibility harder than backward compatibility?

Sign up free to read the full topic

More DDIA topics

Start free on Cariara · Coding problems · System design studio