Is gRPC a better choice over REST?2022-02-04
Communication protocols play essential role in every modern software system. With increasing popularity of Microservice architecture inter-process communication techniques become even more important.
The most common way to build request-response style communication is RESTful services. However, REST can be inefficient and error-prone to use in microservices. We would need something more scalable, loosely coupled and efficient. That is where gRPC will shine.
gRPC (the “g” stands for something different in every gRPC release) is an inter-process communication technology that allows you to connect, invoke, operate, and debug distributed heterogeneous applications as easily as making a local function call.
To develop gRPC application you need to create a service interface using IDL (Interface Definition Language). From this service interface you can generate server-side (server skeleton) and client-side (client stub) code with protocol buffer.
Protocol buffer is a language-agnostic, platform-neutral, extensible mechanism to serializing structured data.
Why would you choose gRPC over REST or any other communication protocol?
Advantages of gRPC
Efficiency. gRPC is one of the most efficient inter-process communication technologies because it uses a protocol buffer (binary based protocol) instead of a textual format such as JSON or XML. What makes it even faster, that is implementation of protocol buffers on top of HTTP/2.
Simplicity of schema and service interfaces. gRPC offers a simple but consistent, reliable, and scalable application development experience compared to using OpenAPI/Swagger for RESTful service.
Strongly typed. gRPC service contracts clearly define the types that you will be using for communication between the applications, which leads to stability of distributed application development.
Language-agnostic. gRPC is designed to work with different programming languages.
Duplex streaming. It has native support for client- or server-side streaming. This makes it much easier to develop streaming services or streaming clients.
Built-in features. gRPC offers built-in support for features such as authentication, encryption, resiliency (deadlines and timeouts), metadata exchange, compression, load balancing, service discovery.
Integration with cloud native ecosystems. gRPC is part of the CNCF(Cloud Native Computing Foundation) and most of the modern frameworks and technologies offer native support for gRPC out of the box.
Widely used. gRPC has been tested and adopted by Google, Square, Lyft, Netflix, Docker, Cisco, and CoreOS.
Disadvantages of gRPC
Heavy gRPC service definition changes will require regeneration of the code for both client and server. Smaller changes can be done without breaking the service contract by using different versions of a proto.
flexibility of the external-facing services can suffer, as consumers get less control. The gRPC gateway is designed as a workaround to overcome this issue.
So, the question is why don’t you use gRPC yet? I am going to. Coming soon … example of gRPC application.