Thursday, October 29, 2020

gRPC

Introduction:

In past (Before Web Services & REST) , RPC was a popular inter-process communication, for building client-server application in distributed systems. Main objective of RPC was to make the process of executing code on a remote machine as simple as calling a local function. Well most conventional RPC approach (CORBA) and few more, have drawbacks, such a s high complexity involved, using TCP protocol etc.

gRPC, originally developed by Google, now it's open source, focuses on high performance inter-service communication technology.

gRPC, tries to overcome most of limitations of traditional RPC. It enables communication between services build using heterogeneous technologies. It is based on idea of defining service and specifying methods, that can be called remotely with their parameters and return types.

gRPC By Default, uses Protocol Buffers - Such as IDL (Interface Definition Language) to describe both the service interface and structure of payload messages.
gRPC uses HTTP2 as the Transport Protocol, a key reason for gRPC wide adaption.
  • HTTP2 overcome the problem in Http 1.1, where 
    • Each connection can handle one request at a time. I.e. If the first request is blocked, then the next request has to wait. Therefore require to open multiple connections between client and server to support real uses cases.
    • Many Headers (User-Agent, Cookie) are repeated across multiple request, which is a waste of bandwidth.
  • HTTP2 overcome these problems
    • All communication between client and server happens over a single TCP connection that carry number of bi-direction flows - Concept named as 'Stream', which carries one or more messages, bi-directionally. (See at the last of this post).
    • Avoids Header repetition and introduces 'Header Compression' to optimize used of bandwidth.
    • Also introduced new feature of sending 'Server Push Messages' without using request-response style messages.
Building MS requires lot many things to think over: 

  • Data Formats
  • Error Patterns
  • Think about data model (JSON/XML/Binary)
  • EndPoints
  • Load Balancing
  • Efficiency
  • Latency
  • Interoperability
  • Authentication/Authorization
  • Logging
  • Token Based
  • Scalablity etc etc.

What if there is some framework exists to take care of all such things - There is where gRPC helps.

What is gRPC:
  • It's a free and open-source framework developed by Google.
  • At the core of gRPC, developer need to define the Message (REQUEST and RESPONSE) and Services using Protocol Buffers.
  • Rest of gRPC code will be auto generated and as a developer, only need to provide implementation for it. 
    • I.e. At high level, gRPC allows to define REQUEST and RESPONSE for RPC and handles the rest for you.
  • One .proto file works for over 12 programming languages (Server and Client) - And allows you to use a framework that scales to millions of RPC per seconds
  • As code can be generated in any language 
  • gRPC globally - 
    • Modern, Fast, Efficient, 
    • Built on top of HTTP/HTTP2, 
    • Low latency, 
    • Support streaming, 
    • Language Independent, 
    • Plugged-in authentication, 
    • Load Balancing, 
    • Logging and Monitoring. 

gRPC vs REST:

Features

gRPC

REST

Protocol

HTTP/2 (Fast)

HTTP/1.1 (Slow)

Payload

Protobuf (Binary & Small)

JSON (Text & Large)

API Contract

Strict, Required (.proto)

Loose, Optional (OpenAPI)

Code Generation

Built-In (Protocol)

3rd Party Tools (Swagger)

Security

TLS/SSL

TLS/SSL

Streaming

Bi-Direction

Client to Server (Request Only

Browser Support

Limited (require gRPC-Web)

Yes

 


Where to use gRPC:
  • Microservices
    • Low latency and High Throughput Comminication
    • Strong API Contract
  • Polygot Environments
    • Code generation out of the box for many languages
  • Point to Point Real Time Communication
    • Excellent Support for Bi-Directional Streaming
  • Network Constrained Environments
    • Lightweight Message Format

Types of gRPC:

There are 4 types of gRPC:
  • Unary: The simplest one is Unary, where the client sends 1 single request message and the server replies with 1 single response. This looks somewhat similar to the normal HTTP REST API.
  • Client-Streaming - In this scenario, the client will send a stream of multiple messages or a sequence of messages to server and it expects the server to send back only 1 single response. 
  • Server-Streaming -  Here the client sends only 1 request message, and the server replies with a stream of multiple responses. The client reads from the returned stream until there are no messages.
  • Bidirectional (or bidi) streaming - 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. I.e. Server could wait to receive all client messages before sending response, it could alternately read message and then write a message or follow any other combination of read and writes.






















Happy gRPC.. 

Arun Manglick

Protocol Buffers (a.k.a Protobuf)

Introduction:

  • Google's mature open source mechanism/protocol to enable serialization and deserialization of structured data between different services.  
  • Method of serializing structured data. It is useful in developing programs to communicate with each other over a wire or for storing data. 
    • gRPC By Default, uses Protocol Buffers, such as IDL (Interface Definition Language) to describe both the service interface and structure of payload messages.
    • gRPC uses HTTP2 as the Transport Protocol, a key reason for gRPC wide adaption.
  • Protocol buffers are Google's Language-Neutral, Platform-Neutral, extensible mechanism for serializing structured data – think XML, but smaller, faster, and simpler
    • I.e. “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.”
  • Google’s design goal was to 
    • Create a better method than XML to make systems communicate with each other over a wire or for the storage of data. 3 Times Smaller & 10 Times Faster than XML
    • Better than Json in terms of size.
  • Protocol Buffers are 'Language Agnostic. I.e. Code can be generated in many languages
  • Data exchange is in 'Binary format', lowering payload size lower than anything (XML, JSON) making serialization efficient.
Note: 
  • Facebook uses an equivalent protocol called Apache Thrift and 
  • Microsoft uses Microsoft Bond Protocols in addition to a concrete RPC protocol stack used for defined services known as gRPC.
Few more:
  • Data is 'Fully Typed' 
  • Data is 'Compressed Automatically' (Leading to less CPU usage)
  • Data can be read across 'Any Language' (e.g. C#, Java, Go, Python, JS etc)
  • Huge amount of code Code is generated automatically in any language from a simple .proto file.
  • Payload is Binary, thus very efficient to send/receive on a network and serialize/de-serialize on a CPU.
  • Protocol Buffers define rules to make an API evolve without breaking existing clients, which is helpful.
Protocol Buffers is defined by a .proto text file. (HelloWorld.proto)













Protocol Buffers Over XML & JSON:
  • With XML - 3 Times Smaller & 10 Times Faster than XML
  • With JSON - High CPU Intensive - Parsing JSON is CPU Intensive (Bcoz JSON is human readable)











How do Protocol Buffers and JSON Compare?
Protocol Buffers and JSON messages can be used interchangeably; however, they were designed with different goals in mind.

JSON arose out of a subset of the JavaScript programming language. Its messages are exchanged in text (human-readable) format, are completely independent and supported by almost all programming languages.

Protobuf is not only a message format. It is simultaneously a set of rules and tools that define and exchange the messages. It is currently restricted to only some programming languages. In addition, it has more data types than JSON, such as enumerates and methods, and has other functions, including RPC.

gRPC: At the core of gRPC, messages and services are defined using Protocol Buffers.

Happy Blogging!!

Arun Manglick