gRPC

In my previous posts, I wrote about some async and sync communication. If you want to understand comprehensively communication types, I strongly suggest having a look at them which are given below.Async communicationApache Kafka with JavaRabbitMQ with …


This content originally appeared on Level Up Coding - Medium and was authored by Ibrahim Ates

In my previous posts, I wrote about some async and sync communication. If you want to understand comprehensively communication types, I strongly suggest having a look at them which are given below.

Async communication

Sync communication

Today, I’d like to explain another synchronization method gRPC(It could work asynchronously as well).

It was developed by Google and it is an RPC(Remote Procedure Call) framework. Today, I would like to provide information about gRPC to understand it better. I’ll try to find answers to some questions for gRPC. However, if you want to deep dive, there are excellent reference links at the bottom of the page.

RPC(Remote Procedure Call)

For understanding gRPC better, we should understand RPC first. Remote Procedure Call is a technique for building distributed systems. Basically, it allows a program on one machine to call a subroutine on another machine without knowing that it is remote. RPC is not a transport protocol: rather, it is a method of using existing communications features in a transparent way.

Image source: IBM

What is gRPC?

gRPC is an environment-independently modern open-source high-performance Remote Procedure Call (RPC) framework. It allows the client and server applications to communicate transparently and develop connected systems. This framework uses HTTP/2, protocol buffers, and other modern technology stacks to ensure maximum API security, performance, and scalability. Many leading tech firms use gRPC, such as Google, Netflix, Square and so on.

gRPC Concepts

gRPC shines because of its development and success with the use of the leading technologies that perform better than JSON and XML and provide increased API security. Some features of gRPC are as follows.

Protocol Buffers(ProtoBuf)

Protocol buffers, or Protobuf, is Google’s serialization/deserialization protocol. It is used for data exchange between the client and remote services. gRPC uses this protocol as their Interface Definition Language (IDL) and serialization toolset.

Image Source: https://www.wallarm.com

The Protobuf offers some more benefits over JSON and XML. Parsing with Protobuf requires fewer CPU resources since data is converted into a binary format, and encoded messages are lighter in size.

Streaming

Streaming is another key concept of gRPC, where many processes can take place in a single request. The multiplexing capability (sending multiple responses or receiving multiple requests together over a single TCP connection) of HTTP/2 makes it possible. Here are the main types of streaming:

  • Unary (no streaming)
  • Client-to-server streaming
  • Server-to-client streaming
  • Bi-directional streaming

HTTP/2

gRPC is developed on HTTP/2, which was published in 2015 to overcome the HTTP/1.1 limitations. While it is compatible with HTTP/1.1, HTTP/2 brings many advanced capabilities, such as:

  • Binary Framing Layer: Unlike HTTP/1.1, HTTP/2 request/response is divided into small messages and framed in binary format, making message transmission efficient. With binary framing, the HTTP/2 protocol has made request/response multiplexing possible without blocking network resources.
  • Streaming: Bidirectional full-duplex streaming in which the client can request and the server can respond simultaneously.
  • Flow Control: The flow control mechanism is used in HTTP/2, enabling detailed control of memory used to buffer in-flight messages.
  • Header Compression: Everything in HTTP/2, including headers, is encoded before sending, significantly improving overall performance. Using the HPACK compression method, HTTP/2 only shares values different from the previous HTTP header packets.
  • Processing: With HTTP/2, gRPC supports both synchronous and asynchronous processing, which can be used to perform different types of interaction and streaming RPCs.

All these features of HTTP/2 enable gRPC to use fewer resources, resulting in reduced response times between apps and services running in the cloud and longer battery life for a client running mobile devices.‍

Strengths of gRPC

gRPC provides a new take on the old RPC design method by offering many benefits in certain operations. Some of the gRPC strengths which have been increasing its adoption are as follows:

  1. Performance

gRPC offers up to 10x faster performance and API security than REST+JSON communication as it uses Protobuf and HTTP/2.

2. Interoperability

