Notes

Part 1 of 11

Encoding and evolution

Apps keep changing. A new feature usually means new data too: an extra field, a new kind of record, or a new way to look at data you already have. This chapter is about how data is turned into bytes, and how to change that shape over time without breaking the programs that read it. It ties back to evolvability from the earlier chapter on requirements: a good system is one that is easy to change. The key idea: old and new versions of your code, and of your data, will always overlap for a while. Plan for that, and you can ship small changes often and safely.

Why old and new must coexist

How data shape changes depends on the database:

  • A relational database has one schema in force at a time. You change it with a migration, such as adding a column.
  • A schema-on-read ("schemaless") store does not force one shape. Records in old and new shapes can sit side by side.

Code also cannot change everywhere at the same moment:

  • Servers: a rolling upgrade (also called a staged rollout) puts the new version on a few machines, watches them for problems, then moves on to the rest. There is no downtime, so teams can release more often.
  • User devices: people install app updates late, or never.

So for a while, old and new code run together, and old and new data formats sit together.

Backward and forward compatibility

TermMeaningDifficulty
Backward compatibilityNew code can read data written by old codeUsually easy: the new code knows the old format and can keep the old reading logic
Forward compatibilityOld code can read data written by new codeHarder: the old code must ignore things that were added after it was written

For network APIs, which one you need depends on who upgraded:

Who upgradedRequests needResponses need
Service is new, client is oldBackwardForward
Client is new, service is oldForwardBackward

Keeping unknown fields

Guess first

Old code loads a user record that has a field it doesn't know, changes another field, and saves the record. What should happen to the unknown field?

Old code loads a user record that has a field it doesn't know, changes another field, and saves the record. What should happen to the unknown field?

Forward compatibility is more than "do not crash on new fields". Say a new version of a quiz app adds a nickname field to the user record. An old server loads that record, adds one to the streak, and saves it back. If the old code decoded the record into a class that throws away fields it does not know, the nickname is silently erased. Old code should carry unknown fields through untouched.

Old code, new data

Step 1 of 4

The new version of the app saves a user with a new field, nickname, next to name and streak.