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
- Performance: gRPC is designed to be highly performant and efficient, making it an ideal choice for high-throughput, low-latency applications. It achieves this by using binary serialization (Protocol Buffers) and supporting bi-directional streaming.
- Language agnostic: gRPC supports multiple programming languages, making it easy to integrate with existing codebases written in different languages. It provides automatic code generation in multiple languages based on the API definitions in Protocol Buffers.
- Strongly typed APIs: Protocol Buffers provide a strongly typed API, which makes it easier to maintain and evolve the API over time. This is especially important in large-scale distributed systems where changes to the API can have significant downstream impacts.
- Security: gRPC provides built-in support for Transport Layer Security (TLS) and authentication, ensuring that communications between client and server are secure and authenticated.
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
- Multiplexing: HTTP/2 allows for multiple requests and responses to be sent and received simultaneously over a single TCP connection. Which helps to reduce latency.
- Server Push: HTTP/2 enables the server to push resources to the client before they are requested, reducing the need for the client to make additional requests.
- Binary protocol: HTTP/2 uses a binary protocol instead of the text-based protocol used by HTTP/1.1, which simplifies parsing and reduces overhead
- Header Compression: HTTP/2 uses header compression to reduce the size of headers, which can significantly reduce the amount of data that needs to be transferred
- Security: HTTP/2 requires the use of Transport Layer Security (TLS), providing improved security and privacy over HTTP/1.1
gRPC with Node.js
- Protocol buffer
package test;
message Request {}
message Response {}
service TestService {
rpc Test (Request) returns (Response);
}
- Creating server
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);
}
);
- Client
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.