What’s gRPC?

gRPC is a modern open-source, high-performance RPC system that can run in any environment. This will easily link facilities in and around data centers with plug-in support for load balancing, logging, safety checks, and authentication. It is also important in the last mile of distributed computing for linking computers, smartphone apps, and browsers to backend services. The great advantages of RPC, Protocol Buffer, HTTP / 2 will be wrapped in gRPC.

  • An RPC platform developed by Google to optimize and accelerate communication between services in a microservice architecture.
  • GRPC uses Protocol buffer to serialize payload data with binary and small size, while REST uses JSON with text and large size.

gRPC - remote procedure call

gRPC code example:

gRPC code sample

To begin, we will first understand the concepts of Protocol buffers and HTTP / 2.

http1-http2

Remote Procedure Call

RPC (Remote Procedure Call), built with the idea of simplifying communication between services instead of RESTful API-style communication, now simply calling functions like objects talking to each other, and dispersing services is a story of future development that does not involve coding.

Protocol buffer

Protocol buffers are a flexible, efficient, automated mechanism for serializing structured data – think XML, but smaller, faster, and simpler. You decide how to organize your data, and then you can use a special generated source code to conveniently write and read the structured data to and from a variety of data sources and to use a variety of languages. You can even update your data structure without breaking programs against an “old” format.

HTTP/2

HTTP / 2 is an upgraded version of HTTP / 1.1, HTTP / 2 was born with the purpose of improving communication speed between client/server on the Web platform. These changes help to maintain interoperability and compatibility with HTTP 1.1. The advantages of HTTP/2 are expected to increase over time based on real-world experiments.

HTTP/2 Feature Upgrades

Request Multiplexing:

HTTP/2 sents data packages from multiple streams are essentially mixed and transmitted over a single TCP connection. These packages are then split at the receiving end and presented as individual data streams. With this method, HTTP/2 can prevent bottlenecks network performance.

Request multiplexing

HTTP/2 Server Push

This capability allows the server to send additional cacheable information to the client without requesting but anticipating future requests. For example, if the client requests for the resource X and but the resource Y as the requested file, the server can choose to push Y along with X instead of waiting for an appropriate client request.
This is another way to optimize the loading speed of the website, instead of having to request from the client, the server returns the resource, HTTP / 2 will push the resource back to the client without the client sending the request.

Server Push - gRPC

Binary Protocols

Binary Protocols which mean Browsers using HTTP/2 implementation will convert the same text commands into binary before transmitting it over the network.

Binary Protocols

Stateful Header Compression

hPACK

Obviously, any HTTP request carries a ton of data headers, so even though it’s the same from the second request, HTTP/2 optimizes this bit, HTTP/2 eliminates unnecessary data headers in the second request so compresses them until submitting.
HTTP/2 implementation addresses these concerns with the ability to compress a large number of redundant header frames. It uses the HPACK specification as a simple and secure approach to header compression. Both client and server maintain a list of headers used in previous client-server requests.

Compare gRPC vs REST:

gRPC comparison

The similarity between gRPC and REST: Both use TLS / SSL for security.

Difference between gRPC and REST:

Performance

gRPC uses HTTP / 2, Rest uses HTTP / 1, HTTP / 2 protocol will be faster than HTTP / 1.1 (currently, I can turn on HTTP / 2 on Rest)
gRPC uses Protocol buffer to serialize payload data with binary and small, while RESt uses JSON with text and large.

Code Generation

Code generate is built into gRPC, with the help of Protocol buffer compiler. While REST must have 3nd tool like OPEN API and swagger.

Streaming

2-way communication in gRPC, REST only 1-way communication from client-server.

Strict specification

The API in gRPC is very strict and requires an explicit definition in the proto file. While REST is often lax and optional, OPEN API can be defined if desired, but it is not required.

Therefore, gRPC is better than REST for most of the issues mentioned above.

However, one thing that REST is still better about, is browser support. REST is fully supported by all browsers. However, support for gRPC is limited. It requires gRPC-web with a proxy layer to switch between HTTP / 1 and HTTP / 2.

For conclusion

The aforementioned article provides you detailed knowledge about the gRPC, from which you will read more about the new API technologies as well as decide and determine whether or not to add the gRPC to your next project.

Article by Tinh Tran