What are Protocol Buffers (protobuf) and how do they work?
Understand Protocol Buffers (protobuf): how .proto schemas, protoc and binary field-number encoding produce compact, fast, cross-language data for gRPC.
Expected Interview Answer
Protocol Buffers (protobuf) are a language-neutral, platform-neutral mechanism for serializing structured data into a compact binary format, defined by a schema in a .proto file.
You declare messages with typed, numbered fields in a .proto file, then compile it with protoc to generate data-access classes in many languages. At runtime protobuf encodes each field using its field number as a tag plus a compact wire-type encoding, producing small, fast binary payloads. Because both sides share the schema and fields are identified by number, protobuf supports forward and backward compatibility as schemas evolve.
- Very compact binary payloads
- Fast serialization and deserialization
- Strongly-typed, schema-driven data
- Cross-language code generation
- Backward and forward compatibility via field numbers
- Underpins gRPC message transport
AI Mentor Explanation
Protobuf is like a scorer's shorthand where each event has a fixed numbered code instead of a full sentence. A dot ball, a four, a wicket each become a tiny agreed symbol, so the whole innings compresses into a compact record that any scorer sharing the codebook can expand back into full detail.
Step-by-Step Explanation
Step 1
Define messages
Write a .proto file declaring messages with typed fields, each assigned a unique field number.
Step 2
Compile the schema
Run protoc to generate data-access classes in your target languages.
Step 3
Serialize data
Populate a generated message object and encode it to a compact binary byte stream.
Step 4
Transmit or store
Send the bytes over the network (for example via gRPC) or persist them.
Step 5
Deserialize
The receiver, sharing the same schema, decodes the bytes back into a typed object using field numbers.
What Interviewer Expects
- Definition of protobuf as a binary serialization format
- Understanding of the .proto schema and protoc compiler
- Knowledge that field numbers, not names, are encoded
- Awareness of backward/forward compatibility rules
- Connection to gRPC as the message format
Common Mistakes
- Calling protobuf a transport protocol rather than a serialization format
- Thinking field names are sent on the wire instead of field numbers
- Reusing or changing field numbers and breaking compatibility
- Assuming protobuf is human-readable like JSON
- Forgetting both sides must share the schema
Best Answer (HR Friendly)
“Protocol Buffers are a way to package structured data into a small, fast binary format using a shared blueprint. You describe your data once in a schema file, generate code from it, and then programs in different languages can efficiently exchange that data.”
Code Example
syntax = "proto3";
message User {
int32 id = 1;
string name = 2;
string email = 3;
repeated string roles = 4;
}Follow-up Questions
- Why does protobuf use field numbers instead of names?
- How does protobuf achieve backward compatibility?
- How does protobuf compare to JSON in size and speed?
- What is the role of protoc?
- Can you change a field's type safely in a .proto file?
MCQ Practice
1. What does the protoc compiler produce from a .proto file?
protoc generates typed data-access code in target languages from the .proto schema.
2. What is primarily encoded on the wire to identify each protobuf field?
Protobuf encodes each field's number as a tag, not its name, which keeps payloads compact.
3. Why is reusing an old field number dangerous?
Old data uses that number for the previous field, so reusing it corrupts decoding and breaks compatibility.
Flash Cards
What is protobuf? — A compact, language-neutral binary serialization format defined by a .proto schema.
What does protoc do? — Compiles a .proto file into typed data-access classes for many languages.
What is encoded on the wire? — Field numbers as tags plus compact wire-type values, not field names.
How does protobuf stay compatible? — Stable field numbers let readers ignore unknown fields and keep old ones working.