Before we understand what is gRPC, we need to understand few basic terminologies as below.
Microservices
Microservice are built in different language and encompass a function of your business.
Above diagram signifies bunch of different microservices which can be written in different languages but are interacting with one another.
When these microservices exchange information they need to agree on below things:
- The API to excahnge data
- The data format
- The error patterns and many others
One of the most popular choice of building an API is REST/ HTTP-JSON
Things to be kept in mind while building an API
- Need to think about the data model
– JSON
– XML
– Something Binary - Need to think about the endpoint
– GET /api/v1/user/123/post/456
– POST /api/v1/user/123/post - Need to think how to invoke it and handle errors
– API
– Error - Need to think about efficiency of the API – how much data will I get from a call
- And many other things
So don’t you wish you could leave the boring and hard stuff to the framework ? That framework is gRPC
What is an API from the core ?
- API is a contract saying:
- Send me this REQUEST(CLIENT)
- I will send you this response(SERVER)
- It’s all about the data
gRPC
- gRPC is a free open source framework developed by Google
- At high level, it allows you to define REQUEST and RESPONSE for RPC(Remote Procedure Call) and handles the rest for you
- On top of it, it’s fast and efficient, build on top of HTTP/2, low latency, supports streaming, language independent, and makes super easy to plug in authentication, load balancing, logging and monitoring
RPC(Remote Procedure Call)
- In your client code, it looks like you are calling a function directly on the server
How to get started with gPRC
- At the core of gRPC, you need to define the messages and services using Protocol Buffers
- The rest of the gRPC code will be generated for you and you will have to provide the implementation for it
- One .proto file works for 12 programming languages and allows you to use a framework that scales to millions of RPC per second
API in gRPC
The arrows signifies the request/response. and the dotted square represents the no. of request/response.
All the above are self explanatory except bi directional. In bi directional streaming the client may send 2 request and receive one response from server. Then send 1 more request and receives 2 responses.
Scalability in gRPC
- gRPC servers are asynchronous by default
- This means they do not block threads on request
- Therefore each gRPC server can serve millions of request in parallel
- gRPC clients can be asynchronous or synchronous(blocking)
- The client decides which model works best for the performance needs
- gRPC clients can perform client side load balancing
gRPC vs REST