What is unary RPC in gRPC and when do you use it?
Learn what a unary RPC is in gRPC: one request, one response. See when to use it, how to define it in proto, and how it differs from streaming calls.
Expected Interview Answer
Unary RPC is the simplest gRPC call pattern: the client sends exactly one request message and receives exactly one response message, just like a normal function call over the network.
It is defined in the .proto file as a plain method with a single request and single response type (no stream keyword). The client blocks (or awaits) until the single response arrives, and the whole exchange rides over one HTTP/2 stream with Protocol Buffers serialization. Use it for classic request/response operations such as fetching a record, creating a resource, or validating a token, where there is no need to stream multiple messages in either direction.
- Simplest pattern to implement and reason about
- Maps cleanly to typical CRUD and lookup operations
- Strongly typed request and response via Protocol Buffers
- Built-in deadlines, cancellation and status codes
- Easy to cache, retry and load balance
AI Mentor Explanation
A unary RPC is like an umpire referral for a single delivery: the on-field umpire sends one clear question upstairs, the third umpire reviews it, and sends back exactly one verdict — out or not out. One request, one answer, and play does not continue until that single decision comes back down. There is no ongoing stream of updates, just a clean ask-and-reply for that one ball in question.
Step-by-Step Explanation
Step 1
Define the method
In the .proto file declare rpc GetUser(GetUserRequest) returns (GetUserResponse); with no stream keyword on either side.
Step 2
Generate stubs
Run protoc (or your language plugin) to generate the client stub and server base class from the service definition.
Step 3
Implement the server handler
Override the generated method: read the single request, do the work, and return one populated response message.
Step 4
Call from the client
Invoke the stub method with one request; the client blocks or awaits until the single response or an error status arrives.
Step 5
Handle status and deadlines
Set a deadline on the call and inspect the returned gRPC status code to handle success, NOT_FOUND, or timeouts.
What Interviewer Expects
- Clear statement that it is one request and one response
- Knowing it needs no stream keyword in the .proto
- Recognising it suits CRUD and lookup operations
- Awareness of deadlines, cancellation and status codes
- Contrast with the three streaming RPC patterns
Common Mistakes
- Confusing unary with client-side streaming
- Thinking unary calls cannot have deadlines or cancellation
- Believing unary is synchronous-only and has no async form
- Using unary where a stream of many messages is needed
- Forgetting it still runs over a single HTTP/2 stream
Best Answer (HR Friendly)
“A unary RPC is the simplest kind of gRPC call: the client asks one question and the server gives back one answer, just like a normal function call but over the network. You use it for everyday operations like looking up or saving a single record.”
Code Example
service UserService {
// One request, one response = unary
rpc GetUser(GetUserRequest) returns (GetUserResponse);
}
message GetUserRequest {
string user_id = 1;
}
message GetUserResponse {
string user_id = 1;
string name = 2;
}ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
defer cancel()
resp, err := client.GetUser(ctx, &pb.GetUserRequest{UserId: "42"})
if err != nil {
log.Fatalf("GetUser failed: %v", err)
}
fmt.Println(resp.Name)Follow-up Questions
- How does unary RPC differ from server-side streaming?
- How do deadlines and cancellation work on a unary call?
- What HTTP/2 features does a unary gRPC call rely on?
- How would you retry a failed unary call safely?
- When would unary be the wrong choice?
MCQ Practice
1. How many request and response messages does a unary RPC exchange?
Unary means exactly one request message and exactly one response message per call.
2. How is a unary method marked in a .proto file?
A unary method uses no stream keyword; adding stream makes it a streaming RPC instead.
3. Which use case best fits a unary RPC?
A single lookup is a classic one-request/one-response operation, ideal for unary.
Flash Cards
What is a unary RPC? — A gRPC call with exactly one request and one response message.
How is it declared in proto? — rpc Method(Req) returns (Resp); with no stream keyword.
When to use unary? — For request/response operations like lookups and CRUD writes.
Does unary support deadlines? — Yes — deadlines, cancellation and status codes all apply.