Part 6 of 11
The merits of schemas
The schema languages of protobuf and Avro are far simpler than JSON Schema and XML Schema. They have no regular expressions or range checks, so they are easy to implement, and many languages support them. These ideas are not new; schema-based binary formats for network protocols go back decades.
Binary formats with schemas bring real benefits:
- Compact: much smaller than binary JSON, because field names are left out.
- Living documentation: decoding needs the schema, so the schema can never fall out of date.
- Safety checks: a database of schema versions lets you check forward and backward compatibility before you deploy.
- Type checks: generated code lets typed languages catch mistakes when compiling.
So schema evolution gives you the same flexibility as schemaless databases, but with better guarantees and better tools. Still, use as few different formats as you can, to keep operations simple.
Modes of dataflow
Whenever you send data to a process that does not share your memory, over a network or through a file, you must encode it. Compatibility is always a relationship between two processes: the one that encodes and the one that decodes. Getting it right is what lets you upgrade parts of a system one at a time instead of all at once.
The main ways data flows between processes:
- Through databases.
- Through service calls (REST and RPC).
- Through workflow engines.
- Through asynchronous messages.
Dataflow through databases
Whoever writes a value encodes it; whoever later reads it decodes it.
- If one process writes data and reads it back later, it is like sending a message to its future self. That needs backward compatibility, so newer code can read what it wrote earlier.
- Usually many processes share one database: several apps, or many copies of one service. During a rolling upgrade, some copies run new code and some old.
- So a value written by new code may be read by old code. That needs forward compatibility as well, and old code must keep unknown fields when it writes records back.
Old and new rows side by side
A database can hold a row written a second ago right next to one written five years ago. You can replace your app's code in a few minutes, but old data stays in its original encoding unless something rewrites it. In short: data outlives code.
Rewriting all data into a new schema is expensive on large datasets, so databases avoid it or do it lazily:
- Log-structured (LSM) storage engines rewrite data into the newest format as part of compaction.
- Relational databases let you make simple changes, like adding a column that allows null, without touching existing rows. When an old row is read, the missing column is filled in as null.
With schema evolution, the whole database looks as if it uses one schema, even though storage holds many past versions. Bigger changes still need a rewrite, often done by the app itself. Examples: turning a single value into a list, or moving data into another table. Keeping such changes compatible is still an open research problem.
Archival storage
You may take a snapshot of a database for a backup or to load into a data warehouse. The copy is usually written with the latest schema, even if the source mixes versions, since you are copying everything anyway. A dump is written once and never changed, so Avro object container files fit it well. It is also a good moment to convert to a column-oriented format like Parquet for analysis.