Introduction to gRPC

Introduction to gRPC

Before we get into gRPC, let's first understand what RPC is.

RPC stands for Remote Procedure Call. It is a form of communication in which systems use function calls to communicate over a network. It is an alternative to Representational State Transfer (REST). The most significant difference between REST and RPC is that REST is resource-centric while RPC is great for actions.

rpc diagram

But, what does 'g' in gRPC stands for? Many people think it's for Google as gRPC is made by Google but it actually changes with every version. Here's the list of the meaning of 'g' in each version.

What is gRPC?

index

gRPC is a framework used for building high-performant RPC-style APIs which can be used to communicate between multiple services efficiently.

gRPC can use protocol buffers(or protobufs) as the IDL (Interface Definition Language) which basically means a contract on functions and their data types. Protobuf is a message format in which structured data can be serialized to send over wire or store on disk. Unlike JSON, the data needs to be typed but it is way more space efficient and is the preferred choice for performant systems. Here is an example of Person data type in proto:

message Person {
    required string name = 1;
    required int32 age = 2;
    required string email = 3;
}

gRPC is built on top of HTTP/2 which is very efficient because it is a binary protocol. Also, HTTP/2 allows long-lived connections which enables gRPC to support streaming. It has 4 types of APIs:

  • Unary RPC: In this, the client sends a single request and the server returns a single response.

  • Server Streaming RPC: In this, the client sends a single request and the server returns a stream of response. It can be useful when the client is requesting a lot of data from the server.

  • Client Streaming RPC: In this, the client sends a stream of messages as a request and the server returns a single response. It can be useful when the client is sending a large chunk or media to the server.

  • Bidirectional streaming RPC: In this, the client calls the method and the server can choose to send back a stream of messages or wait for the client to send streaming messages. Basically, both the client and server can send messages to each other.

Architecture

In gRPC, every client service has a stub (auto-generated) which includes all data types, interfaces and functions. When client calls a function from stub, the request parameters are first serialized into protobufs and then sent to the server using HTTP/2 protocol. When the server receives the request, it is deserialized so it can be read by the server. The response is again first serialized and send back to client service. Now, the stub at client service deserializes the response so it can be used by the service.

Why should you use gRPC?

gRPC has many advantages over traditional APIs.

  • It is very performant and uses minimal bandwidth. It has low latency and is highly scalable which is ideal for distributed systems.

  • It supports all popular languages. Here's a list of all supported languages.

  • Code generation is always a good thing as it reduces the surface area for bugs.

Why not use it everywhere?

There are a few use cases where using gRPC may not be ideal.

  • If your language is not supported then you can't do much. Or if your customers are using a language that is not on the list then you have to look for something else. One thing you can do is to use a reverse proxy like grpc-gateway in front of your gRPC service which basically converts gRPC to RESTful endpoints. But this solution may not be ideal in the long run.

  • gRPC does not work natively on the browser. It is because browser APIs do not provide enough fine-grained control over HTTP/2 requests. But we can use grpc-web which allows browser clients to use gRPC. But again, this may not be ideal as it does not support Client-side and Bi-directional streaming.

    Wrapping up

    I hope this article was helpful and you learned something new. gRPC is an excellent choice if you are building a performant and low-latency system. There is a lot more to know about gRPC and you can read more about it from its official documentation.

    Thanks for reading!