gRPC tools and libraries are designed to work with multiple platforms and programming languages, including Java, JavaScript, Go, C#, and more. Due to the Protobuf binary wire format and efficient code generation for virtually all platforms, programmers can develop performant applications while still using full cross-platform support.

3. Security

gRPC ensures API security using HTTP/2 over the TLS end-to-end encryption connection. gRPC encourages the use of SSL/TLS to authenticate and encrypts data exchanged between the client and server.

4. Usability and Productivity

As gRPC is an all-in-one RPC solution, it works seamlessly across various languages and platforms. Additionally, it features excellent tooling, with much of the required boilerplate code generated automatically. This saves considerable time and enables developers to focus more on business logic.‍

Weaknesses of gRPC

As with every other technology, gRPC also has the following downsides that you need to be aware of when choosing it for developing applications.

  1. Lack Browser Support

As gRPC heavily uses HTTP/2, it is impossible to call a gRPC service from a web browser directly at the moment. No modern browser provides the control needed over web requests to support a gRPC client.

2. Non-human Readable Format

Protobuf compresses gRPC messages into a non-human readable format. This compiler needs the message’s interface description in the file to deserialize correctly. So, developers need additional tools like the gRPC command-line tool to analyze Protobuf payloads on the wire, write manual requests, and perform debugging.

3. No Caching

While HTTP supports mediators for edge caching, gRPC calls use the POST method, which is a threat to API security. The responses can’t be cached through intermediaries. Moreover, the gRPC specification doesn’t make any provisions and even indicates the wish for cache semantics between server and client.

4. Steep Learning Curve

Learning gRPC is slightly more challenging rather than REST; Getting familiar with Protobuf, and looking for tools to deal with HTTP/2 take a bit of time. It is a common reason why users prefer to rely on REST for as long as possible.

When to use gRPC

What are the conditions for using gRPC instead of REST?

  • Real-time communication services where you deal with streaming calls
  • When efficient communication is a goal
  • In multi-language environments
  • For internal APIs where you don’t have to force technology choices on clients
  • New builds as part of transforming the existing RPC API might not be worth it

Microservices with gRPC

In one of my previous companies, we were using gRPC between our internal microservices. We preferred it because of two things: its high performance and its polyglot nature.

One of the greatest strengths of microservices is the ability to use different technologies for each independent service. Microservices must agree on the API to exchange data, data format, error patterns, load balancing, and more. gRPC allows for describing a service contract in a binary format, programmers can have a standard way to specify those contracts, independent of any language, that ensures interoperability.

Our architecture seemed as follows;

Using gRPC between GW and Microservices

Thanks for reading.

Hope it helps.

See you in the next posts…

References:

Remote Procedure Call

https://www.w3.org/History/1992/nfs_dxcern_mirror/rpc/doc/Introduction/WhatIs.html


gRPC was originally published in Level Up Coding on Medium, where people are continuing the conversation by highlighting and responding to this story.


This content originally appeared on Level Up Coding - Medium and was authored by Ibrahim Ates


Print Share Comment Cite Upload Translate Updates
APA

Ibrahim Ates | Sciencx (2022-09-12T03:27:24+00:00) gRPC. Retrieved from https://www.scien.cx/2022/09/12/grpc/

MLA
" » gRPC." Ibrahim Ates | Sciencx - Monday September 12, 2022, https://www.scien.cx/2022/09/12/grpc/
HARVARD
Ibrahim Ates | Sciencx Monday September 12, 2022 » gRPC., viewed ,<https://www.scien.cx/2022/09/12/grpc/>
VANCOUVER
Ibrahim Ates | Sciencx - » gRPC. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2022/09/12/grpc/
CHICAGO
" » gRPC." Ibrahim Ates | Sciencx - Accessed . https://www.scien.cx/2022/09/12/grpc/
IEEE
" » gRPC." Ibrahim Ates | Sciencx [Online]. Available: https://www.scien.cx/2022/09/12/grpc/. [Accessed: ]
rf:citation
» gRPC | Ibrahim Ates | Sciencx | https://www.scien.cx/2022/09/12/grpc/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.