gRPC
Last updated
Last updated
gRPC is a modern open source high performance Remote Procedure Call (RPC) framework that can run in any environment.
It can efficiently connect services in and across data centers with pluggable support for load balancing, tracing, health checking and authentication.
It is also applicable in last mile of distributed computing to connect devices, mobile applications and browsers to backend services.
Efficiently connecting polyglot services in microservices style architecture
Connecting mobile devices, browser clients to backend services
Generating efficient client libraries
Idiomatic client libraries in 11 languages
Highly efficient on wire and with a simple service definition framework
Bi-directional streaming with http/2 based transport
Pluggable auth, tracing, load balancing and health checking
In gRPC, a client application can directly call a method on a server application on a different machine as if it were a local object, making it easier for you to create distributed applications and services.
As in many RPC systems, gRPC is based around the idea of defining a service, specifying the methods that can be called remotely with their parameters and return types.
On the server side, the server implements this interface and runs a gRPC server to handle client calls.
On the client side, the client has a stub (referred to as just a client in some languages) that provides the same methods as the server.
gRPC clients and servers can run and talk to each other in a variety of environments - from servers inside Google to your own desktop - and can be written in any of gRPC’s supported languages.
So, for example, you can easily create a gRPC server in Java with clients in Go, Python, or Ruby.
In addition, the latest Google APIs will have gRPC versions of their interfaces, letting you easily build Google functionality into your applications.
By default, gRPC uses Protocol Buffers, Google’s mature open source mechanism for serializing structured data (although it can be used with other data formats such as JSON).
Here’s a quick intro to how it works. If you’re already familiar with protocol buffers, feel free to skip ahead to the next section.
The first step when working with protocol buffers is to define the structure for the data you want to serialize in a proto file: this is an ordinary text file with a .proto
extension.
Protocol buffer data is structured as messages, where each message is a small logical record of information containing a series of name-value pairs called fields.
Here’s a simple example:
Then, once you’ve specified your data structures, you use the protocol buffer compiler protoc
to generate data access classes in your preferred language(s) from your proto definition.
These provide simple accessors for each field, like name()
and set_name()
, as well as methods to serialize/parse the whole structure to/from raw bytes.
So, for instance, if your chosen language is C++, running the compiler on the example above will generate a class called Person
.
You can then use this class in your application to populate, serialize, and retrieve Person
protocol buffer messages.
You define gRPC services in ordinary proto files, with RPC method parameters and return types specified as protocol buffer messages:
gRPC uses protoc
with a special gRPC plugin to generate code from your proto file: you get generated gRPC client and server code, as well as the regular protocol buffer code for populating, serializing, and retrieving your message types.
Characteristic
gRPC
REST API
HTTP Protocol
HTTP 2
HTTP 1.1
Messaging Format
Protobuf (Protocol Buffers)
JSON (usually) or XML and others
Code Generation
Native Protoc Compiler
Third-Party Solutions Like Swagger
Communication
Unary Client-Request or Bidirectional/Streaming
Client-Request Only
Implementation Time
45 Minutes
10 Minutes