Key takeaway
- Protocol Buffers serialise structured data into compact binary using a .proto schema. See how Protobuf works, how to evolve schemas, and when to avoid it.
Protocol Buffers (Protobuf) is a language-neutral, platform-neutral, extensible mechanism for serialising structured data: you describe your messages once in a .proto file, and a compiler generates code that writes and reads them as compact binary. Teams use it for service-to-service communication where JSON would be larger and slower to parse.
This guide is for engineers and architects who run microservices or streaming pipelines, including those built on Apache Pulsar and other streaming and real-time analytics systems. It covers how a schema is defined, how Protobuf works, how to change a schema safely, and when another format is the better choice.
What is Protobuf, and how does it differ from JSON?
The Protocol Buffers overview describes Protobuf as "like JSON, except it's smaller and faster, and it generates native language bindings". It combines a definition language, compiler-generated code, language runtime libraries, a serialisation format and the serialised data itself. The same page calls it the most commonly used data format at Google, used for inter-server communication and archival storage, and names gRPC and Envoy Proxy among the projects that use it.
| Question | Protobuf | JSON |
|---|---|---|
| How is the structure defined? | In a .proto schema that is compiled into code | Usually by convention or a separate schema language |
| What goes on the wire? | Compact binary | Text |
| Can a person read a message directly? | No: messages are not self-describing, so you need the .proto file | Yes, with any text editor |
| Where does it fit best? | Service-to-service calls and storage between your own systems | Public APIs and data a browser reads directly |
How do you define a message in a .proto file?
A .proto file lists messages, and each message lists typed fields. Each field has a name, a type and a unique field number. The proto3 language guide gives the valid number range as 1 to 536,870,911, with 19,000 to 19,999 reserved for the implementation. Numbers 1 to 15 encode in one byte and 16 to 2047 take two, so keep the low numbers for the fields you set most often. The field name helps readers and generated code, while the number identifies the field in the binary format.
syntax = "proto3";
package tutorial;
message Person {
string name = 1;
int32 id = 2;
optional string email = 3;
repeated PhoneNumber phones = 4;
}
Scalar types include int32, int64, float, double, bool, string and bytes, and fields can be nested messages. The guide lists the labels optional, repeated and map, so the required label that appears in older examples should not be copied into proto3 files. Enums must have a first value of zero.
How does Protobuf work?
- Write the message definitions in a .proto file.
- Run the protocol buffer compiler at build time to generate classes for each language you use.
- Use the generated classes in your code to set fields and serialise a message to bytes.
- Send or store the bytes, then parse them back into an object on the receiving side, in the same language or another one.
According to the Protocol Buffers overview, protoc has built-in support for C++, C#, Java, Kotlin, Objective-C, PHP, Python and Ruby, Google supports Dart and Go through plugins, and other languages come from third-party projects.
How do you change a Protobuf schema safely?
Backward and forward compatibility is a main reason to choose Protobuf. Old code ignores fields it does not know, and new code reads old messages with defaults for fields that are missing, provided you follow a few rules from the guide.
| Change | Safe on the wire? | Note |
|---|---|---|
| Add a field | Yes | Old readers keep unknown fields |
| Remove a field | Yes | Reserve its number and name so no one reuses them |
| Add an enum value | Yes | Keep zero as the first value |
| Change a field number | No | Treat it as a new field |
| Reuse a deleted number | No | Field numbers should never be reused |
| Move a field into an existing oneof | No | Create a new message version instead |
Use a reserved statement, for example reserved 2, 15, 9 to 11, to block deleted numbers. Changing a field type is only conditionally safe: int32, uint32, int64, uint64 and bool are compatible with each other, but values can be truncated when parsed as a narrower type.
When should you not use Protobuf?
The Protocol Buffers overview lists the cases where it is a poor fit.
- Messages are assumed to fit in memory, so data beyond a few megabytes calls for another solution.
- Messages are not self-describing: you cannot interpret one without its .proto file.
- Messages are not compressed by default, and the same data can have many binary encodings, so you cannot compare two messages for equality without parsing them.
- It is not a formal standard from any standards organisation.
Protobuf also adds a compile step and generated code to your build. For a small team with one language and a public JSON API, that cost may not be worth paying.
How do AI agents change the way you use Protobuf?
When AI agents call internal services, the message schema becomes the contract the agent relies on. A typed schema with disciplined field numbering means a service change does not silently alter what an agent receives. Version the .proto files, review changes like code, and record which schema version each decision used. The ElixirData page describes XenonStack's Context OS for governing the data that agents act on.
What should you do first?
- Pick one internal service boundary where payload size or parsing cost matters.
- Write the .proto file and generate code for the languages on both sides.
- Agree the rules for adding, removing and reserving fields.
- Keep JSON at any boundary a browser consumes directly.
- Check message sizes against the memory limits of your services.
To map one decision your agents make, and the data and policy it depends on, book a Context Workshop. See what happens in the workshop.
Frequently asked questions
What is Protobuf used for?
It serialises structured data into a compact binary format, mainly for communication between services and for storage, with generated code for many languages.
Is Protobuf faster than JSON?
The Protocol Buffers overview describes it as smaller and faster than JSON. Measure your own payloads, because results depend on the data and the language.
Can I change a .proto file after deployment?
Yes. Adding and removing fields is wire-safe, but never reuse a field number, and reserve the numbers and names of deleted fields.
Does proto3 have required fields?
The proto3 language guide lists optional, repeated and map labels and does not list a required label.
When should I avoid Protobuf?
Avoid it for very large messages, for data people must read without the schema, and where a simple JSON API is enough.
Ready to put AI agents to work? See an ElixirClaw demo: the agentic OS to build, orchestrate and govern AI agents across your enterprise systems.
Explore the XenonStack platform family: ElixirData · ElixirClaw · ElixirHub · NexaStack · MetaSecure · Neural AI · AgentGRC · AgentSRE · AgentAnalyst · AgentInstruct · AgentRAI · AgentScrum · AgentSearch · AgentForce · AI Aviator · SAIF Aviator · CohortIQ · XenonStack AI · XenonStack UAE