Network Abstractions: Remote Procedure Calls
Look into what remote procedure calls are and how they help developers.
Remote procedure calls (RPCs) provide an abstraction of a local procedure call to the developers by hiding the complexities of packing and sending function arguments to the remote server, receiving the return values, and managing any network retries.
What is an RPC?
RPC is an interprocess communication protocol that’s widely used in distributed systems. In the OSI model of network communication, RPC spans the transport and application layers.
RPC mechanisms are employed when a computer program causes a procedure or subroutine to execute in a separate address space.
Note: The procedure or subroutine is coded as a regular/local procedure call without the programmer explicitly coding the details for the remote interaction.
RPC 是一种广泛用于分布式系统的进程间通信协议。在 OSI 网络通信模型中,RPC 跨越传输层和应用层。
当计算机程序使某个过程或子例程在另一个地址空间中执行时,会使用 RPC 机制。
How does RPC work?
When we make a remote procedure call, the calling environment is paused and the procedure parameters are sent over the network to the environment where the procedure is to be executed.
When the procedure execution finishes, the results are returned to the calling environment where execution restarts as a regular procedure call.
To see how it works, let’s take an example of a client-server program. There are five main components involved in the RPC program, as shown in the following illustration:
The components of an RPC system
The client, the client stub, and one instance of RPC runtime are running on the client machine. The server, the server stub, and one instance of RPC runtime are running on the server machine.
During the RPC process, the following steps occur:
- A client initiates a client stub process by giving parameters as normal. The client stub is stored in the address space of the client.
- The client stub converts the parameters into a standardized format and packs them into a message. After packing the parameter into a message, the client stub requests the local RPC runtime to deliver the message to the server.
- The RPC runtime at the client delivers the message to the server over the network. After sending a message to the server, it waits for the message result from the server.
- RPC runtime at the server receives the message and passes it to the server stub.
Note: The RPC runtime is responsible for transmitting messages between client and server via the network. The responsibilities of RPC runtime also include retransmission, acknowledgment, and encryption.
-
The server stub unpacks the message, takes the parameters out of it, and calls the desired server routine, using a local procedure call, to do the required execution.
-
After the server routine has been executed with the given parameters, the result is returned to the server stub.
- The server stub packs the returned result into a message and sends it to the RPC runtime at the server on the transport layer.
- The server’s RPC runtime returns the packed result to the client’s RPC runtime over the network.
- The client’s RPC runtime that was waiting for the result now receives the result and sends it to the client stub.
- The client stub unpacks the result, and the execution process returns to the caller at this point.
Note: Back-end services use RPC as a communication mechanism of choice due to its high performance and simple abstraction of calling remote code as local functions.
当我们进行远程过程调用(RPC)时,调用环境会被暂停,过程的参数会通过网络发送到要执行该过程的环境中。
当过程执行完成后,结果会返回到调用环境,执行流程会像普通的本地过程调用一样继续。
为了更清楚地理解其工作原理,让我们以一个 客户端-服务器 程序为例。在 RPC 体系中,有五个主要组成部分,如下图所示:
RPC 系统的组成部分
客户端、客户端存根(client stub)和一个 RPC 运行时实例运行在客户端机器上。 服务器、服务器存根(server stub)和另一个 RPC 运行时实例运行在服务器机器上。
在 RPC 过程执行时,会发生以下步骤:
- 客户端调用客户端存根:客户端像普通函数调用一样传递参数,客户端存根被存储在客户端的地址空间中。
- 客户端存根打包参数:客户端存根将参数转换为标准格式,并将其打包到消息中。随后,它请求本地 RPC 运行时将消息传输到服务器。
- 客户端 RPC 运行时传输消息:客户端的 RPC 运行时将消息通过网络传输到服务器,同时等待服务器返回的结果。
- 服务器端接收消息:服务器的 RPC 运行时接收该消息,并将其传递给服务器存根。
注意: RPC 运行时负责在客户端和服务器之间通过网络传输消息,并处理消息的重传、确认和加密等任务。
- 服务器存根处理请求:服务器存根解包消息,从中提取参数,并通过本地过程调用(local procedure call)调用目标服务器端的例程进行实际的执行。
RPC 的工作流程
- 服务器例程执行完成后返回结果:服务器例程执行完毕后,将结果返回给服务器存根。
- 服务器存根打包结果:服务器存根将返回的结果打包成消息,并将其发送到服务器端的 RPC 运行时。
- 服务器 RPC 运行时传输结果:服务器端的 RPC 运行时通过网络将打包的结果返回给客户端的 RPC 运行时。
- 客户端 RPC 运行时接收结果:客户端的 RPC 运行时收到服务器返回的结果,并将其传递给客户端存根。
- 客户端存根解包结果:客户端存根解包返回的结果,并将执行流程返回到调用者,完成远程调用。
注意: 由于 RPC 具有高性能并且能够将远程调用抽象成本地函数调用的简单模型,因此 后端服务 通常将 RPC 作为主要的通信机制。
Real-world usage:
RPCs are used in many real-world services. Take a look at the examples given below:
- Google: Google uses RPC in various parts of its distributed infrastructure. They have developed gRPC, an open-source framework that uses RPC to build efficient and highly performant distributed systems. It’s employed for services like Google Search, YouTube, etc., where it allows Google to maintain communication and data exchange between different components of their services, which is essential for achieving real-time performance.
- Uber: Uber uses RPC for various functions, including real-time location tracking, ride matching, and communication between drivers and riders. RPC allows Uber to provide fast and responsive services by facilitating data exchange between the user’s app and the server.
- Facebook: Most services at Facebook are written using Thrift for RPC, and some storage systems use Thrift for serializing records on disk. This provides several benefits to Facebook. For example, it enables interoperability between different languages, such as a Python client communicating with a C++ server.
Summary
The RPC method is similar to calling a local procedure, except that the called procedure is usually executed in a different process and on a different computer.
RPC allows developers to build applications on top of distributed systems. Developers can use the RPC method without knowing the network communication details. As a result, they can concentrate on the design aspects, rather than the machine and communication-level specifics.
真实世界的应用:
RPC 在许多现实世界的服务中都有应用。以下是一些示例:
- Google: Google 在其分布式基础架构的各个部分使用 RPC。他们开发了 gRPC,这是一个开源框架,利用 RPC 构建高效且高性能的分布式系统。它被用于 Google 搜索、YouTube 等服务,使 Google 能够在不同组件之间保持通信和数据交换,从而实现实时性能。
- Uber: Uber 在多个功能中使用 RPC,包括实时位置追踪、行程匹配以及司机与乘客之间的通信。RPC 使 Uber 能够在用户应用程序与服务器之间高效地交换数据,从而提供快速响应的服务。
- Facebook: Facebook 的大多数服务都使用 Thrift 进行 RPC,一些存储系统也使用 Thrift 进行磁盘记录的序列化。这为 Facebook 带来了多个好处,例如,它允许不同语言之间的互操作性,使 Python 客户端可以与 C++ 服务器通信。
总结
RPC 方法类似于调用本地过程,但被调用的过程通常在不同的进程甚至不同的计算机上执行。
RPC 允许开发者在分布式系统上构建应用程序。开发者可以使用 RPC,而无需了解底层的网络通信细节。因此,他们可以专注于设计,而不必关心具体的机器和通信层面的技术细节。