gRPC — An Introduction

A quick introduction to gRPC

Photo by Taylor Vick on Unsplash

Introduction

Since gRPC supports multiple programming languages and platforms, a client written in one language (e.g. Java) can communicate with a server written another language (e.g. Go) seamlessly. This behaviour allows multiple services to talk to each other without any additional effort, thus making it a good candidate for micorservices style architecture.

In this article, let’s try to understand gRPC at a high level.

Target Audience

This article is intended for those who are relatively new to gRPC. If you are already familiar with gRPC, feel free to glance through. You might find something interesting. :-)

Trivia

Do you know what does gRPC stand for? gRPC Remote Procedure Calls. Yep, you read that right. :-)

Types of RPCs

Unary RPCs

Server streaming RPCs

Client streaming RPCs

Bidirectional streaming RPCs

Following diagram depicts how gRPC server and clients typically interact.

Image courtesy: grpc.io

Language Support

gRPC supports a broad spectrum of languages such as C/C++, C#, Dart, Go, Java, Node.js, Objective-C, PHP, Python and Ruby. This helps developers to choose the right language depending on their use cases.

Design Principles

Services with different message types and encodings are expected to work seamlessly. Streaming support is an integral aspect of gRPC. Both asynchronous and synchronous processing of sequence of messages exchanged between client and server are supported. Support for cancellation and timeouts are built-in, which would help to reclaim resources in cases of expensive operations.

For more details on the design principles, refer here.

Service

Using Protocol Buffers, we can define the service interface as well as structure of payload message. (gRPC can also be configured to use other message formats such as JSON, XML and Thrift. But, for performance reasons, it is better to stay with Protocol Buffers.)

Protocol Buffers

Protocol Buffers, also known as protobuf, are mechanisms to serialize structured data. It is much smaller and faster compared to the likes of XML and JSON. Structure of the data can be defined once; reading and writing data to and from various data streams can be performed subsequently. It also supports many programming languages and platforms.

Structure of the data is defined in a .proto file by specifying required (and optional fields) along with their data types. Then, by using Protocol Buffer compiler (known as protoc), additional support files can be generated. These files provides functions required to set/get the fields, serialize message to output stream and parse message from input stream. For example, if we choose Go as the language, the protoc compiler will generate additional files with .pb.go extension. (We’ll see more of this in action when we try to build a sample program.)

To know more on Protocol Buffer, refer here.

Life Cycle

Unary RPC

  • Server may send back it’s metadata or wait to receive client’s request message.
  • Once server receives client’s request, it performs required activities at server side.
  • Once the processing is completed, server returns the response along with status code. If the response status is OK, client gets the response and completes the call at client’s side.

Server streaming RPC

Client streaming RPC

Bidirectional streaming RPC

Other Choices

REST

To know more, check here.

GraphQL

To know more, check here.

What’s Next

Well, let’s head over to my article available here where we’ll discuss how to write a simple client-server program using gRPC.

Passionate about technology | Capability Center Lead (Full Stack Development) at Shell

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store