- gRPC - a popular implementation of RPC (Remote Procedure Call ) developed by Google
- Primarily used for communication between micro-services
Features of gRPC
- Protocol Buffers as IDL
- gRPC requires you to define the structure of the service and the message it exchanges using a special language called protocol buffer IDL (Interface Definition Language) to ensure a standardised way of communication btw the services
- It’s where you basically store your data and function contracts in the form of a proto file.
- As this is in the form of a contract, both the client and server need to have the same proto file. The proto file acts as the intermediary contract for client to call any available functions from the server.
- Protobuf is a language-agnostic binary serialization format that is more efficient in terms of size and speed compared to traditional JSON or XML. It helps in encoding and decoding data for communication.
- HTTP/2
- gRPC uses HTTP/2 as its underlying protocol for communication. HTTP/2 is a newer version of the HTTP protocol and comes with features like multiplexing, header compression, and other optimizations, making communication faster and more efficient.
- Bidirectional streaming
- Unlike traditional RPC, gRPC supports bidirectional streaming. This means that both the client and server can send a stream of messages to each other concurrently. It’s like having a phone conversation where both parties can talk at the same time.
- Multiplexing
- gRPC can multiplex multiple streams over a single TCP connection. Imagine you are sending multiple letters to your friend, and they can respond to each letter independently. This reduces the overhead associated with establishing and maintaining multiple connections.
- Support for multiple languages
- gRPC is designed to be polyglot, meaning it supports multiple programming languages. You can have a service written in one language communicating seamlessly with a service written in another language, which adds flexibility to your system architecture.
gRPC workflow
gRPC Example
syntax = "proto3";
service Greeter {
rpc SayHello (HelloRequest) returns (HelloResponse);
}
message HelloRequest {
string name = 1;
}
message HelloResponse {
string greeting = 1;
}
Why don’t we use gRPC btw frontend and backend?
gRPC needs access to low level HTTP 2 primitives. There are no browsers currently that offers that level of control, thus gRPC is simply not supported.