Introduction to gRPC
Tags: grpc, microservices, protocol
Published: 2022-02-20 01:00:34

---
title: Introduction to gRPC
published: true
date: 2022-02-18 00:00:00 UTC
tags: grpc, microservices, protocol
canonical_url: https://sahansera.dev/introduction-to-grpc/
cover_image: https://sahansera.dev/static/6b356c712866f2cca4a511648cd19e0d/134f7/introduction-to-grpc.jpg
---

## Intro

If you have built RESTful or other OpenAPI-like APIs for some time and wondering what’s next for you, then you have come to the right place. This article series discusses leveraging gRPC to build your next API, even multiple services. We will initially look at the main concepts from a high-level view and then move on to the implementation aspects of it.

## Motivation

There are many tutorials on getting started there. But the main issue I faced was that they even made me more confused as there was a lot of contexts lost in the process and brought in way too many third-party libraries or explained a bunch of steps without emphasizing how different pieces work together. Therefore, I thought to create a guide for anyone who’s interested in getting started with gRPC from a hands-on perspective.

This article is the first part of a series on gRPC. The links are down below. If you want to jump ahead, please feel free to do so.

- [Introduction to gRPC](https://sahansera.dev/introduction-to-grpc/) (You are here)
- [Building a gRPC server with Go](https://sahansera.dev/building-grpc-server-go/)
- [Building a gRPC client with .NET](https://sahansera.dev/building-grpc-server-dotnet/)
- [Building a gRPC client with Go](https://sahansera.dev/building-grpc-client-go)
- [Building a gRPC client with .NET](https://sahansera.dev/building-grpc-client-dotnet)

## Background

One of the popular choices for building APIs nowadays is creating a RESTful service. However, before even coming to REST API, we need to look back to see other forms we used to have in the past.

- [SOAP](https://en.wikipedia.org/wiki/SOAP) - Popularized back in the late-90s for building service-oriented architectures (SOA) systems which are known for exchanging _bloated_ XMLs. The benefits were detrimental for distributed applications where the schema was rigid.
- [REST](https://en.wikipedia.org/wiki/Representational_state_transfer) - Promoted the resource-oriented architecture (ROA)-style distributed applications. Often bulky with JSONs, and everything that the service provides is represented as resources (Eg: `/api/v1/users/` or `api/v1/books/1234` etc.). Sometimes this could result in exposing too much or too little data at the cost of making multiple HTTP calls.
- [GraphQL](https://en.wikipedia.org/wiki/GraphQL) - GraphQL takes a step further and exposes a single endpoint that you can use to query or mutate the data through HTTP verbs. It’s still a request-response model and based on the text-based transport protocol, HTTP 1.x.

What if I want to have some bi-directional communication? None of the above solved that. Then we got technologies like WebSockets and Server Sent Eventing.

- [WebSockets](https://en.wikipedia.org/wiki/WebSocket) - Built to support bi-directional communication over a single TCP connection. Known to be a very chatty protocol often sending packets back and forth. If you want to know about WebSockets, here is an [article that I wrote](https://sahansera.dev/understanding-websockets-with-aspnetcore-5/).
- [Server Sent Eventing](https://en.wikipedia.org/wiki/Server-sent_events) - Another paradigm where the server sends messages to the client once the initial connection has been set up by the client.

There’s a recurring theme going on in the above technologies. It could be summarized as the messages being (bulky, not strongly typed etc.) and inefficient protocols (text-based such as HTTP 1.x etc.).

## Hello gRPC

gRPC was born to address some of the challenges we face in the above approaches. In 2015 Google released gRPC to the open-source world. The idea behind gRPC is to enable developers to use [RPC](https://en.wikipedia.org/wiki/Remote_procedure_call)-like communications over HTTP/2 while have a single client library. Whoever is maintaining gRPC will maintain the client libraries. Because HTTP/2 works on binary format, gRPC will also abstract away the HTTP/2 stuff from you. The developers only have to define their service contracts through requests, responses and RPC calls, and the gRPC framework will handle the rest for us.

> 💡 Even before we start learning about gRPC, you must have also thought about what the “g” in gRPC mean? Some say it stands for “Good”; others say it stands for “Google”. You know what? It doesn’t matter as it doesn’t provide more context. You can find all the different variations [here](https://github.com/grpc/grpc/blob/master/doc/g_stands_for.md), which is, by the way, hilarious! 😂

![introduction-to-grpc](https://sahansera.dev/static/9d0c125599cba7de0200e654e16530cf/08c0b/introduction-to-grpc-1.png "introduction-to-grpc")

<center style="margin-bottom: 20px"><i>Source: <a href="https://grpc.io/docs/what-is-grpc/introduction/" target="_blank">https://grpc.io/docs/what-is-grpc/introduction/</a></i></center>RPC has been around for some time; however, gRPC approaches RPC in a much cleaner way. Another term that you’d come across when learning gRPC is [inter-process communication](https://en.wikipedia.org/wiki/Inter-process_communication) or IPCs. GRPC is mainly built to cater to inter-process communication that lets you connect, invoke, and operate, an excellent choice for micro-services like applications. In distributed computing realm, inter-process communication or, in short, IPC refers to passing messages (synchronously or asynchronously) where any application or a node can act as both client and a server.
## Protocol Buffers

They are a language-agnostic way to define what your service does. These are commonly known as “IDLs” or [Interface Definition Language](https://en.wikipedia.org/wiki/Interface_description_language)(s).

So the steps are,

1. You write the messages. These messages have statically typed fields.
2. You write your services by defining what comes in, what goes out.
3. Compile the proto files and generate the client libraries for your application.

It’s also worth mentioning that Protocol Buffers are not the only way to define our IDLs. There are other formats like [FlatBuffers](https://google.github.io/flatbuffers/), [Bond](https://github.com/microsoft/bond) etc.

Let’s say we want to model a book shop which can be queried to get a list of books available. This is what a protocol buffer would like:

```protobuf
syntax = "proto3";

message Book {
  string title = 1;
  string author = 2;
  int32 page_count = 3;
  optional string language = 4;
}

message GetBookListRequest {}
message GetBookListResponse { repeated Book books = 1; }

service Inventory {
  rpc GetBookList(GetBookListRequest) returns (GetBookListResponse) {}
}
```

The above is an example that we will use throughout this blog series. Don’t worry if you don’t understand what’s going on here. We will go in-depth in upcoming posts.

![introduction-to-grpc-2](https://sahansera.dev/static/e0537d1ed9e8550e23a6f22864f64f93/5a190/introduction-to-grpc-2.png "introduction-to-grpc-2")

- The first line specifies the Protobuf version we will be using. It will be set to `proto2` if you don’t specify explicitly.
- The Book is a `message` definition with some statically typed fields such as title, author etc.
- `GetBookListRequest` and `GetBookListResponse` are also messages composed of the Book type we defined above.
- Inventory is a `service` that says what methods we expose to the remotely invoked clients.

There are many advantages of using Protobufs compared to something like JSON. Once you write the definitions for your service, you can share them with other teams and use them to generate stubs/code that can interact with your service.

Another advantage is that Protobufs are binary encoded. The payload is smaller than JSON, which means it would be efficient to send. This also means that it will use fewer CPU cycles to serialize/deserialize the messages.

## gRPC Server & Client

Now that we have the Protobuf definitions for our service, we could generate the Server-side and Client-side implementations using the [Protoc compiler](https://grpc.io/docs/protoc-installation/).

![introduction-to-grpc](https://sahansera.dev/static/28b7f79564ae18cd72558ba5a93cde92/5a190/introduction-to-grpc-3.png "introduction-to-grpc")

1. We first create the definition of the service with a `.proto` file
2. We then generate the server-side code in our preferred language (Go, C#, Java etc.). This code includes the boilerplate code to serialize, deserialize, functions for receiving and responding to messages.
3. We then generate the client-side code in our preferred language (doesn’t have to be the same language we chose for the server). This includes methods that we can invoke on our server with additional code to serialize, deserialize messages.
4. Depending on which gRPC mode we choose, the client-server communication happens over an HTTP/2 connection. We will discuss more on these modes in the next section.

## gRPC Modes

There are 4 modes of gRPC communication styles. Following are their brief introductions. Feel free to go more in-depth by reading through the official [docs](https://grpc.io/docs/what-is-grpc/core-concepts/).

1. Unary RPC - More like our traditional APIs where we send a request and receive a single response.
2. Server Streaming RPC - Client sends a request and reads until the server stops sending messages via a stream.
3. Client Streaming - Reverse of the above, the client sends messages through the stream and waits for the server to read and return a response.
4. Bidirectional streaming RPC - Pretty much both (2) & (3) combined - both client and server streams messages both ways.

## Pros and Cons

Any technology comes with a set of advantages and disadvantages. Whether you choose to use gRPC might depend on some of these factors.

**Pros**

- Efficient for inter-process communication with all the good stuff that comes with HTTP/2.
- Well defined interfaces to be able to communicate also while supporting polyglot development.
- Code-generation of client and server stubs with strong types.

**Cons**

- It may not be suitable for external-facing services since most web browsers’ support is limited.
- Changing the service definitions might require rework and regeneration of code.
- Could be a steeper learning curve compared to other RESTful or GQL like architectural styles.

## Conclusion

In this article, we looked at gRPC from a high level. In the next article, we will look at how we can put these into action and generate a gRPC service with Go. Feel free to let me know any feedback or questions. Thanks for reading ✌️

## References

1. [https://developers.google.com/protocol-buffers/docs/proto3](https://developers.google.com/protocol-buffers/docs/proto3)
2. [https://www.oreilly.com/library/view/grpc-up-and/9781492058328/](https://www.oreilly.com/library/view/grpc-up-and/9781492058328/)
3. [https://github.com/grpc-ecosystem/awesome-grpc](https://github.com/grpc-ecosystem/awesome-grpc)you