Skip to main content
XenonStack

Enterprise Data Management

XenonStack Insights Protocol Buffers (Protobuf): Schemas and Serialisation

| Published | Updated

Protocol Buffers (Protobuf): Schemas and Serialisation
7:33
Protocol Buffers: a .proto schema, compact binary messages, generated code and safe schema evolution

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.

QuestionProtobufJSON
How is the structure defined?In a .proto schema that is compiled into codeUsually by convention or a separate schema language
What goes on the wire?Compact binaryText
Can a person read a message directly?No: messages are not self-describing, so you need the .proto fileYes, with any text editor
Where does it fit best?Service-to-service calls and storage between your own systemsPublic APIs and data a browser reads directly
1. Define: Write the message definitions in a .proto file.
2. Compile: Run the protocol buffer compiler at build time to generate classes for each language you use.
3. Serialise: Use the generated classes to set fields and serialise a message to bytes.
4. Parse: Send or store the bytes, then parse them back into an object on the receiving side.
Diagram: how Protobuf moves a message from schema to receiver (flow view)

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.

Two-column diagram. Sender: write proto, run protoc, set fields, serialise. Receiver: receive bytes, parse, unknown fields, use the data.
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?

  1. Write the message definitions in a .proto file.
  2. Run the protocol buffer compiler at build time to generate classes for each language you use.
  3. Use the generated classes in your code to set fields and serialise a message to bytes.
  4. 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.

ChangeSafe on the wire?Note
Add a fieldYesOld readers keep unknown fields
Remove a fieldYesReserve its number and name so no one reuses them
Add an enum valueYesKeep zero as the first value
Change a field numberNoTreat it as a new field
Reuse a deleted numberNoField numbers should never be reused
Move a field into an existing oneofNoCreate 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?

  1. Pick one internal service boundary where payload size or parsing cost matters.
  2. Write the .proto file and generate code for the languages on both sides.
  3. Agree the rules for adding, removing and reserving fields.
  4. Keep JSON at any boundary a browser consumes directly.
  5. 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.

Table of Contents

Get the latest articles in your inbox

Subscribe Now

Interested in solving your challenges with the XenonStack team?
Get Started

Tell us about you and your priorities so we can prepare the right conversation. It takes about 2 minutes.

Please Select your Industry
Banking
Fintech
Payment Providers
Wealth Management
Discrete Manufacturing
Semiconductor
Machinery Manufacturing / Automation
Appliances / Electrical / Electronics
Elevator Manufacturing
Defense & Space Manufacturing
Computers & Electronics / Industrial Machinery
Motor Vehicle Manufacturing
Food and Beverages
Distillery & Wines
Beverages
Shipping
Logistics
Mobility (EV / Public Transport)
Energy & Utilities
Hospitality
Digital Gaming Platforms
SportsTech with AI
Public Safety - Explosives
Public Safety - Firefighting
Public Safety - Surveillance
Public Safety - Others
Media Platforms
City Operations
Airlines & Aviation
Defense Warfare & Drones
Robotics Engineering
Drones Manufacturing
AI Labs for Colleges
AI MSP / Quantum / AGI Institutes
Retail Apparel and Fashion

Continue