Netflix的实用API设计,第一部分。使用Protobuf FieldMask
By Alex Borysov, Ricky Gardiner
通过 Alex Borysov, Ricky Gardiner
Background
背景介绍
At Netflix, we heavily use gRPC for the purpose of backend to backend communication. When we process a request it is often beneficial to know which fields the caller is interested in and which ones they ignore. Some response fields can be expensive to compute, some fields can require remote calls to other services. Remote calls are never free; they impose extra latency, increase probability of an error, and consume network bandwidth. How can we understand which fields the caller doesn’t need to be supplied in the response, so we can avoid making unnecessary computations and remove calls? With GraphQL this comes out of the box through the use of field selectors. In the JSON:API standard a similar technique is known as Sparse Fieldsets. How can we achieve a similar functionality when designing our gRPC APIs? The solution we use within the Netflix Studio Engineering is protobuf FieldMask.
在Netflix,我们大量使用gRPC来实现后端到后端的通信。当我们处理一个请求时,知道调用者对哪些字段感兴趣,哪些字段被他们忽略,往往是有益的。一些响应字段的计算可能很昂贵,一些字段可能需要远程调用其他服务。远程调用从来不是免费的;它们会带来额外的延迟,增加出错的概率,并消耗网络带宽。我们如何理解调用者不需要在响应中提供哪些字段,以便我们可以避免进行不必要的计算和删除调用?在GraphQL中,这一点是通过使用字段选择器来实现的。在JSON:API标准中,类似的技术被称为稀疏字段集。在设计我们的gRPC API时,我们怎样才能实现类似的功能?我们在Netflix Studio工程中使用的解决方案是protobufFieldMask。

Money Heist (La casa de papel) / Netflix
抢钱》(La casa de papel)/ Netflix
Protocol Buffers, or simply protobuf, is a data serialization mechanism. By default, gRPC uses protobuf as its IDL (interface definition language) and data serialization protocol.
协议缓冲区,或简称为protobuf,是一种数据序列化机制。默认情况下,gRPC使用protobuf作为其IDL(接口定义语言)和数据序列化协议。
FieldMask is a protobuf message. There are a number of utilities and conventions on how to use this message when it is present in an RPC request. A FieldMask message contains a single field named paths, which is used to specify fields that should be returned by a read operation or modified by an update operation.
FieldMask是一个protobuf消息。当这个消息出现在RPC请求中时,有许多关于如何使用这个消息的实用工具和惯例。一个FieldMask消息包含一个名为paths 的字段,用于指定应该由读操作返回或由更新操作修...