/intro-to-rpc

This repository was made to document everything i have learned about rpc and grpc and provide implementation

Primary LanguagePHP

Intro to RPC

RPC is mechanism that allows distributed systems to communicate. This repository gives a brief overview about RPC works under the hood and how to implement gRPC framework with a simple example application.

What is RPC (Remote Procedure Call) ?

  • A mechanism for client server communication

  • A local function when called executes some code within its own address space

  • A remote procedure call gets executed but in a different machine or address space

  • The function call will seem to user as if it is calling a local function

  • The implementation detail for both server and client will be same




What is gRPC ?

grpc-logo


  • An open source framework released by google in 2015

  • Built on HTTP 2.0 and uses protocol buffers

  • A rewrite of their internal rpc implementation

  • A language agnostic and operating system agnostic framework

  • Used by large companies for communicating between their various microservices






  • Supported in a wide array of programming languages

C++

Ruby

C#

GO

PHP

Python

Java

Node JS




Types of API Implementation through gRPC

Unary RPC

  • Simple client server communication

  • Client invokes a function on the server and gets back response




Sever Streaming

  • Client invokes a method on server

  • Get a stream of response back

  • Client reads the response after server has finish sending




Client Streaming

  • Client sends a sequence of messages to server

  • Sever waits for the client to finish sending all the message

  • After which it prepares response




Bi-directional

  • Allows both ways

  • Either client can send a stream of message and server waits

  • Or server sends a stream of response and client waits

  • Or client and server pass message and get response in a ping pong manner





Motivation

Microservice communication

  • In a distributed system using each service can be written in different programming language

  • Each service needs to have an HTTP Client implementation if using REST for communication




  • Maintaining HTTP clients for different programming language are difficult

  • gRPC provides a single library for all the clients

  • Maintenance by the open source community for each language in a single unified repo

  • No need to create or maintain your own http client library

HTTP 2 Protocol

  • Use of http 2 protocol as a hidden implementation brings a lot of benefits

  • Since it is hidden any updates to the protocol does not effect the client or server application

Performing Actions

  • Easy to create and perform actions on server

Use of Protocol Buffers

  • gRPC uses Protocol Buffer as a message serialization and deserialization format

  • RPC comes in other format as well suc as

    • HTTP RPC
    • JSON RPC
  • gRPC allows faster serialization and deserialization than JSON

  • Language agnostic

Why use Protocol Buffers instead of JSON ?




  • A open format developed by google for serializing structured data

  • Messages are converted into binary format when serialized with a .proto file using the protoc compiler

  • Allows faster message passing as it converts it into binary

  • Language and platform agnostic format

  • Data structure in this format are staticly typed

  • The services defined when using gRPC to be used by client and server are written in .proto file

  • It has the following structure

// The greeter service definition.
service Greeter {
  // Sends a greeting
  rpc SayHello (HelloRequest) returns (HelloReply) {}
}

// The request message containing the user's name.
message HelloRequest {
  string name = 1;
}

// The response message containing the greetings
message HelloReply {
  string message = 1;
}
  • Each data that will be sent or received are placed in a message

  • Each message has a strongly typed property that defines the type of data it takes and will receive

  • Defining a RPC service is placed under a service block where the function is defined

  • The function accepts parameter type and return type as well

  • This service definition defines the schema to generate code between server and client

How does RPC(gRPC) actually work ?





Source : https://grpc.io/docs/what-is-grpc/introduction



Pros and Cons

Pros

  • Fast and space efficient message passing

  • Only one library for any language

  • Allows native upload feedback

  • Cancelling request functionality

  • Use of protocol Buffers

Cons

  • Defining the schema can become a hassle

  • Client can become bloated

  • Steep learning curve

  • No proper error handling

  • No proper browser support

  • Handling timeouts can become complicated




Reference and Further Resouces

Examples