Microservices with gRPC

Bhargav Shah
4 min readMar 25, 2020

“Hello World” API using gRPC (Remote Procedure Call)

https://grpc.io

Why gRPC?

We have been using REST (most preferred) Microservice Architecture, which is suitable for many real world scenarios, But when you work with backend system which required high performing microservice we need to think beyond REST architecture. We should consider “GraphQL” or “gRPC” as a microservices architecture options. In this blog, we will go through basics of gRPC.

gRPC use case scenarios:

  • Efficiently connecting polyglot services in microservices style architecture
  • Connecting mobile devices, browser clients to backend services
  • Generating efficient client libraries

What is gRPC?

gRPC is a modern open source high performance RPC framework that can run in any environment. It can efficiently connect services in and across data centers with pluggable support for load balancing, tracing, health checking and authentication. It is also applicable in last mile of distributed computing to connect devices, mobile applications and browsers to backend services.

source: grpc.io

The story behind gRPC

Google has been using a single general-purpose RPC infrastructure called Stubby to connect the large number of microservices running within and across data centers for over a decade. Stubby has powered all of Google’s microservices interconnect for over a decade and is the RPC backbone behind every Google service that you use today. In March 2015, Google decided to build the next version of Stubby in the open so Google can share learning's with the industry and collaborate with them to build the next version of Stubby both for microservices inside and outside Google.

https://grpc.io/about

How to use gRPC?

We will create simple “Hello World” API and understand gRPC. At the end will perform small API performance testing between REST API and gRPC.

Overview:

In gRPC, a client application can directly call a method on a server application on a different machine as if it were a local object, making it easier for you to create distributed applications and services. As in many RPC systems, gRPC is based around the idea of defining a service, specifying the methods that can be called remotely with their parameters and return types. On the server side, the server implements this interface and runs a gRPC server to handle client calls. On the client side, the client has a stub (referred to as just a client in some languages) that provides the same methods as the server.

Protocol Buffers: gRPC can use protocol buffers as both its Interface Definition Language (IDL) and as its underlying message interchange format. Protocol buffers are a language-neutral, platform-neutral extensible mechanism for serializing structured data. We will be using “proto3” syntax in our demo.

HTTP/2: gRPC uses http2 protocol. HTTP2 and gRPC deliver significant performance and reliability improvements. Some of benefits are as below,

  • Binary Framing and Compression
  • Multiplexing
  • Flow Control

If you want to see HTTP/2 performance in action then check,

http://www.http2demo.io

This test consists of 200 small images from CDN77.com

We are going to use NodeJS + gRPC for our demo.

Implementation:

  1. Create folder called “helloworld-grpc”. Create “package.json” and “helloworld.proto” in “helloworld-grpc”folder. Don’t forget to run “npm install”.

2. Now we will generate our Client and Server side interface using following command. These will create “helloworld_pb.js” and “helloworld_grpc_pb.js”

$ npm install -g grpc-tools
$ grpc_tools_node_protoc --js_out=import_style=commonjs,binary:./ --grpc_out=./ --plugin=protoc-gen-grpc=`which grpc_tools_node_protoc_plugin` helloworld.proto

3. Now let’s create our “client”. We are simply using files created in previous step and building our gRPC client.

4. Now let’s create our “server”. Again we are using files we created in step 2 and creating our gRPC server which listens on port 50051.

5. It’s time to run our server and client for testing.

$ node server.js
# In different terminal
$ node client.js
# output
$ Greeting: Hello World

Hurray !!! we successfully completed our first gRPC “Hello World” implementation.

I did some basic performance testing between our NodeJS + gRPC and NodeJS + Express— “Hello World” API. All testing done on my Mac. My initial (I will do dig more later) findings are as below,

  • NodeJS + Express (API) : 1000 invokes took 17secs.
  • NodeJS + gRPC (API) : 1000 invokes took 900ms. Wow ❤️

If you are not sure how to create NodeJS + Express — “Hello World” API then refer,

By the way, “curl” command and “Postman” will not work with gRPC. Yes, that was shock 😟 but there are some alternative available for us. We can use “BloomRPC” instead of “Postman”.

Conclusion:

Yes, gRPC is fast in performance, we should keep our eye on gRPC community. But still gRPC is relatively very new and eco-system of gRPC is growing. There are some hurdles to adopt gRPC as team have learning curve and we need to find alternatives for our favorite tool like curl and postman.

Thank you for reading 😎

--

--