11 mins
Apr 03, 2024
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)
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.
You can argue what’s the big deal in this. Imagine for a moment that you do not have FieldMask property. What you can do is have extra fields in the request whose purpose would be to declare the intention of the consumer.
This approach requires includeXXX fields for each response field and doesn’t work well for nested fields. It also increases the maintenance and complexity of the request. Not only this, but you also need to update your Discount.Grpc service to check for these fields, which is of course not an ideal solution.
This is the reason why FieldMask is introduced.
In this article, we saw how to use FieldMask for read operation, but you can also use it for update/create operations. Just send only those fields which need to be updated and you can skip the rest of them.