Avro
Schema Versioning
Data Serialization
Apache Avro
Big Data Management

Avro schema versioning and schema evolution

Codemia Knowledge Hub ·

Avro schema versioning works through schema evolution: you change a schema in compatible ways (such as adding a field with a default), Avro's schema resolution reconciles the writer's and reader's schemas, and a registry such as Confluent Schema Registry keeps track of every version. Apache Avro is a data serialization system, widely used with Apache Hadoop, that uses a compact binary format and defines schemas in JSON.

Avro schema basics

Avro data is always serialized with its schema, which describes the structure of the binary data. Schemas are built from:

  • Primitive types: null, boolean, int, long, float, double, bytes, string
  • Complex types: records, enums, arrays, maps, unions and fixed

A simple schema for a user:

json
1{
2  "type": "record",
3  "name": "User",
4  "fields": [
5    {"name": "name", "type": "string"},
6    {"name": "age", "type": "int"},
7    {"name": "email", "type": "string"}
8  ]
9}

Compatibility types

Schema evolution means modifying a schema without breaking the applications that use it. Each message is paired with a schema so the reader can interpret it. Avro compatibility comes in four kinds:

  • Backward: the new schema can read data written with previous schemas.
  • Forward: data written with the new schema can be read by applications using previous schemas.
  • Full: both backward and forward.
  • None: no compatibility is guaranteed.

Compatibility is what keeps applications working as they read older or newer data.

Rules for changing a schema

  1. Evolve conservatively. Add optional fields with defaults, or new items to arrays and maps. Do not remove fields or change types in ways that break reading.
  2. Rely on Avro's schema resolution, which handles missing, extra or modified fields.
  3. Track every version. A registry such as Confluent Schema Registry stores and serves the different versions.

Example: adding an optional field

Original schema:

json
1{
2  "type": "record",
3  "name": "User",
4  "fields": [
5    {"name": "name", "type": "string"},
6    {"name": "age", "type": "int"}
7  ]
8}

New schema with an optional phone field:

json
1{
2  "type": "record",
3  "name": "User",
4  "fields": [
5    {"name": "name", "type": "string"},
6    {"name": "age", "type": "int"},
7    {"name": "phone", "type": ["null", "string"], "default": null}
8  ]
9}

This change is backward compatible: when the new schema reads old data that has no phone field, Avro fills in the default null. Readers still on the old schema simply ignore the extra field in new data.

Versioning practices

PracticeDescriptionBenefit
Consistent NamingMaintain consistent naming conventions across all versions.Prevents confusion and errors during data processing.
Semantic VersioningUse semantic versioning for schemas.Helps in tracking compatibility and understanding the impact of changes.
Schema RegistryUse a schema registry for storing and retrieving schemas.Centralizes schema management and enhances compatibility checks.

Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design