Why Protocol Buffers?
When you need to serialize structured data, you have several options. Protocol Buffers offer distinct advantages over alternatives like JSON and XML:| Feature | Protocol Buffers | JSON | XML |
|---|---|---|---|
| Format | Binary | Text | Text |
| Schema | Required (.proto) | Optional | Optional (XSD) |
| Type safety | Strong | Weak | Moderate |
| Size | Smaller | Larger | Largest |
| Parse speed | Faster | Slower | Slowest |
| Human readable | No | Yes | Yes |
Key use cases
Remote procedure calls (RPC) Protobuf is the standard serialization format for gRPC, Google’s open-source RPC framework. Service definitions and messages are described in.proto files, and protoc generates both client and server stubs in your language of choice.
Data storage
Protobuf’s compact binary format makes it well-suited for storing structured records. Because the schema is separate from the data, you can evolve your schema over time while preserving backward and forward compatibility.
Configuration and data exchange
Any application that needs to exchange structured data between services — including microservice architectures, mobile clients, and embedded systems — benefits from protobuf’s small payload size and strong typing.
How it works
The workflow for using Protocol Buffers follows three steps:Define your schema
Write a
.proto file that describes your data structures as messages. Each message is a small logical record containing a series of typed fields.addressbook.proto
Compile with protoc
Run the
protoc compiler against your .proto file, specifying your target language. The compiler generates source files you include directly in your project.Supported languages
Protobuf ships with runtime libraries and code generators for a wide range of languages:| Language | Source |
|---|---|
| C++ (includes protoc) | src/ |
| Java | java/ |
| Python | python/ |
| C# | csharp/ |
| Ruby | ruby/ |
| PHP | php/ |
| Objective-C | objectivec/ |
| Go | github.com/protocolbuffers/protobuf-go |
| Dart | github.com/dart-lang/protobuf |
| JavaScript | github.com/protocolbuffers/protobuf-javascript |
All officially supported language runtimes use the same binary wire format, so a message serialized by a Java program can be parsed by a Python program without any additional configuration.
Next steps
Quickstart
Define your first
.proto file, compile it, and use the generated code in Python and Java.Installation
Install the
protoc compiler and the runtime library for your language.