gRPC
RPC Semantics
Network Protocols
Web Development
Programming

RPC semantics of gRPC

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

Google's gRPC framework is designed around the idea that you can define your application's communication protocol in a language-agnostic way and then use autogenerated client-server stubs to do the remote communication. gRPC uses Protocol Buffers (protobuf) as its Interface Definition Language (IDL) to define both the service interface and the structure of the payload messages.

RPC Semantics in gRPC

Remote Procedure Call (RPC) semantics define how functions or procedures in one computer's process can appear to be called within another process, potentially on a different computer. With gRPC, these semantics are expressed using protobufs, and the framework provides several types of RPCs.

Types of RPCs Supported in gRPC:

Unary RPCs

The simplest type of RPC, where the client sends a single request to the server and gets back a single response. It's effectively a single function call.

Example:

proto
service Greeter {
  rpc SayHello (HelloRequest) returns (HelloResponse);
}

Server streaming RPCs

The client sends a request to the server and gets a stream to read a sequence of messages back. The client reads from the returned stream until there are no more messages.

Example:

proto
service Greeter {
  rpc LotsOfReplies(HelloRequest) returns (stream HelloResponse);
}

Client streaming RPCs

The client writes a sequence of messages and sends them to the server, again using a provided stream. Once it has finished writing the messages, it waits for the server to read them and return a response.

Example:

proto
service Greeter {
  rpc LotsOfGreetings(stream HelloRequest) returns (HelloResponse);
}

Bidirectional streaming RPCs

Both sides send a sequence of messages using a read-write stream. The two streams operate independently, so clients and servers can read and write in whatever order they like: for example, the server could wait to receive all the client's messages before writing its responses, or it could alternately read a message then write a message, or some other combination of reads and writes.

Example:

proto
service Greeter {
  rpc BidiHello(stream HelloRequest) returns (stream HelloResponse);
}

Deadlines/Timeouts

gRPC allows clients to specify how long they are willing to wait for an RPC to complete before the call is terminated with the error DEADLINE_EXCEEDED. On the server side, the server can query if a particular call is still viable or if too much time has elapsed.

Metadata

gRPC enables both client and server to send additional metadata in the form of key-value pairs, both at the beginning and at the end of a call. This feature is often used for sending authentication tokens or other contextual information.

Error Handling

gRPC has built-in support for conveying error conditions from server to client. In gRPC, both clients and servers can provide a status code and a status message, as well as an optional status details, which can be used to describe the error programmatically.

Security

gRPC has built-in support for Transport Layer Security (TLS) and its predecessor, Secure Sockets Layer (SSL). gRPC users can configure SSL/TLS to authenticate the server and can also configure the client, providing mutual authentication.

Summary Table

FeatureDescriptionExample
Unary RPCSimple request-response pattern.rpc SayHello (HelloRequest) returns (HelloResponse);
Server streaming RPCClient sends a request, gets a stream to read a sequence of messages.rpc LotsOfReplies(HelloRequest) returns (stream HelloResponse);
Client streaming RPCClient writes a sequence of messages, server returns a single response.rpc LotsOfGreetings(stream HelloRequest) returns (HelloResponse);
Bidirectional streaming RPCBoth client and server send a sequence of messages using a stream.rpc BidiHello(stream HelloRequest) returns (stream HelloResponse);
Deadlines/TimeoutsClient specifies maximum wait time for a response before the call fails.Not applicable
MetadataKey-value pairs sent at the start/end of a call.Not applicable
Error HandlingCommunicates errors via status codes and messages.Not applicable
SecuritySupports TLS/SSL for secure communication.Not applicable

This summary shows the versatility and robustness of gRPC, making it a popular choice for microservices architectures and systems requiring efficient and robust service-to-service communication.


Related reading
Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

All Rights Reserved.