Protocol Buffers (Protobuf) is the schema language and binary serialization
format underneath gRPC. You define messages and services in a .proto file: one
schema, every language:
syntax = "proto3";
package library;
service BookService {
rpc ListBooks (ListBooksRequest) returns (ListBooksResponse);
rpc GetBook (GetBookRequest) returns (Book);
rpc CreateBook (CreateBookRequest) returns (Book);
rpc CheckoutBook (CheckoutRequest) returns (Book);
// Server-streaming: push an update every time inventory changes
rpc WatchBooks (WatchRequest) returns (stream Book);
}
message Book {
int32 id = 1;
string title = 2;
bool available = 3;
string author = 4;
}
message CheckoutRequest { int32 id = 1; }
message GetBookRequest { int32 id = 1; }
message CreateBookRequest { string title = 1; string author = 2; }
message ListBooksRequest {}
message ListBooksResponse { repeated Book books = 1; }
message WatchRequest {} Field numbers are the wire format
The field numbers (= 1, = 2) are the real identifiers
sent over the wire; human-readable field names are entirely stripped from the binary payload. That
has a consequence worth burning into memory:
- Renaming a field is safe — the name only exists in generated code.
- Changing a field's number is a breaking change — old clients and new servers now disagree about which bytes mean what. Never reassign a number without a migration plan.
- Adding new fields and removing old ones are both backward-compatible — unknown fields are skipped, missing fields get defaults.
This is what makes Protobuf payloads so much smaller and faster to parse than JSON: no key names, no quotes, no whitespace, just numbered, typed binary fields.
Exercise
Complete this .proto file. Fill in the keyword that declares a data shape, the wire
number for the title field, and the keyword that marks WatchBooks as
server-streaming.
Code challenge
Complete the .proto schema
Complete the .proto by replacing each blank, then return it as a
string. Fill in:
__1__the keyword that declares a data shape (a record of typed fields)__2__the next free wire number for thetitlefield__3__the keyword that marksWatchBooksas server-streaming
Check your understanding
Check your understanding