Protocol Buffers
Protocol Buffers, or protobuf, is a way to describe structured data and encode it as bytes. A shared schema lets programs written in different languages exchange that data.
Also known as: protobuf
What Protocol Buffers does
Suppose a billing service written in Java publishes an event that a Go service needs to read. The two programs do not need to share classes or agree on how objects are laid out in memory. They agree on a protobuf schema instead. The producer serializes a message into bytes, and the consumer interprets those bytes using its copy of the schema. The same encoding works for messages stored on disk as well as messages sent over a network.
From schema to bytes
A .proto file defines message types and gives each field a name, a data type, and a field number. The protoc compiler and the appropriate generator can turn this definition into language-specific code for creating, encoding, and reading messages.
The binary encoding identifies fields by number rather than by name. A field's tag also carries a wire type, which tells the parser how to read or skip the encoded value. It does not tell the parser everything about the field: strings and raw byte arrays, for instance, use the same wire type. Omitting field names and using fewer bytes for small integers can make protobuf smaller than JSON. The difference depends on the payload, so measure it if size is a deciding factor.
Example: changing an event without a coordinated deploy
syntax = "proto3";
message InvoicePaid {
uint64 invoice_id = 1;
uint64 amount_cents = 2;
optional string receipt_id = 3;
}
Here 3 identifies receipt_id; it is not the field's value or a position in the byte stream. If receipt_id is added later, an older consumer can skip the field and still read the invoice ID and amount. A newer consumer can read events produced before the field existed. The optional declaration lets it distinguish an absent receipt ID from one explicitly set to an empty string.
Where it fits
Protobuf is the usual message format for gRPC, but the two solve different problems. Protobuf encodes the request and response data; gRPC handles remote calls and streaming. You can also put protobuf messages on a Kafka topic without using gRPC at all. JSON is often easier to inspect with ordinary text tools. Binary protobuf generally requires a decoder and the message schema to make sense of a captured payload.
What interviewers ask
A common question is whether a deleted field's number can be reused. Suppose a Kafka consumer replays an event written before the deletion. If the new field uses the same number and a compatible wire type, the consumer may read the old value as though it belonged to the new field. Mark the retired number as reserved in the schema so it cannot be reassigned. Also distinguish wire compatibility from application compatibility: a consumer may parse an event successfully and still make the wrong decision if the producer has changed what a field means.