What is an .proto file and how do you define a gRPC service?
Learn what a .proto file is, how Protocol Buffers define gRPC messages and services, how protoc generates code, plus examples and interview answers.
Expected Interview Answer
A .proto file is the contract for a gRPC API: it uses Protocol Buffers syntax to declare messages (the data shapes) and services (the RPC methods), and a compiler generates typed client and server code from it.
You define message types with numbered fields and a service block whose rpc methods each take one request message and return one response message. Running protoc with the gRPC plugin turns this single source of truth into stubs in Go, Java, Python, and other languages, so both sides agree on the wire format without hand-writing serialization.
- Single language-neutral contract shared by client and server
- Auto-generated, strongly typed stubs in many languages
- Compact, fast binary serialization
- Backward-compatible evolution via field numbers
- Clear, self-documenting API surface
AI Mentor Explanation
A .proto file is like the printed match playing conditions handed to both teams before a game: it fixes exactly what an over, a wicket, and a boundary mean so neither side argues mid-match. gRPC generates code from it the way umpires apply those agreed rules, keeping the batting and bowling sides in perfect sync.
Step-by-Step Explanation
Step 1
Set the syntax
Declare proto3 at the top of the file so the compiler knows the language version.
Step 2
Define messages
Create message types with typed, uniquely numbered fields for request and response payloads.
Step 3
Declare the service
Add a service block listing rpc methods, each mapping one request message to one response message.
Step 4
Compile with protoc
Run protoc with the gRPC plugin for your target language to generate client stubs and server interfaces.
Step 5
Implement and call
Implement the generated server interface and use the generated client stub to invoke the RPCs.
What Interviewer Expects
- Knowing .proto defines both messages and services
- Understanding field numbers and their role in compatibility
- Awareness that protoc generates multi-language code
- Ability to write a simple service and rpc definition
- Distinguishing the contract from its implementation
Common Mistakes
- Confusing a message with a service
- Reusing or changing field numbers and breaking compatibility
- Thinking .proto contains runtime logic rather than a contract
- Forgetting to specify the proto3 syntax line
Best Answer (HR Friendly)
“A .proto file is a shared blueprint that describes the data and the operations a gRPC service offers. A tool reads this blueprint and automatically creates matching code for many programming languages, so the client and server always agree on how to talk to each other.”
Code Example
syntax = "proto3";
package greet;
message HelloRequest {
string name = 1;
}
message HelloReply {
string message = 1;
}
service Greeter {
rpc SayHello (HelloRequest) returns (HelloReply);
}Follow-up Questions
- Why do Protocol Buffers use numbered fields instead of names on the wire?
- How do you evolve a .proto file without breaking existing clients?
- What is the difference between proto2 and proto3?
- How does protoc generate code for different languages?
- What happens if a client and server use mismatched .proto versions?
MCQ Practice
1. What does a service block in a .proto file define?
A service block lists rpc methods, each pairing one request message with one response message.
2. What is the purpose of the field number in a Protocol Buffers message?
Field numbers, not names, are used to encode fields on the wire, so keeping them stable preserves backward compatibility.
3. Which tool generates client and server code from a .proto file?
protoc, the Protocol Buffers compiler with a gRPC plugin, generates typed stubs from the .proto definition.
Flash Cards
What is a .proto file? — A Protocol Buffers file that defines messages and services as a language-neutral gRPC contract.
What does a message define? — A structured data type with numbered, typed fields used as request or response payloads.
What does protoc do? — Compiles a .proto file into strongly typed client stubs and server interfaces for target languages.
Why keep field numbers stable? — Wire encoding uses field numbers, so reusing or changing them breaks backward compatibility.