This article is an introduction to gRPC (Remote Procedure Call) which is an open source framework developed by Google. It helps in connecting polyglot services based on service-contract or API definition by using protobuf files. It supports creating service contracts using IDL(Interface Definition Language) using .proto files that provides service definition with methods, that can be invoked by remote services having data structure of request and response. Its best for inter-microservice communication.
gRPC vs REST:
- gRPC is an ideal choice for inter-service communications as it uses protocol buffers i.e the binary data interchange format, which is highly optimized and is faster compared to JSON used in REST.
- gRPC uses HTTP 2.0 which is secured, faster, low-latency as compared to HTTP 1.0 used in REST.
- gRPC automatically generates client stubs in several languages to avoid creating and maintaining the client libraries, whereas in REST it needs to be created manually.
- gRPC provides inbuilt support of selective message compression, which is not possible in REST. For Ex: In a message combination of text and images, the streaming gRPC can decide to compress only text and it can avoid compressing images to avoid CPU usage.
- gRPC endpoints can be exposed as REST using grpc-gateway.
- REST API’s have been used to expose directly to consumers based on human understandable text-messaging( JSON, XML over HTTP), but not ideal for internal service-to-service communications.
4 Types of gRPC Service Methods :
- Unary RPC
- Client Streaming RPC
- Server Streaming RPC
- Bidirectional Streaming RPC
Unary RPC: It is similar to traditional REST API, where the client sends one request and server responds with one response.
Client Streaming RPC: It is similar to Unary RPC except the client send streams of messages and then server responds with one message.
Server Streaming RPC: It is similar to unary RPC except the server sends streams of messages for a message sent by the client.
Bidirectional Streaming RPC: The call is invoked by the client by sending streams of messages to the server, and the server sends streams of messages to the client. Both the streams are independent, the client and server can send messages in any order.
This was just an introduction to gRPC. We will see the Implementation of the gRPC in java and golang in the next article.
Please leave your comments on the topics you would like to know more about.