What is gRPC?

gRPC is a framework to efficiently connect services and build distributed systems. Initially designed by Google, it is now an open-source project promoting the Remote Procedure Call (RPC) model for communication between services. It is focused on high performance and uses the HTTP/2 protocol to transport binary messages. It also relies on the Protocol Buffers language to define service contracts. Protocol Buffers, also known as Protobuf, allow you to define the interface to be used in service to service communication independently from the programming language. Many tools for the most common programming languages are available to translate these Protobuf interfaces into code.

The gRPC framework allows developers to create services that can communicate with each other efficiently and independently from their preferred programming language. Once you define a contract with Protobuf, this contract can be used by each service to automatically generate the code that sets up the communication infrastructure. This aspect simplifies the creation of services interaction and, together with the high performance, makes gRPC the ideal framework to create microservices.

 

gRPC-2

 

gRPC Different Types

gRPC-2

 

gRPC comes with four different types of RPC:

 

  • Unary RPC: this is the simplest form of RPC. In this case, the client sends a request message to the server and receives a response.
  • Server streaming RPC: in this scenario, the client sends a request message to the server and receives a sequence of responses.
  • Client streaming RPC: this is the case where the client sends a sequence of messages and receives a single response from the server.
  • Bidirectional streaming RPC: in this case, the client and the server exchange messages in both directions. This one is the most complex because client and server will keep sending and receiving multiple messages in parallel and with arbitrary order. It’s very flexible and no blocking, which means no sides need to wait for the response before sending the next messages.

Understanding gRPC APIs

As a variant of the RPC architecture, gRPC was created by Google to speed up data transmission between microservices and other systems that need to interact with each other. Compared to REST APIs, gRPC APIs are unique in the following ways: 
Protobuf Instead of JSON
Built on HTTP 2 Instead of HTTP 1.1
In-Born Code Generation Instead of Using Third-Party Tools Like Swagger
7 to 10 times Faster Message Transmission
Slower Implementation than REST 

gRPC API vs Rest API

Here’s a simple matrix that compares the basics of REST APIs and gRPC:

 

Characteristic gRPC REST API
HTTP Protocol HTTP 2 HTTP 1.1
Messaging Format Protobuf (Protocol Buffers) JSON (Common), also 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


When to Use REST APIs

REST APIs are the most widely-used and popular APIs for connecting microservices-based infrastructures. Whether you are building an internal system or an open system that exposes its resources to the rest of the world, REST APIs are likely to remain the de facto choice for app integration for a very long time. Also, REST APIs are ideal when a system needs high-speed iteration and standardization of HTTP protocol. 
With its universal support from third-party tools, REST APIs should be your first consideration for app integrations, microservices integrations, and web services development. 

When to Use gRPC APIs

As for gRPC, most third-party tools continue to lack in-built features for gRPC compatibility. As such, gRPC is mostly relegated to building internal systems, i.e., infrastructures that are closed to external users. With that caveat in mind, gRPC APIs could be useful for the following circumstances: 
Microservices connections: gRPC’s low-latency and high-speed throughput communication make it particularly useful for connecting architectures that consist of lightweight microservices where the efficiency of message transmission is paramount.
Multi-language systems: With its native code generation support for a wide range of development languages, gRPC is excellent when managing connections within a polyglot environment. 
Real-time streaming: When real-time communication is a requirement, gRPC’s ability to manage bidirectional streaming allows your system to send and receive messages in real-time without waiting for Unary client-response communication. 
Low-power low-bandwidth networks: gRPC’s use of serialized Protobuf messages offers light-weight messaging, greater efficiency, and speed for bandwidth-constrained, low-power networks (especially when compared to JSON). IoT would be an example of this kind of network that could benefit from gRPC APIs.

gRPC and .NET Core

Before the .NET Core 3.0 release, you could use gRPC in your C# applications by using the Grpc.Core library. It basically is a C# wrapper around the gRPC Core library written in C. It wasn't playing very well with Kestrel and other managed code libraries like HttpClient. You also didn't have any project template helping you to quickly set up your application with gRPC support.
Starting from .NET Core 3.0, you can leverage the grpc-dotnet library, entirely written in managed code and well-integrated with the .NET platform. It also provides you with a specific project template that helps you to set up your application very quickly.

إضافة تعليق جديد

This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.