Encoding Formats: JSON, Protobuf, Avro, Thrift
How programs turn in-memory data into bytes, and how text formats like JSON compare with schema-based binary formats like Protocol Buffers, Thrift and Avro.
Overview
Inside a program, data lives in objects, lists and hash maps that point at each other in memory. To write that data to a file, send it over the network or put it on a queue, the program has to turn it into a flat sequence of bytes. This is encoding (also called serialization). The reverse, bytes back into objects, is decoding. Every service call, every message and every stored record goes through this step, so the format you choose affects size, speed, safety and, most of all, how easily the data can change later.
Two families of formats
Every exchange has the same shape. The writer process encodes objects in memory into bytes, those bytes travel through a file, the network or a queue, and the reader process decodes them back into objects in memory. The writer and the reader must agree on the format, and in the binary formats on a schema too (a .proto, .thrift or .avsc file). Everything interesting happens at the encode and decode steps.
The formats fall into two families. Text formats such as JSON, XML and CSV are human-readable. They write the field names in every record, their types are loose (JSON has one number type), and a schema is optional and separate (JSON Schema). They are the usual choice for public APIs, browsers and config.
Schema-based binary formats such as Protocol Buffers, Thrift and Avro are not human-readable; you need a tool to read them. They put no field names on the wire, only numbers (Protobuf, Thrift) or nothing at all (Avro). Types are declared in the schema, and the schema is required and drives code generation. They are the usual choice for internal RPC, event streams and data files.
The MessagePack format sits in between. It is a binary form of JSON: more compact types, but it still writes every field name in every record, and it has no schema.
Key concepts
- Encoding (serialization)
- Field number (tag)
- Wire type
- Varint
- Writer's and reader's schema
- Schema registry
- MessagePack
Interview questions
- Why would you choose Protobuf over JSON for internal service calls?
- How does Avro decode a record when the bytes contain no field names or tags?
- How can an old Protobuf reader handle a message with a field it has never seen?
- What are the real problems with JSON as a data format?
- What does a schema registry add to a Kafka pipeline?
Sign up free to read the full topic
More DDIA topics
- Change Data Capture (CDC)
- Clocks and Time in Distributed Systems
- Column-Oriented Storage and Compression
- Consensus Algorithms: Paxos and Raft
- Consistency Guarantees: Eventual, Causal and Linearizable
- Declarative vs Imperative Query Languages
- End-to-End Correctness and the Exactly-Once Myth
- Event Streams and Message Brokers
- Fan-Out and the Twitter Timeline Problem
- Fault Tolerance in Stream Processing
- Faults and Partial Failures
- Graph Data Models
Start free on Cariara · Coding problems · System design studio