gRPC
What gRPC is, how it differs from JSON over REST, and its four kinds of RPC with a Protocol Buffers service example.
2 min readUpdated
gRPC is an open-source, high-performance remote procedure call (RPC) framework, originally developed at Google. A client calls a method on a server in another process or on another machine as if it were a local object. It connects services in and across data centres, with pluggable support for load balancing, tracing, health checking and authentication.
This note compares gRPC with JSON over REST and shows the four kinds of RPC that a gRPC service can define.
How gRPC differs from REST
Protocol Buffers instead of JSON or XML. REST APIs usually exchange JSON, which is flexible, text-based and human-readable, but verbose and comparatively slow to parse. gRPC uses Protocol Buffers (protobuf), a binary format that produces smaller messages and is faster to serialise and parse.
Code generation from the contract. A gRPC service is defined in a .proto file. The protoc compiler with the gRPC plugin for each language generates the message classes, the client stub and the server base class from it. REST APIs need extra tools, such as OpenAPI generators, to get the same result.
HTTP/2. REST APIs mostly run over HTTP/1.1 and follow a request-response model. gRPC requires HTTP/2, which multiplexes many calls over one connection and lets the client, the server or both send a stream of messages.
Works across languages. Because the code is generated from the same contract, services written in different languages can call each other directly. In the diagram below, a Python server answers requests from stubs generated for Go and Java.

Define the four kinds of RPC
A gRPC method takes either a single message or a stream of messages, and returns either a single message or a stream. That gives four kinds of RPC:
- Unary: the client sends one request and gets back one response, like a normal function call.
- Server streaming: the client sends one request and the server returns a stream of messages.
- Client streaming: the client sends a stream of messages and the server answers with one message.
- Bidirectional streaming: both sides send a stream. The two streams are independent, so client and server can read and write in any order.

The stream keyword in the service definition marks a streamed argument or return value. This service declares one method of each kind:
service GreetService { // Unary rpc Greet(GreetRequest) returns (GreetResponse) {};
// Server streaming rpc GreetManyTimes(GreetManyTimesRequest) returns (stream GreetManyTimesResponse) {};
// Client streaming rpc LongGreet(stream LongGreetRequest) returns (LongGreetResponse) {};
// Bidirectional streaming rpc GreetAll(stream GreetAllRequest) returns (stream GreetAllResponse) {};}The request and response types (GreetRequest and so on) are message definitions in the same file. The Java codelab in the references builds a complete service from such a file.