Topic 034 / Advanced
Serialization and schema evolution: JSON, Protocol Buffers, Avro, schema registries, compatibility rules
Chapter 3.4 · API Styles and Serialization
What and why
Every message crossing a process boundary — an API response (→ 31–33) or, later, an event (→ 113) — must be turned into bytes on one side and back into a value on the other, and the two sides are rarely upgraded at the same instant. Serialization is the encoding itself: JSON (self-describing text, human-readable, verbose) versus Protocol Buffers and Avro (compact, schema-driven binary formats). Schema evolution is the discipline of changing that schema over time without breaking a reader or writer that hasn't updated yet, enforced by a schema registry that checks a new schema against declared compatibility rules before accepting it. Builds on: → 33.
Diagram
| Format | Schema required | Size | Human-readable |
|---|---|---|---|
| JSON | optional, documented | larger | yes |
| Protocol Buffers | required, compiled | compact | no |
| Avro | required, carried with data | compact | no |
How it works
- Producer and consumer agree a schema — explicit and enforced for Protobuf/Avro, documented but not enforced for JSON.
- A proposed schema change is classified as backward-compatible (new readers can read old data), forward-compatible (old readers can read new data), or full (both).
- A schema registry rejects a new schema version that violates the service's declared compatibility mode before it's published, catching a breaking change before it ships.
- Typically safe under backward compatibility: adding a new optional field with a default. Typically unsafe: removing or repurposing a field a live reader still depends on, or changing a field's type.
Practical example
(illustrative). A producer adds a new optional field to an event; consumers still running the previous schema simply ignore the field they don't recognize — a backward-compatible change. If that same producer instead removed a field a live consumer still reads, every un-upgraded consumer would fail to decode the very next message.
When to use it
- Use when: JSON is right for public APIs and human-debuggable payloads where verbosity is an acceptable cost; Protobuf or Avro with a registry are right for high-volume internal traffic or event streams where payload size, parse speed, or contract strictness matter.
- Avoid when: introducing a schema registry for a single small service with one producer and one consumer that always deploy together — the coordination overhead isn't earning its keep yet.
- Failure scenarios: a producer ships an incompatible change with no registry enforcing a compatibility mode, and every consumer still on the old schema starts failing to decode messages at once; a field silently reused for a different meaning corrupts data instead of raising a visible error.
- Simpler alternative: JSON with a documented, carefully reviewed, versioned contract, for lower-volume systems that don't yet justify a registry.
Practice
(vocabulary kind — classification drill; 20 min, no lab services). Classify each of these 5 proposed schema changes as backward-compatible, forward-compatible, both, or breaking, and state what happens to an un-upgraded reader or writer in each case: (1) add an optional field with a default; (2) remove a field a live consumer still reads; (3) rename a field; (4) widen an integer field to a larger integer type; (5) change a field's type from string to number. Done when each change is classified safe or breaking, with the consumer failure it would cause.
Key takeaway
A schema registry's job is to stop an incompatible change from shipping, not to document a compatible one after the fact. Done when you can: classify a proposed schema change by compatibility mode and predict exactly which caller breaks if it's wrong. Used next in → 36 (AsyncAPI documents these contracts for event APIs), → 113 (the foundation for safe event contracts); applied in Project 19 (checklist: schema compatibility, topic 34).
References
None — this topic makes no external claims; the serialization formats and
registry mechanics are kept at the conceptual level, with no dedicated reference entry for
topic 34 in references.md.