gRPC: Microservices API Invocation Framework

Vikas Kesakar

May 10, 2023 | 5 minutes read

gRPC (gRPC Remote Procedure Call) is a high-performance, open-source remote procedure call (RPC) framework developed by Google basically to connect with a large number of microservices. It uses Protocol Buffers, a binary serialization format, to define and serialize APIs and messages sent between client and server applications.

Developers can define services using Protocol Buffers, and gRPC generates server and client code in multiple languages, making it easier to create distributed applications that can communicate with each other seamlessly. One key feature is its support for bi-directional streaming, which can result in significant performance improvements

Strengths of gRPC

Overall, gRPC is a powerful and flexible RPC framework that offers high performance, strong typing, interoperability, and security. These features make it an ideal choice for building modern, cloud-native applications and microservices.

gRPC architecture

HTTP/2

HTTP/2 (Hypertext Transfer Protocol version 2) is a major revision of the HTTP network protocol used to transfer data between a web server and a web browser. It was developed by the HTTP Working Group of the Internet Engineering Task Force (IETF) and was published in May 2015.

HTTP/2 was designed to address the limitations and performance issues of HTTP/1.1, which has been the predominant protocol used on the web since 1999.

Key features

gRPC with Node.js

syntax = “proto3”;
package test;
message Request {}
message Response {}
service TestService {
rpc Test (Request) returns (Response);
}

const grpc = require(“@grpc/grpc-js”);

const protoLoader = require(“@grpc/proto-loader”);

const path = require(“path”);

const PROTO_PATH = path.join(__dirname, “definitions/test.proto”);

const protoDefinition = protoLoader.loadSync(PROTO_PATH, {

 keepCase: true,

 longs: String,

 enums: String,

 defaults: true,

});

const protoDescriptor = grpc.loadPackageDefinition(protoDefinition);

const Server = new grpc.Server();

Server.addService(protoDescriptor.test.TestService.service, {

 test: (call, callback) => {

   callback(null, “Test”);

 },

});

Server.bindAsync(

 “0.0.0.0:50051”,

 grpc.ServerCredentials.createInsecure(),

 () => {

   Server.start();

   console.log(“Server started on port “, 50051);

 }

);

const grpc = require(“@grpc/grpc-js”);

const protoLoader = require(“@grpc/proto-loader”);

const path = require(“path”);

const PROTO_PATH = path.join(__dirname, “definitions/test.proto”);

const protoDefinition = protoLoader.loadSync(PROTO_PATH, {

 keepCase: true,

 longs: String,

 enums: String,

 defaults: true,

});

const protoDescriptor = grpc.loadPackageDefinition(protoDefinition);

// Create client with Test Service defined in protobuf

const client = new protoDescriptor.test.TestService(

 “localhost:50051”,

 grpc.credentials.createInsecure()

);

// Call gRPC method

client.test(null, () => {

 console.log(“Server responded”);

});

Conclusion

In conclusion, gRPC is a powerful and flexible RPC framework that offers high performance, strong typing, interoperability, and security. It is an ideal choice for building modern, cloud-native applications and microservices that require efficient and scalable communication between services. With its support for multiple programming languages, bi-directional streaming, and binary serialization, gRPC can significantly reduce latency and improve throughput, while providing a strongly-typed API that is easy to maintain and evolve over time. Overall, gRPC is a great choice for building distributed systems that require high performance, scalability, and interoperability.