Monday, November 14, 2022

gRPC

About gRPC 

gRPC is an open source remote procedure call (RPC) system initially developed at Google. It uses HTTP/2 for transport, Protocol Buffers as the interface description language, and provides features such as authentication, bidirectional streaming and flow control, blocking or nonblocking bindings, and cancellation and timeouts.

This would look similar to websockets but underlying difference is it works on HTTP2 protocol and the data format for request response would be bound to Protobuf, cannot use JSON or XML. But protobuf is more compact and light weight than the latter. The connection would be persistent and client can invoke the methods in remote server through the connection as needed. It offers 4 types of method call, traditional request/response model, server-side streaming, client side streaming and bi-directional streaming.

What are protocol buffers?
Protocol buffers are mechanism for serializing structured data – think XML, but smaller, faster, and simpler. You define how you want your data to be structured once, then you can use special generated source code to easily write and read your structured data to and from a variety of data streams and using a variety of languages.


Notes: we can use gRPC from different languages  like C#, Dart, Java, Node, PHP, Python,...


Http2 vs Http1.1



Http 1.1 only supports Request/Response pattern, and not supports compress headers, create new TCP connection per request. So, if we visit a page that contains one image and one CSS, this means create 3 TCP connections!


Http 2.0. one TCP connection will use for multiple Requests/Responses, Supports Server Push, headers and data are both compressed to binary data (less bandwidth), support send multiple messages at the same time. SSL will be required by default.  


How to Enable http 2.0 on IIS?
IIS running on Windows 10 or Windows Server 2016 supports HTTP/2 by default, but the connection should be https.
you shouldn't need to change anything in your application for HTTP/2 to work.

here is how to install IIS and enable local SSL for testing...







How to validate that current connection using http2.0 ?

Launch your browser from your Windows 10 or Windows Server 2016 machine and hit F12, (or go to Settings and enable F12 Developer Tools), and then switch to the Network tab. Browse to https://localhost and voila, you are on HTTP/2!

if "Protocol" is not exists, then right click > Header Options > Protocol




Types of gRPC APIs



1. Unary
It is a classic request-response API. This is what everyone is using mostly as REST APIs. The Client sends a request and the server sends a response to that request.

2. Server Streaming
In this case, the client will send a request to the server and the server will keep sending data like a stream.

3. Client Streaming
It is a bit opposite to Server Streaming. Here client will send a stream of requests and expects a single response. The server will send a single response. May be after the end of all the requests or in the middle, it depends on the implementation.

4. Bi-Directional Streaming
It is a kind of combination of both Server Streaming and Client Streaming in the sense that both server and client will send a stream of requests and responses. The client will initiate a connection and start streaming messages in the request and the server will start streaming the response to the client.







gRPC Scalability 
Server : Async
Client: sync/ Async


gRPC Performance

let us compare Data Streaming via GRPC vs MQTT vs Websockets, which is better?


Above results clearly shows that GRPC wins because of persistent connection and protobuf data format, which is lightweight.

Conclusion
Out of 3 options, it depends on individual requirements to choose one. If it is collecting data from sensors and IoT device the choice would always be MQTT. But if data streaming is between devices which doesn't have resource constraints GRPC and websockets can be an options. For my requirement GRPC is winner.