PROTOBUF
EXPLAINED
Learn about important Protobuf concepts in a more interactive and visual way. Learn how Protobuf works, from the language to the binary encoding.
Protocol Buffers (Protobuf) is a schema-driven format for serializing structured data.
Shared Contract
A Protobuf schema acts as a single source of truth for your APIs and data. Use the same type definitions across teams and languages.
Type Safety
Shared schemas allow you to generate code that only accepts properly shaped data.
Compatibility
Schemas can evolve without breaking the code that is already deployed. Clients that are built against older versions of the schema keep working thanks to forward compatibility.
How it works
Protocol Buffers, or "Protobuf", refers to two things: an Interface Definition Language (IDL) for writing schemas, and the encodings those schemas produce. Protobuf messages have two possible encodings. One is a compact binary form; the other is JSON, which uses a standardized JSON mapping. Both encodings are part of the specification, and either can go over the wire. Code generated from a schema can read and write both representations.
JSON is a text-based format that includes field names and readable values. Because of this, you can read it without needing to reference the Protobuf schema. Any tool that speaks JSON can just as easily read it as well. The binary encoding is a bit different. It only writes field numbers to identify each field. A decoder needs to reference the schema to know that field 2 is name, for example.
The Binary section explains how those bytes are laid out.
From schema to code
You don't write any of the encoding above by hand. A compiler reads the schema and generates native code for each language you target: structs in Go, classes in TypeScript and Java, and so on. The generated code gives you typed constructors, the binary serialization, and the JSON mapping, so a message feels like any other object in your language.
Next
Basics
Messages, fields, field numbers, enums, packages, and collections, ending with generating code from a schema and putting it to work.