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.

Now think of another scenario where the user adds 10 items in the basket. We also need to apply discounts to the individual items. If we give frontend the responsibility to call discount API and calculate the discounted item price, then it needs to make 10 API calls. Bad code smell.
Don’t worry if the message format is a bit confusing. This configuration file contains the context information. The numbers are just identifiers of the fields. By using this configuration, we can send messages in encoded format.Let’s try to understand by seeing AddressModel.
If it were JSON, then the address object might have looked like below:
But in protobuf, the same message would get converted into: 1211Some street236123456
google.protobuff.FieldMask is nothing but a protobuf message. It contains a single field named paths, which is used to specify the fields to be returned by a read operation or modified by an update operation.
As basket service is interested in only id and amount fields, it will add them in the Paths array. This array would be received by Discount.Grpc service in the request object.
Run the application and check the response received by basket service. For that, we will send a request from Swagger.
In the below code snippet, highlighted in yellow colour, you can see the coupon model object. It just has two fields.
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.












