Microservices / Networking / Programming Languages / Technology / Sponsored

Build Real-World Microservices with gRPC

27 Nov 2018 10:24am, by

WSO2 sponsored this post.

Kasun Indrasiri
Kasun is a key member of WSO2’s architecture team that drives development efforts for the company's integration platform. Previously, he was involved in developing WSO2 Enterprise Service Bus as a product lead. He has authored the book Beginning WSO2 ESB and co-authored Microservices for the Enterprise. He is an elected member of the Apache Software Foundation and a project management committee member and a committer for the Apache Synapse open source ESB project. Kasun has provided technology consulting on numerous customer engagements by helping to successfully design and implement solutions for integrating web services, REST, SAP and various other technologies.

Early microservices implementations leveraged Representational State Transfer (REST) architecture as the de-facto communication technology. However, RESTful services are often useful for external-facing services, which are directly exposed to consumers. As they are based on conventional text-based messaging (JSON, XML, CVS over HTTP, etc.), which are optimized for humans, these are not ideal choices for internal service-to-service communication.

Rather, using a text-based messaging protocol, we can leverage a binary protocol that is optimized for inter-service communication. The Cloud Native Computing Foundation’s gRPC (gRPC Remote Procedure Call) is an ideal choice for inter-service communication since it uses protocol buffers as the binary data interchange format for inter-service communication.

When we build multiple microservices with different technologies and programming languages, it is important to have a standard way to define service interfaces and underlying message interchange formats. gRPC offers a clean and powerful way to specify service contracts using protocol buffers. Therefore, gRPC is probably the most viable solution for building communication between internal microservices.

In this article, we will take a closer look at why gRPC is a great choice for building inter-microservices communication.

Fundamentals of gRPC

With gRPC, a customer can directly call methods on a server application on a different machine as if it were a local object.  gRPC is based on the foundations of conventional Remote Procedure Call (RPC) technology but implemented on top of the modern technology stacks such as HTTP2, protocol buffers etc. to ensure maximum interoperability.

gRPC natively supports the ability to define a service contract using the gRPC Interface Definition Language (IDL). So, as part of the service definition, you can specify the methods that can be invoked remotely and the data structure of the parameters and return types.

Figure 1. illustrates the use of gRPC with an online retail application as part of an inventory and product-search service. The contract for the Inventory service is defined using gRPC IDL, which is specified in the inventory.proto file. So, a developer for the inventory service should first define all the business capabilities using the service and then generate the service side skeleton code from the proto file. Similarly, the client side code (stub) can be generated using the same proto file.

Figure 1

Since gRPC is programming-language agnostic, you can use heterogeneous languages to build services and clients. In this example, we have generated the Inventory service code using Ballerina (ballerina.io) and the client-side code using Java. You can try out this example using this source code on GitHub.

The service contract of the inventory(inventory.proto) is shown below.

syntax = "proto3";
package grpc_service;
import "google/protobuf/wrappers.proto";
service InventoryService {
   rpc getItemByName(google.protobuf.StringValue) returns (Items);
   rpc getItemByID(google.protobuf.StringValue) returns (Item);
   rpc addItem(Item) returns (google.protobuf.BoolValue);
}

message Items {
   string itemDesc = 1;
   repeated Item items = 2;
}

message Item {
    string id = 1;
    string name = 2;
    string description = 3;
}

The service contract is easy to understand and can be shared between the client and the service. If there’s any change to the service contract, both the service and client side code has to be regenerated.

For example, the following code snippet shows the generated code of the gRPC service for Ballerina. For each operation that we have in the gRPC service definition, the corresponding Ballerina code is generated. (Ballerina provides out-of-the-box capabilities to generate the service or client code with “ballerina grpc –input inventory.proto –output service-skeleton –mode service” or “ballerina grpc –input inventory.proto –output bal-client –mode client”).

import ballerina/grpc;
import ballerina/io;
endpoint grpc:Listener listener {
   host:"localhost",
   port:9000
};

