gRPC — Introduction

Aksshar Ramesh
4 min readFeb 4, 2021

In the REST based Microservices architecture, what we do is that let’s say there is a book service. we will be having a REST controller for the books by using various methods like GET,POST,PUT and DELETE. We do certain actions on those resources. This is how we design things. But gRPC is more of an action oriented and instead of resource oriented. gRPC is a High-Performance open source RPC framework developed at Google. Here we use the proto files to create models for communication.

Similarly, we also define the service or an interface for the list of services we are going to expose using a proto file. Using the protoc tool what we will be doing is that we will be generating an abstract class or interface, which needs to be implemented by the server. the same protoc tool, will also create the client library or stub in most of the languages you know. the client will be using that client library generated by the protoc tool to interact with the server. this is what gRPC is.

The claim library generated by the protoc tool provides convenient methods to make the call to be synchronous or asynchronous. If you need to wait for the response, you will be using the blocking or the synchronous API. You can also register a call back and move on if you need an non-blocking behavior. Those choice under this completely up to that client. Let’s see the types of gRPC.

RPC — Types

Unary

There are four different types of RPC. The first one is Unary, here we make that traditional request and get the response. One request and one response. That RPC is considered to be completed once it receives the response. This is a very basic one to one request and response model.

Server-streaming

Let’s take look at the Server-streaming. Some times the client sends one request and the server sends multiple responses. This can be seen as one to many request and response model. The streaming is an ordered sequence of messages. Usually this behavior is good when you think that the receiving side cannot be able to handle all the responses at once. So instead of sending everything at once we are sending responses at chunks. Pagination is a good example for this.

Client-streaming

Next we have Client-streaming, the Client-streaming same like Server-streaming but here the client sends multiple streaming requests for the server which send back only one response. An example could be file upload. The client that might be upload a huge file, instead of sending the big file sized in .GB’s , it might be sending the file in small chunks. Once the file uploading is finished ,the server sends a response saying your file upload is completed.

Bidirectional-streaming

Then we have Bidirectional-streaming. Streaming at both sides. Combination of Server-streaming and Client-streaming. When we have Bidirectional-streaming, they completely independent. The Bidirectional-streaming is very helpful when you have to finish a task when the server and the client have to coordinate and work together in more than a interactive way to finish a task.

If your interested more about to learn about protobuf, refer my previous articles about Protocol Buffers and Json vs Proto -Performance Comparison

--

--

Aksshar Ramesh

AI Security Research fellow at Centre for Sustainable Cyber Security, University of Greenwich