$ avro-tools getschema orders.avro

Avro

Apache Avro is a binary serialization format whose schema is written in JSON. A record is encoded as just its values, in schema order, with no field names or tags, so Avro data is very compact. The catch: you can only decode the bytes if you know the exact schema they were written with. Avro’s key feature is what it does with that: it resolves the writer’s schema against the reader’s schema, so old and new versions of a producer and consumer can keep talking.

Avro is a common format for Kafka events and CDC streams (Debezium, Confluent connectors), and it is also used as a row-oriented file format (.avro container files) for data exchange.

The schema

{
  "type": "record",
  "name": "Order",
  "namespace": "shop",
  "fields": [
    {"name": "id",     "type": "long"},
    {"name": "name",   "type": "string"},
    {"name": "coupon", "type": ["null", "string"], "default": null}
  ]
}

Primitive types are null, boolean, int, long, float, double, bytes and string. Complex types are record, enum, array, map, union and fixed. Logical types add meaning on top, for example decimal, date, timestamp-millis, timestamp-micros and uuid. An optional field is a union with null; list null first, because a union’s default must match its first branch.

Old reader, new writer

The producer team ships schema v2: it adds email and drops city. The consumer still runs code built for v1. Here is what happens to one record.

$ consumer --schema v1 < producer --schema v2
Avro schema resolution: writer v2, reader v1
WRITER · v2 id: longname: stringemail: string new READER · v1 id: longname: stringcity: string default "unknown" encode 42 "alice" "[email protected]" BYTES · values only, no names, no tags SCHEMA REGISTRY schema id 7 → writer v2 matched by field name skipped (not in v1) city = "unknown" consumer sees { id: 42, name: "alice", city: "unknown" }
  1. The producer writes with schema v2. The consumer reads with schema v1. Neither knows the other was upgraded.
  2. The writer encodes only the values, in v2 field order. On their own, these bytes can't be decoded.
  3. The reader looks up the writer's schema: from a schema registry (the message carries a schema ID) or from the header of an Avro file.
  4. Avro walks both schemas and matches fields by name, not by position. id and name decode as usual.
  5. email exists only in the writer schema, so the reader decodes it and throws it away.
  6. city is missing from the data, so the reader fills in its default. Without a default, this read would fail.

Compatibility rules

A schema registry (Confluent Schema Registry, Apicurio, AWS Glue Schema Registry) stores every version of a schema and rejects changes that break the compatibility mode you set for the topic.

Mode Guarantee Safe changes
BACKWARD (Confluent default) New consumers can read old data Delete a field; add a field with a default
FORWARD Old consumers can read new data Add a field; delete a field that had a default
FULL Both directions Add or delete fields that have defaults
*_TRANSITIVE Same, against every past version, not just the last Same rules, stricter check

The practical rule: give every field a default, never rename a field (use aliases if you must), and never change a field’s type except for Avro’s allowed promotions (int → long, float or double; long → float or double; float → double; string ↔ bytes).

Avro vs Protobuf vs JSON

Avro Protobuf JSON
Schema Required, JSON Required, .proto Optional
Field identity on the wire None (schema order) Field number Field name
Needs writer schema to read Yes No No
Code generation Optional (generic records work) Usual No
Typical home Kafka, CDC, data files gRPC, service APIs REST APIs, logs

Common mistakes

  • Adding a required field (no default) to a topic in BACKWARD mode. The registry will reject it; if you bypass the registry, consumers break.
  • Sending raw Avro without a schema ID. Without the writer schema, the bytes are unreadable.
  • Using Avro files for analytics. Avro is row-oriented. For scans, convert to Parquet.

Learning Resources