<img height="1" width="1" style="display:none" src="https://www.facebook.com/tr?id=1063935717132479&amp;ev=PageView&amp;noscript=1 https://www.facebook.com/tr?id=1063935717132479&amp;ev=PageView&amp;noscript=1 "> Protobuf

Protobuf

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.
JSON payload { "title": "The Hobbit", "available": true } key names + quotes ship in every message Protobuf payload 2 (title) → "The Hobbit" 3 (available) → true // only the number is in the bytes field numbers only — smaller and faster to parse
JSON ships every key name, quote, and brace in every message. Protobuf sends only the field number and a typed value; the names live in generated code, never on the wire.

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 the title field
  • __3__ the keyword that marks WatchBooks as server-streaming

Check your understanding

Check your understanding

Which schema change breaks existing Protobuf clients: renaming title to bookTitle, or changing its number from = 2 to = 5?