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