@grpc:ServiceConfig
service InventoryService bind listener {
   getItemByName(endpoint caller, string value) {
       // Implementation goes here.
       // You should return a Items
   }

   getItemByID(endpoint caller, string value) {
       // Creating a dummy inventory item
       Item requested_item;
       requested_item.id = value;
       requested_item.name = "Sample Item " + value ;
       requested_item.description = "Sample Item Desc for " + value;
       _ = caller->send(requested_item);
       _ = caller->complete();
   }

   addItem(endpoint caller, Item value) {
       // Implementation goes here.
       // You should return a boolean
   }

}

For the client side, the product-search service, which is a Java (Spring Boot) service, is again generated from the gRPC service definition of the Inventory service. You can use the maven plugin to generate the client stub for the Spring Boot/Java service (the client code is embedded in the Spring Boot service). The client code that invokes the generated client stub is shown below.

package mfe.ch03.grpc;
import com.google.protobuf.StringValue;
import io.grpc.ManagedChannel;
import io.grpc.ManagedChannelBuilder;

public class InventoryClient {
   public static void main(String[] args) {
       ManagedChannel channel = ManagedChannelBuilder.forAddress("127.0.0.1", 9000)
               .usePlaintext()
               .build();
       InventoryServiceGrpc.InventoryServiceBlockingStub stub
               = InventoryServiceGrpc.newBlockingStub(channel);
       Inventory.Item item = stub.getItemByID(

StringValue.newBuilder().setValue("123").build());
       System.out.println("Response : " + item.getDescription());
   }
}

Communication Under the Hood

When the client invokes the service, the client-side gRPC library uses the protocol buffer and marshals the remote procedure call, which is then sent over HTTP2. On the server side, the request is un-marshaled and the respective procedure invocation is executed using protocol buffers. The response follows a similar execution flow from the server to the client.

The main advantage of developing services and clients with gRPC is that your service code or client side code doesn’t need to worry about parsing JSON or similar text-based message formats (within the code or implicitly inside the underlying libraries such as Jackson, which is hidden from service code). What comes in the wire is a binary format, which is unmarshalled into an object. Also, having first-class support for defining a service interface via an IDL is a powerful feature when we have to deal with multiple microservices and ensure and maintain interoperability.

A Pragmatic Microservices Use Case with gRPC

Microservices-based applications consist of multiple services and are built with a variety of programming languages. Based on the business use case, you can pick the most appropriate technology to build your service. gRPC plays a very important role in this polyglot architecture. For example, let’s further extend our online retail use case for something more realistic. As shown in Figure 2, product-search service communicates with multiple other services, which are built using gRPC as the communication protocol. So, we can define the service contract for each service:inventory, electronics items, clothing items, etc. Now, if you want to foster a polyglot architecture, you can generate service skeletons using different implementation technologies.

Figure 2 illustrates the inventory service with Ballerina lang, the electronics service with Golang and the clothing service with Vert.x (Java). The client side can also generate a stub for each of these service contracts.

Figure 2

A closer look at the microservices communication styles in Figure 2 shows gRPC is used for all internal communication, while the external-facing communication can be based on REST or GraphQL. When we use REST for external-facing communication, most of the external clients can consume the service as an API (leveraging API definition technologies such as Open API) because most of the external clients will know how to communicate with an HTTP RESTful service. Also, we can use technologies such as GraphQL to allow consumers to query the service based on the specific client needs, which cannot be facilitated with gRPC.

Therefore, as a general practice, we can use gRPC for all synchronous communications between internal microservices. Other synchronous messaging technologies such as RESTful services and GraphQL are more suitable for external-facing services.

The Cloud Native Computing Foundation is a sponsor of The New Stack.

Feature image via Pixabay.


A digest of the week’s most important stories & analyses.

View / Add Comments

Please stay on topic and be respectful of others. Review our Terms of Use.