liveBook · Manning

Protobuf

As explained in Microservices Patterns, Second Edition

Protocol Buffers is a language-neutral, platform-neutral mechanism for serializing structured data using compact binary encoding and numeric field tags.

Protocol Buffers is a binary format for structured message payloads, designed for compactness and efficient processing. Numeric field tags identify values, letting consumers skip unknown tags when their local schema is missing fields or has changed. section 8.1.6

Message structures are defined in a typed IDL. A compiler generates message classes and serialization code; this API-first workflow requires extra tooling, but can let statically typed clients catch API misuse at compile time. section 8.1.6

With gRPC, the Protocol Buffers-based IDL defines services and request/response messages. The compiler generates client stubs and server code; clients and servers exchange binary messages over HTTP/2. section 8.2.3

Table 8.3 The benefits and drawbacks of text-based vs. binary formats for message payloads

Format

Benefits

Drawbacks

Text

  • Human-readable
  • Simple to process
  • Self descriptive
  • Verbose
  • Inefficient to process large messages

Binary

  • Compact
  • Fast to process
  • Requires more tooling
  • Not human-readable

Compare the compactness and processing benefits of binary payloads with their tooling and readability drawbacks. section 8.1.6

Listing 8.1 An excerpt of the gRPC API for the Order Service
package net.chrichardson.ftgo.orderservice_v1;

service OrderService {
  rpc createOrder(CreateOrderRequest) returns (CreateOrderReply) {}
  rpc cancelOrder(CancelOrderRequest) returns (CancelOrderReply) {}
  rpc reviseOrder(ReviseOrderRequest) returns (ReviseOrderReply) {}
  ...
}

message CreateOrderRequest {
  int64 restaurantId = 1;
  int64 consumerId = 2;
  repeated LineItem lineItems = 3;
  ...
}

message LineItem {
  string menuItemId = 1;
  int32 quantity = 2;
}


message CreateOrderReply {
  int64 orderId = 1;
}
...

Inspect an Order Service definition and see numeric tags on request-message fields. section 8.2.3

How Microservices Patterns, Second Edition covers it

How deep it goes
Explains typed IDL, generated code, field tags, and unknown-tag skipping, but not the binary wire encoding.
Examples
Uses an Order Service API with CreateOrderRequest fields, numeric tags, and generated gRPC client and server code.
What you can do after
Apply it via the Protocol Buffer compiler to generate typed messages and client/server code for communication.
How it connects
Connects Protocol Buffers with gRPC, Avro, IDL, message serialization, and microservice interprocess communication.

Also explained in

Compare how 12 books explain protobuf →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Microservices Patterns, Second Edition. The section links point to the parts of the book it draws on.