Before we get into the nitty-gritty of Protobuf and FieldMasks, it is important to understand what is gRPC. Nowadays, the backend architecture purely depends upon microservices. These services are not only called from frontend but there is also a lot of inter-service communication to get the aggregated results. This is where gRPC (Remote Procedure Call) comes into the picture to facilitate backend to backend communication more efficiently. Although, the client (JavaScript, etc) can also make the call. But why use gRPC when we already have REST and Open API (think of Swagger)
Why gRPC?
In gRPC, one service can directly call a method of another service on a different machine as if it were a local object, making it easier for you to create distributed applications and services.
So how is gRPC’s performance better than other API models? HTTP/2 is one of the big reasons on which gRPC relies. In traditional HTTP protocols (till HTTP/1.1), it is not possible to send multiple requests or get multiple responses together in a single connection. A new connection will need to be created for each of them. This kind of request/response multiplexing is made possible in HTTP/2 with the introduction of a new HTTP/2 layer called binary framing.
Image Credit: Google
The binary layer encapsulates and encodes the data. In this layer, the HTTP request/response gets broken down into frames. Using this mechanism, it’s possible to have data from multiple requests in a single connection.
You might have also come across cases where HTTP headers are bigger than the payload. This is solved in HTTP/2 using a strategy called HPack. Everything in HTTP/2 is encoded before it’s sent, including the headers. But compressing headers is not the most important part. HTTP/2 maps the header on both the client and the server-side. From that, HTTP/2 is able to know if the header contains the same value and only sends the header value if it is different from the previous header.
Image Credit: Google
HTTP/2 doesn’t modify the application semantics of HTTP in any way. All the core concepts, such as HTTP methods, status codes, URIs, and header fields, remain in place. HTTP/2 just modifies the way data is framed and transported between the client and server.