What gRPC is and how it works
gRPC is a remote procedure call (RPC) framework: the client calls a method of a service on another machine as if it were a method of a local object. Methods and messages are described in a .proto file in the Protocol Buffers language, the protoc compiler generates the client and server code, and the calls travel over HTTP/2.
gRPC suits communication between microservices, between mobile clients and the server, and everywhere that needs a strict contract for several languages and streams of messages: a Java server is called from Go, Python or Ruby through the same .proto. For calls from the browser there is a separate project, gRPC-Web. A public API that people read with their own eyes and call through curl is easier to build with REST and JSON.
The contract is a .proto file: it contains the service, its methods and messages. protoc with a language plugin generates from it a stub for the client and a service interface for the server. The client code calls a method of the stub; the stub packs the arguments into a protobuf message and sends it through a channel — an HTTP/2 connection to the server. The gRPC server parses the request, calls your implementation of the method and returns the response together with a status.
.proto file.Every call is a separate HTTP/2 stream. The method is passed in the request path, for example /orders.v1.OrderService/GetOrder, the messages in DATA frames, and the status code in trailers at the end of the response. This is why one connection carries many calls at once, and the server can answer with a stream of messages.
How it differs from REST and JSON
| Aspect | gRPC | REST and JSON |
|---|---|---|
| Contract | The .proto file is required; the client and the server are generated from it | An API description (for example, OpenAPI) is optional |
| Message format | Binary Protocol Buffers; JSON can be plugged in | Text, usually JSON |
| Addressing | A method of a service: POST /orders.v1.OrderService/GetOrder | A resource in the URL and an HTTP method |
| Streams | Four kinds of call, including the bidirectional stream | A request and a response |
| Errors | 17 gRPC status codes | HTTP response codes |
| Debugging | Needs .proto or reflection on the server and a tool like grpcurl | curl and a browser |
Choose gRPC for internal APIs between services, for streams of data and for clients in different languages; choose REST for public APIs that are called from the browser and read by people.
What to know in advance
- HTTP/2 streams and frames — the HTTP and REST reference, section “HTTP/1.1, HTTP/2 and HTTP/3”.
- The language the service is written in — the Go, Python or Java and Spring references.
- Certificates and TLS for secure channels — the TLS and PKI reference.
Section sources
the section is written from these pages- Introduction to gRPCgrpc.io/docs/what-is-grpc/introduction/(opens in a new tab)
- FAQgrpc.io/docs/what-is-grpc/faq/(opens in a new tab)
- gRPC over HTTP2github.com/grpc/grpc/blob/master/doc/PROTOCOL-HTTP2.md(opens in a new tab)
- Core concepts, architecture and lifecyclegrpc.io/docs/what-is-grpc/core-concepts/(opens in a new tab)
- Reflectiongrpc.io/docs/guides/reflection/(opens in a new tab)