With gRPC, a client application calls a method directly on a server on another machine, as if it were a local object. Like many RPC systems, gRPC is built around defining a service: the callable methods along with their parameters and return types. The server implements this interface, and the client holds a so-called stub with the same methods.

As its interface definition language (IDL) and its exchange format, gRPC uses Protocol Buffers by default. The structure is described in a text file with a .proto extension, built from message records with numbered fields. At build time, the proto compiler generates native code for many languages from this, so that, for example, a Java server can talk to clients written in Go, Python, or Ruby.

gRPC runs over HTTP/2 and supports four types of calls:

  • Unary: one request, one response, like a normal function call.
  • Server streaming: the client sends one request and reads a stream of responses.
  • Client streaming: the client sends a sequence of messages, and the server responds once.
  • Bidirectional streaming: both sides send independently over a read-write stream; the order of messages within a single call stays guaranteed.

Advantages: high performance thanks to the compact binary format (smaller and faster than JSON, according to protobuf.dev), automatic code generation with strong typing, and native streaming. Disadvantages: the data is binary and therefore not human-readable, direct browser support is limited (gRPC-Web is needed as an intermediary layer), and according to the documentation Protocol Buffers aren't well suited for very large payloads beyond a few megabytes. Typical use is therefore internal microservices, where speed and fixed contracts matter more than readability.

Protocol Buffers
service HelloService {
  rpc SayHello (HelloRequest) returns (HelloResponse);
}

message HelloRequest {
  string greeting = 1;
}

message HelloResponse {
  string reply = 1;
}

A concise .proto definition: a service with one unary method, plus request and response messages (source: gRPC Core concepts).

Sources

Keep reading