gRPC Remote Procedure call is a high performance, open-source, feature-rich RPC framework. The ideal usage of gRPC is for inter-service communication in microservices and for communication between mobile applications and servers.
Mobile applications can rely on gRPC for improved performance because it’s built over HTTP/2 which has lower latency while providing higher performance for mobile devices that have a slower CPU. Mobile clients can make use of streaming options in gRPC to save bandwidth and reduce the number of TCP connections made to the server which successively helps in reducing CPU usage and help in optimizing battery in mobile devices.
In microservices architecture, inter-service communication should be faster. gRPC uses protocol buffers which are smaller, faster, and can efficiently connect services than JSON. gRPC servers are asynchronous by default which means that they do not block threads on request and can handle multiple requests in parallel. gRPC promotes the use of SSL in which all the data transmitted over the network are encrypted and authentication is integrated.
● gRPC uses protocol buffers which are a way of serializing structured data that is strongly typed
● Parsing protocol buffers which are in binary format consume less CPU than parsing JSON which is a text format
● Protocol buffers help in backward compatibility without affecting the client-side code
● A large amount of code can be generated in any language from a simple proto file
Proto file is used to define the messages(request and response), service(RPC endpoint).
The following proto file(cart.proto) is a service contract for a simple cart operation.
// The cart service definition
service CartService {
rpc createCart(CreateCartRequest) returns (CreateCartResponse);
}
// The item message
message Item {
string id = 1;
string name = 2;
}
// The request message containing Items
message CreateCartRequest {
repeated Item item = 1;
}
// The response message containing cart id
message CreateCartResponse {
string cart_id = 1;
}
The above cart.proto, which contains service and messages which need to be shared between client and server. The API service is to create a cart. The messages can be used for the corresponding request and response defined in the service.
#technology #grpc #java #api