Part 5 of 11
Avro
Apache Avro began in 2009 as part of Hadoop, because protobuf did not suit Hadoop's needs.
- It is schema-based, with two schema languages: Avro IDL for people to write, and a JSON form that machines read easily.
- Like protobuf, it has fields and types only, with no complex validation.
- Its schema has no tag numbers.
Our example record is 20 bytes in Avro, the smallest of all. The bytes contain nothing that names a field or a type. Values are simply placed one after another: a string is a length and then UTF-8 bytes, and a whole number is a variable-length number. To decode, the reader walks through the fields in the order the schema lists them, and uses the schema to know each type. So the reader must use exactly the schema the data was written with. Any mismatch, and the bytes are read as the wrong values.
Two schemas: writer and reader
- Writer's schema: the schema version the encoding app uses, for example built into that app.
- Reader's schema: the schema the decoding app expects. It may be a different version.
Avro decodes using both. It compares them and translates each record from the writer's shape into the reader's shape. This is called schema resolution:
- Fields are matched by name, so their order can differ.
- A field the writer has but the reader does not is ignored.
- A field the reader expects but the writer did not have is filled with the default value in the reader's schema.
Matching two schemas
Step 1 of 4
Data written by an old version of an app, read by a new one. The writer's schema has city, zip and tags; the reader's has zip, city and country.
Compare protobuf: there, the encoder and decoder can each use their own schema version directly. Avro always needs the exact writer's schema, plus the reader's schema.
Schema evolution rules
In Avro terms:
- Forward compatibility: the writer's schema is newer than the reader's.
- Backward compatibility: the writer's schema is older than the reader's.
The rules:
- To stay compatible, only add or remove fields that have a default value.
- Adding a field without a default breaks backward compatibility: a new reader has nothing to fill in for old data.
- Removing a field without a default breaks forward compatibility: an old reader has nothing to fill in for new data.
- Null is not a free default for every field. To allow null, declare a union type, such as "null or number or string". Null can be the default only if it is the first option in the union. Being explicit like this helps prevent bugs.
- Changing a field's type is allowed if Avro can convert between the two types.
- Renaming a field works by listing the old name as an alias in the reader's schema. So new readers can handle a rename, but old readers cannot (backward only).
- Adding a new option to a union is likewise backward only: old readers do not know the new option.
Finding the writer's schema
The reader must find the writer's schema somehow. Sending the whole schema with every record would often be bigger than the record itself. The answer depends on the setting:
| Setting | How the reader finds the writer's schema |
|---|---|
| A large file of many records, all with one schema | Write the schema once at the start of the file (Avro object container files) |
| A database whose records were written at different times | Put a schema version number at the start of each record, and keep a table of all schema versions to look it up |
| A network connection | Both sides agree on a schema version when the connection opens, and use it for the life of the connection |
The version can be a counting number or a hash of the schema. A database of schema versions is worth having anyway. It serves as documentation, and it is a place to check that a new schema is compatible with the old ones.
Dynamically generated schemas
Because Avro has no tag numbers, it suits schemas that programs generate.
- Say you export a relational database to Avro files every night.
- You generate one record schema per table, with one field per column, using the column names as field names.
- If someone adds or drops a column, you just generate a new schema on the next export.
- Readers match fields by name, so their older reader schemas still work.
With protobuf, someone would have to assign tag numbers by hand, and update them carefully each time without ever reusing an old one. Generated schemas were not a design goal for protobuf, but they were for Avro.
Try it
How big is this record?
Change it and watch the bytes.
- JSON51 bytes
- Protobuf22 bytes
- Avro20 bytes
Show the bytes
cityziptags
Real encodings of this one record. The protobuf schema numbers the fields 1 to 3; Avro writes them in schema order with no numbers at all. JSON spells out every field name each time.