Protocol Buffers Reference
Free reference guide: Protocol Buffers Reference
About Protocol Buffers Reference
The Protocol Buffers Reference is a structured, searchable guide covering all essential syntax of Protocol Buffers (protobuf) version 3, Google's language-neutral binary serialization format used in gRPC services and high-performance data storage. It is organized into five categories: Types (scalar types: double, float, int32, int64, uint32, uint64, bool, string, bytes), Messages (message definition, enum, oneof for union types, map<K,V>, repeated for arrays, nested messages), Services (service and rpc definitions for gRPC, unary and bidirectional streaming with stream keyword), Options (import, package namespace, language-specific code generation options like go_package, java_package), and Encoding (reserved fields/names to prevent compatibility issues, wire type reference for Varint/64-bit/length-delimited/32-bit).
Backend engineers, platform architects, and mobile developers use this reference when designing gRPC APIs, defining data schemas for microservices communication, migrating from JSON/XML to binary serialization for performance, and implementing streaming RPC patterns. Protocol Buffers are the default serialization format for gRPC and are used at scale by Google, Uber, Netflix, and other companies needing efficient, strongly-typed inter-service communication.
Each entry shows the proto3 syntax with a concrete .proto file example demonstrating the field declaration or service definition in a realistic microservice context. The reference covers field number assignment rules, the significance of the zero value for enums (UNKNOWN = 0 convention), the difference between oneof fields and optional fields, how map<string, int32> differs from repeated key-value messages, and the four wire types used in the binary encoding format.
Key Features
- All proto3 scalar types: double, float, int32, int64, uint32, uint64, bool, string, bytes with field number assignment
- Message definitions with nested messages and multiple field types in a single .proto file
- Enum definition with the required zero value (UNKNOWN = 0) convention and integer mapping
- oneof for mutually exclusive fields where only one value can be set at a time
- map<K, V> for key-value pairs (equivalent to dict/HashMap) with allowed key types
- repeated keyword for array/list fields of any scalar or message type
- gRPC service and rpc definition: unary RPC, server streaming, client streaming, bidirectional streaming
- Wire type reference: Varint (0), 64-bit (1), length-delimited (2), 32-bit (5) for understanding the binary encoding
Frequently Asked Questions
What are Protocol Buffers (protobuf) and why use them over JSON?
Protocol Buffers are Google's language-neutral, platform-neutral binary serialization format. Compared to JSON, protobuf serializes data 3-10x smaller and 5-10x faster, provides strong schema enforcement (you cannot send undefined fields), generates type-safe code in multiple languages from a single .proto definition, and supports schema evolution (adding new fields without breaking old clients).
What is the difference between proto2 and proto3?
proto3 simplified proto2 by removing required fields (all fields are optional by default), removing default values (fields default to zero/empty), and dropping field presence tracking for scalar types. proto3 is the recommended version for new projects, especially with gRPC. The key differences are: no required keyword, no custom defaults, first enum value must be 0.
Why does the first enum value in protobuf need to be 0?
In proto3, the default value of an enum field is the first defined value, and that value must be 0. This ensures that when a message is deserialized and the enum field is missing or unrecognized, it defaults to the zero value. The convention is to name the zero value UNKNOWN or UNSPECIFIED to explicitly represent the "not set" or "default" state.
When should I use oneof vs optional fields in protobuf?
Use oneof when you have a set of mutually exclusive fields where only one can be set at a time (like a union type). oneof guarantees that setting one field automatically clears all others in the group. Regular optional fields are independent — multiple can be set simultaneously. oneof is useful for polymorphic messages (e.g., a Result containing either a string text, an int32 code, or a bool flag).
How do I define a gRPC streaming RPC in protobuf?
Add the stream keyword before the request or response type. Server streaming: `rpc ListUsers(ListRequest) returns (stream User)` — server sends multiple User messages. Client streaming: `rpc UploadFile(stream Chunk) returns (UploadResponse)` — client sends multiple chunks. Bidirectional streaming: `rpc Chat(stream Message) returns (stream Message)` — both sides send multiple messages.
What are reserved fields in protobuf and why are they important?
When you remove a field from a message, its field number should be reserved to prevent future developers from reusing it. If a new field reuses an old number, old and new clients will silently misinterpret each other's data. Use `reserved 2, 15, 9 to 11;` to reserve field numbers and `reserved "old_field_name";` to reserve names to prevent reuse.
What is the difference between int32 and sint32 in protobuf?
int32 uses standard Varint encoding, which is inefficient for negative numbers (a value of -1 takes 10 bytes). sint32 uses ZigZag encoding, which is efficient for negative numbers (maps positive and negative integers to unsigned integers). Use sint32 or sint64 when your field commonly holds negative values. For non-negative values only, uint32 is the most space-efficient.
How do proto3 field numbers work and what are the reserved ranges?
Each field in a proto3 message must have a unique field number from 1 to 536,870,911. Field numbers 1-15 use one byte for encoding (use for frequently set fields), while 16-2047 use two bytes. Field numbers 19000-19999 are reserved by Google's protobuf implementation and must not be used. Once a field number is assigned and deployed, changing it breaks backward compatibility.