What are gRPC stubs and how does code generation work?
Learn what gRPC stubs are and how protoc code generation works — client proxies, server skeletons, .proto files, examples and common interview questions.
Expected Interview Answer
A gRPC stub is the auto-generated client-side object that exposes the service's remote methods as ordinary local function calls, hiding all the serialization, networking, and HTTP/2 details. It is produced by the protoc compiler from the service definition in a .proto file.
You define services and messages in a .proto file, then run protoc with a language plugin (e.g. grpc-java, grpc-go, grpc-python). This generates message classes plus a stub for the client and a base/skeleton class for the server. Calling a stub method marshals the request via Protocol Buffers, sends it over HTTP/2 to the server, and returns the deserialized response — so developers write normal method calls instead of manual network code. Stubs can be blocking (synchronous), async, or future-based depending on the language.
- Remote calls look like local method calls
- No hand-written serialization or networking code
- Type-safe, generated from a single source of truth
- Consistent client/server contract across languages
- Supports blocking, async, and streaming call styles
AI Mentor Explanation
A stub is like the team's runner who fetches drinks and messages between the dressing room and the pitch: the batter simply says what they need and the runner handles the walk, the gate, and the return trip. You call a plain request and the stub quietly does the whole journey to the server and back for you.
Step-by-Step Explanation
Step 1
Write the .proto
Define the service with its rpc methods plus the request and response message types.
Step 2
Run protoc
Invoke the protobuf compiler with the appropriate gRPC language plugin for your target language.
Step 3
Get generated code
protoc emits message classes, a client stub, and a server base class to implement.
Step 4
Implement the server
Subclass the generated base class and provide the method logic.
Step 5
Call via the stub
On the client, create a channel, build a stub, and call methods as if they were local functions.
What Interviewer Expects
- Defining a stub as the generated client proxy
- Knowing protoc plus a language plugin generates it
- Understanding the .proto is the single source of truth
- Awareness of blocking vs async vs future stubs
- That the stub hides serialization and HTTP/2 transport
Common Mistakes
- Thinking stubs are hand-written
- Confusing the client stub with the server skeleton/base class
- Not knowing a language-specific plugin is required
- Assuming one stub style exists rather than blocking/async variants
- Forgetting a channel is needed to build a stub
Best Answer (HR Friendly)
“A gRPC stub is auto-generated code that lets you call a remote service as if it were a normal function in your program. A tool called protoc reads the service definition file and generates these stubs, so developers do not have to write the networking code themselves.”
Code Example
protoc \
--go_out=. --go_opt=paths=source_relative \
--go-grpc_out=. --go-grpc_opt=paths=source_relative \
greeter.protoconn, err := grpc.Dial("localhost:50051", grpc.WithInsecure())
if err != nil {
log.Fatal(err)
}
defer conn.Close()
client := pb.NewGreeterClient(conn) // the generated stub
resp, err := client.SayHello(ctx, &pb.HelloRequest{Name: "Ada"})
if err != nil {
log.Fatal(err)
}
log.Println(resp.Message)Follow-up Questions
- What is the difference between a blocking and an async stub?
- What role does the gRPC channel play when creating a stub?
- How does the server side differ from the client stub?
- Which protoc plugins generate gRPC code for different languages?
- How does the stub handle streaming methods?
MCQ Practice
1. What is a gRPC stub?
A stub is the generated client-side object that exposes remote methods as local calls.
2. What tool generates gRPC stubs from a .proto file?
The Protocol Buffers compiler protoc, with a language-specific gRPC plugin, generates the stubs.
3. The client stub primarily hides which details from the developer?
The stub marshals requests via protobuf and handles the HTTP/2 network calls transparently.
Flash Cards
What is a gRPC stub? — Auto-generated client code that exposes remote service methods as local function calls.
What generates stubs? — The protoc compiler run with a language-specific gRPC plugin, from the .proto file.
Client stub vs server code? — The stub is the client proxy; the server implements a generated base/skeleton class.
What does a stub hide? — Serialization to protobuf and the HTTP/2 network transport.