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.
-
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
-
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 |
-
Simple client server communication
-
Client invokes a function on the server and gets back response
-
Client invokes a method on server
-
Get a stream of response back
-
Client reads the response after server has finish sending
-
Client sends a sequence of messages to server
-
Sever waits for the client to finish sending all the message
-
After which it prepares response
-
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
-
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
-
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
- Easy to create and perform actions on server
-
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
-
A open format developed by google for serializing structured data
-
Messages are converted into binary format when serialized with a
.proto
file using theprotoc
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
Source : https://grpc.io/docs/what-is-grpc/introduction
-
Fast and space efficient message passing
-
Only one library for any language
-
Allows native upload feedback
-
Cancelling request functionality
-
Use of protocol Buffers
-
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