

Practical API Design at Netflix, Part 2: Protobuf FieldMask for Mutation Operati...
source link: https://netflixtechblog.com/practical-api-design-at-netflix-part-2-protobuf-fieldmask-for-mutation-operations-2e75e1d230e4
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Practical API Design at Netflix, Part 2: Protobuf FieldMask for Mutation Operations
By Ricky Gardiner, Alex Borysov
Background
In our previous post, we discussed how we utilize FieldMask as a solution when designing our APIs so that consumers can request the data they need when fetched via gRPC. In this blog post we will continue to cover how Netflix Studio Engineering uses FieldMask for mutation operations such as update and remove.
Example: Netflix Studio Production

Previously we outlined what a Production is and how the Production Service makes gRPC calls to other microservices such as the Schedule Service and Script Service to retrieve schedules and scripts (aka screenplay) for a particular production such as La Casa De Papel. We can take that model and showcase how we can mutate particular fields on a production.
Mutating Production Details
Let’s say we want to update the format
field from LIVE_ACTION
to HYBRID
as our production has added some animated elements. A naive way for us to solve this is to add an updateProductionFormatRequest method and gRPC endpoint just to update the productionFormat:
This allows us to update the production format for a particular production but what if we then want to update other fields such as title
or even multiple fields such as productionFormat
, schedule
, etc? Building on top of this we could just implement an update method for every field: one for Production format, another for title and so on:
This can become unmanageable when maintaining our APIs due to the number of fields on the Production. What if we want to update more than one field and do it atomically in a single RPC? Creating additional methods for various combinations of fields will lead to an explosion of mutation APIs. This solution is not scalable.
Instead of trying to create every single combination possible, another solution could be to have an UpdateProduction
endpoint that requires all fields from the consumer:
The issue with this solution is two-fold as the consumer must know and provide every single required field in a Production even if they just want to update one field such as the format. The other issue is that since a Production has many fields the request payload can become quite large particularly if the production has schedule or scripts information.
What if, instead of all the fields, we send only the fields we actually want to update, and leave all other fields unset? In our example, we would only set the production format field (and ID to reference the production):
This could work if we never need to remove or blank out any fields. But what if we want to remove the value of the title
field? Again, we can introduce one-off methods like RemoveProductionTitle
, but as discussed above, this solution does not scale well. What if we want to remove a value of a nested field such as the planned launch date field from the schedule? We would end up adding remove RPCs for every individual nullable sub-field.
Utilizing FieldMask for Mutations
Instead of numerous RPCs or requiring a large payload, we can utilize a FieldMask for all our mutations. The FieldMask will list all of the fields we would like to explicitly update. First, let’s update our proto file to add in the UpdateProductionRequest
, which will contain the data we want to update from a production, and a FieldMask of what should be updated:
Now, we can use a FieldMask to make mutations. We can update the format by creating a FieldMask for the format
field by using the FieldMaskUtil.fromStringList() utility method which constructs a FieldMask for a list of field paths in a certain type. In this case, we will have one type, but will build upon this example later:
Since our FieldMask only specifies the format
field that will be the only field that is updated even if we provide more data in ProductionUpdateOperation
. It becomes easier to add or remove more fields to our FieldMask by modifying the paths. Data that is provided in the payload but not added in a path of a FieldMask will not be updated and simply ignored in the operation. But, if we omit a value it will perform a remove mutation on that field. Let’s modify our example above to showcase this and update the format but remove the planned launch date, which is a nested field on the ProductionSchedule
as “schedule.planned_launch_date”:
In this example, we are performing both update and remove mutations as we have added “format” and “schedule.planned_launch_date” paths to our FieldMask. When we provide this in our payload these fields will be updated to the new values, but when building our payload we are only providing the format
and omitting the schedule.planned_launch_date
. Omitting this from the payload but having it defined in our FieldMask will function as a remove mutation:
Recommend
-
249
Files Permalink Latest commit message
-
48
In today’s industry, having a great product might not be enough. Competition lurks around the corner, and a key advantage for...
-
37
I mmutability, Pure Functions, Side Effects, State Mutation are words we come across almost every day, but little do we know how they work or what they are or what goodies they bring to software developm...
-
6
Practical API Design at Netflix, Part 1: Using Protobuf FieldMaskBy Alex Borysov, Ricky Gardin...
-
6
Practical API Design Using gRPC at Netflix Sep 21, 2021 1...
-
6
在我们之前的文章中,我们讨论了在设计API时如何利用FieldMask...
-
9
在 Netflix,我们大量使用 gRPC 来实现后端到后端的通信。当我们处理请求时,知道调用者对哪些字段感兴趣以及忽略哪些字段通常是有益的。某些响应字段的计算成本可能很高,某些字段可能需要远程调用其他服务。远程调用都是有...
-
8
First images released of Candela’s P-12 flying electric ferry, beginning operations next year June 23,...
-
7
Change Streams in MongoDB 6.0 Support Pre- and Post-Image Retrieval, DDL operations, and more MongoDBJuly 19, 2022
-
10
本文介绍了在Go语言中如何使用oneof字段以及如何通过使用google/protobuf/wrappers.proto中定义的类型区分默认值和没有传值;最后演示了Go语言中借助fieldmask-ut...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK