Video in progress

How do you choose a serialization format for data exchanged between services and stored on disk for a long time: JSON, Protobuf, Avro or Java serialization?

I rule out Java native serialization immediately: it is unsafe, slow and tied to Java. Among the rest, the choice depends on schema evolution, readability, size and the ecosystem: JSON for external APIs, Protobuf for internal RPC, and for Kafka and a data lake often Avro + a schema registry.

📖 Answer

Choosing a format is really not about code but about time: bytes written today have to be read in 3 years by a service of a different version.

The main criteria:

  • Schema evolution. Does adding, removing or renaming a field break old readers? In Protobuf the field number is what matters: you can rename freely, and you never reuse a number (reserved). In Avro the writer's and reader's schemas are compared, and compatibility works through default values. JSON has no schema: only discipline and tests hold compatibility.
  • Size and speed. Binary formats are several times more compact and faster to parse than JSON. At hundreds of thousands of messages per second it shows up in CPU and network bills. In a plain CRUD API it hardly matters.
  • Debuggability. You see JSON with curl and in the log. Protobuf needs tools. During an incident this matters.
  • Ecosystem. Browsers and third-party clients: JSON. gRPC: Protobuf. Kafka + Confluent schema registry: Avro or Protobuf, and the registry checks the compatibility rules (BACKWARD, FORWARD) before deploy.

Why not Java serialization: deserialization is a security hole, the format is tied to class names and internal structure (refactoring breaks compatibility), and other languages can't read it.

An extra point for long-term storage on disk: every record or file should carry a format version, and the code that reads old versions should be tested with real old samples. I usually put "golden" files in the repo, and CI checks that every new version can still read them.

At the organization level the biggest gain comes not from the format but from the rules: schemas in one repo, changed through review, with breaking changes stopped in CI.