What is gRPC
gRPC is a high performance open-source freature-rich RPC (remote procedure calls) framework developed by google. “g” stands for many different meaning like green, good and etc.
It is a protocal that allows a program to
- execute a procedure of another program located in other computer
- without the developer explicitly coding the details for the remote interaction
gRPC is one kind of s2s call and widely used to replace REST API in the backend due to:
- less usage of bandwidth since all data are serialized
- support different languages
- easy to focus on the core logic and gRPC will handle all the boiler-plate code for us
gRPC is defining the next-gen of microservices structure with http2. In other words, gRPC is providing the same functions of HTTP2:
Thus gRPC outperforms REST in many cases:
How gRPC works
- client has a generated stub that provides the same methods as the server
- the stub calls gRPC framework under the hood to exchange information over the network
- since client and server use stubs to interact with each other, so they only need to implement their core service logic
How Stubs work
gRPC will be transferred between server and client via Stubs. Stubs are generated via proto buffers and then we could implement the core logic through the stubs. This we need the proto buffer compiler (aka. protoc) and gRPC plugin for corresponding langague to generate the boiler-plate.
Using proto buffer provides:
- human readable interface
- strongly typed contract
- efficient data serialization
- one size fits all - all other languages can be generated from the same proto
- compatibility
Types of gRPC
gRPC now is support 4 kinds of communication between server and client:
- unary - like REST call, each request will return an response.
- streaming
- client streaming - client is sending multiple requests within one call
- server streaming - sever is sending multiple responses within one call
- bidrectional streaming - server and client are sending multiple requests and responses to each other within one call
We will build a simple book service to implement the 4 kinds of gRPC:
- Unary -> CreateBook Service
- ClientStreaming -> UploadImage Service
- ServerStreaming -> RecommendBook Service
- Bidirectional Streaming -> RateBook Service