- gRPC is free and open source framework developed by google.
- At a high level, gRPC allows us to define REQUEST & RESPONSE for RPC (Remote Procedure Calls) and handle all the rest for us.
- On top of it, a) it's modern, fast and efficient b) build on a top of HTTP/2 c) low latency, supports streaming, language independent and makes it super easy to plug in authentication, load balancing, logging & monitoring.
- At the core of gRPC we need to define messages and services using Protocol Buffers (A Single proto file works over multiple programming language (including client & server) and allow to use a framework that scales to millions of RPC per second)
- Unary (like traditional API)
- Server Streaming
- Client Streaming
- Bi Directional Streaming
- protocol buffers are language agnostic (Code can be generated for pretty much any language)
- Data is binary and efficienty serialized (small paylaod)
- Very convenient for transporting lot of data
- Protocol buffers are used to define messages (data, req, res) and services (service name and rpc endpoints). We then generate code from it
- protocol buffers defines rules to make API evolve without breaking existing client which is useful in microservices
- size of protocol buffer is lesser than JSON hence we save lot of network bandwidth
- Parsing JSON is CPU intensive since format is human readable and not machine readable. While protobufs are in binary format making it close how machine represent data
- HTTP/1.1 opens new tcp connection to server at each request. It doesn't compress header and it's basically request/response format (no server push)
e.g: If average webpage loads 50 assets, then http/1.1 makes 50 calls with plain text header with each request. This increase latency and network packet size
- HTTP/2 supports multiplexing (this reduce latency). It also allows server push (Server can push stream for one request from client). It allow header compressions HTTP/2 is binary and by default SSL enabled (Secured)
-
gRPC - Smaller, faster HTTP/2 Bi Directional, Async, Unary Stream support Code generation through protobufs - 1st Class citizen It is API oriented
-
REST - text based, Bigger in size, Slower HTTP/1.1 Only Client to Server No stream support. Only request/response Code generation through open API/Swagger - 2nd Class citizen It is resource oriented (GET/POST/PUT/DELETE)
- Install golang from
https://golang.org/doc/install
or using homebrew (on mac) - Make sure go binaries are on path. Check version using
go version
- In order to perform code generation, we need to install
protoc
on system. Usebrew install protobuf
for mac - Clone/Fork the Repo & run shell command
sh generate.sh
from root of the project - Run
go run server.go
from calc_server folder & rungo run client.go
from calc_client folder