liveBook · Manning

Protocol buffer

As explained in Akka in Action, Second Edition

Protocol Buffers is a language-neutral schema format and serialization mechanism for structured data with generated language bindings.

Protocol Buffers (protobuf) is a language-neutral interface definition language for describing structured data in .proto files. Compiler libraries generate data types plus serializers and deserializers for a chosen implementation language, avoiding a schema tied to Scala, Java, Go, or Python. Its generated serialization is positioned for high performance, efficient compression, and long-lived schema development. section 11.2.1 section 11.2

A protobuf definition separates a portable contract from its generated implementation:

  • Declare messages and numbered fields in a .proto file; protobuf scalar fields map to implementation-language types, such as string to Scala String and int32 to Int. section 11.2.3
  • Declare a service and RPC methods when the schema is also a gRPC API contract. section 11.2.3
  • Compile the definition to generate message classes, serialization support, and service/client interfaces rather than hand-writing them. section 11.2.1 section 11.2.3
  • Treat schema evolution as an operational concern: protobuf offers flexible schema development, but setup and maintenance require more work than simpler serializers. section 8.5.1

Cargo service contract

A proto3 contract can define Cargo with kind and size, AddedCargo with an ok result, and ContainerService with a unary AddCargo RPC. After compilation, the generated Scala service exposes addCargo(Cargo): Future[AddedCargo]; generated request and response marshallers handle the protobuf messages. section 11.2.3

Listing 11.5 .proto file
syntax = "proto3";                                    #1
 
option java_package = "example.container.grpc";       #2
option java_outer_classname = "ContainerProto";       #3
 
message Cargo {                                       #4
      string kind = 1;
      int32 size = 2;
}
message AddedCargo {                                  #4
      bool ok = 1;
}
service ContainerService {                            #5
 
      rpc AddCargo (Cargo) returns (AddedCargo) {}    #6
}

This compact proto3 definition shows message fields, Java generation options, a service, and a unary request/response RPC in one portable contract. section 11.2.3

Protobuf can also carry event data outside RPC. A Kafka market projection maps domain events such as market opening, closure, and cancellation to generated protobuf messages, packs them into protobuf Any, converts them to Array[Byte], and sends the bytes as a Kafka record value. section 15.1.2

For clustered message serialization, protobuf is an alternative to Jackson-based formats: it is described as offering better throughput than Jackson and flexible schema development, while requiring additional setup and maintenance. Choose a serializer with future schema evolution in mind. section 8.5.1

How Akka in Action, Second Edition covers it

How deep it goes
Explains .proto messages, scalar mappings, service RPCs, compilation, and generated Scala artifacts without detailing schema-evolution procedures.
Examples
Uses a Cargo/AddCargo container service and MarketClosed, MarketOpened, and MarketCancelled Kafka event messages.
What you can do after
Build it from scratch by defining .proto messages and services, then compiling generated Scala classes and gRPC interfaces.
How it connects
Connects Protocol Buffers with gRPC, HTTP/2, Kafka projections, Akka serialization, Jackson, and schema evolution.

Also explained in

Compare how 10 books explain protocol buffer →

Related concepts

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