Go Language and the Introduction of Principles of RPC

First, let’s learn about the content of the RPC framework.

Local procedure call

Let us first look at the execution and calling of the program under normal circumstances. For example, the following Go language code:

func main() {

var a, b int

a = 1

b = 2

c := Add(a, b)

fmt.Println(“Calculation result:”, c)


func Add(a int, b int) int {

return a + b


In the above Go language code, we define an Add method to realize the function of adding two numbers. In the main method, the operation of calculating the sum of two variables is realized by calling the Add method. The whole process involves operations such as pushing variable values ​​into the stack, popping out of the stack, and assigning values. Finally, the result of the popped out calculation is returned and assigned to the c variable.

In summary, the process of local program call can be roughly divided into several steps and stages:

  • The program developed by the developer, compiled, and compiled into an executable file recognized by the machine. Run the executable file and call the corresponding function method. During this time, the machine instructions of the executable file will be read, and operations such as stacking and popping assignment will be performed. At this time, the computer is controlled by the process where the executable program is located. The call ends, all memory data is popped out of the stack, and the program execution ends. The computer continues to be controlled by the operating system.

Problems and solutions

As we have mentioned above, remote procedure calls are calls implemented on two or more different physical machines, during which calls must be made across the network. Therefore, if we want to complete function calls in the form of method calls in the previous text, it will not be possible, because the compiler cannot call the program methods on the remote machine through the compiled executable file. Therefore, it is necessary to use RPC to implement the invocation of program methods on the remote server.

The internal principle of RPC technology is realized through a combination of two technologies: local method invocation and network communication technology.

Introduction to RPC

In the above local procedure call example, we executed the program on the computer on a computer to complete the call. With the development of computer technology and the change of demand scenarios, sometimes it is necessary to execute the program on another computer from one computer, so RPC technology was developed later. Especially with the rapid iteration and development of Internet technology, users and demands are almost exponentially increasing at a high speed. At this time, in most cases, the program is deployed on multiple machines, and you need to call other The situation of the program on the physical machine.

RPC is the acronym for the three initials of Remote Procedure Call Protocol, abbreviated as: RPC, translated into Chinese called Remote Procedure Call Protocol. The so-called remote procedure call is a popular understanding that can call the function method of the program running on another server in the local program. This kind of calling process crosses the limitation of the physical server and is completed in the network. In the process of calling the program on the remote server, the local program waits to return the calling result until the remote program is executed, and the result is returned to the local , And finally complete a complete call.

What needs to be emphasized is: remote procedure call refers to the whole process of calling the program on the remote server.

RPC design composition

RPC technology is composed of four parts in architecture design: client, client stub, server, and server stub.

The concept of client and server is mentioned here, which is a way of program design architecture. In the design of modern computer software program architecture, the general direction is divided into two directions, namely: B/S architecture, C/S Architecture. The B/S architecture refers to the browser-to-server interaction architecture. The other is to install a separate application on the computer, called the client, to interact with the server.

Because in the process of invoking the service, one party is the initiating party and the other party is the party providing the service. Therefore, we call the service initiator the client and the service provider the server. The following is an explanation and description of the four roles of RPC:

  • Client: The initiator of the service call, also known as the service consumer. Client Stub: This program runs on the computer where the client is located, and is mainly used to store the address of the server to be called. In addition, the program is also responsible for packaging the data information requested by the client for the remote server program into The data packet is sent to the server Stub program through the network; secondly, the call result data packet sent by the server Stub program must be received, and then analyzed and returned to the client. Server: A program running on a remote computer machine, in which there are methods to be called by the client. Server Stub: Receive the request message data packet sent by the client Stub program through the network, and call the real program function method in the server to complete the function call; secondly, the result of the server execution call is packaged and sent Stub program to the client.

RPC principle and call steps

After understanding the structure of the RPC technology, let’s take a look at how to implement the call from the client to the server. In fact, if we want to implement a remote call process on any two computers in the network, we have to solve many problems, such as:

  • Two physical machines must establish a stable and reliable communication connection in the network. The definition of the communication protocol between two servers is how the programs on the two servers recognize each other’s request and return the result. That is to say, both computers must be able to recognize the information sent by the other party, and be able to recognize the meaning of the request and the meaning of the return, and then they can process it. This is actually the work to be done by the communication protocol.

Let’s take a look at how RPC solves these problems. The specific call steps of RPC are as follows:

Image for post
Image for post

In the above figure, the invocation process of each step of RPC is illustrated in the form of steps 1–10. The specific description is:

  • 1. If the client wants to initiate a remote procedure call, it first calls the function method name it wants to use by calling the local client Stub program; 2. The client Stub program receives the client’s function call request and calls the client request The method name, the parameters and other information carried are serialized and packaged into a data packet. 3. The client Stub finds the IP address of the remote server program, calls the Socket communication protocol, and sends it to the server through the network. 4. The server Stub program receives the data packet information sent by the client, and deserializes the data through the agreed protocol to obtain the requested method name and request parameters and other information. 5. The server-side Stub program prepares relevant data, calls the corresponding function method of the local Server, and passes in the corresponding parameters for business processing. 6. The server program executes the calling process according to the existing business logic, and after the business execution ends, the execution result is returned to the server Stub program. 7. The server Stub program serializes the program call result according to the agreed protocol and sends it back to the client Stub program through the network. 8. The client Stub program receives the return data sent by the server Stub, deserializes the data, and passes the data returned by the call to the client request initiator. 9. The client requests the initiator to get the call result, and the entire RPC call process ends.

RPC related technologies

Through the above series of text descriptions and explanations, we have understood the origin of RPC and the entire invocation process of RPC. We can see that RPC is a collection of a series of operations, which involves many operations on data and network communications. Therefore, we make a summary and analysis of the technologies involved in RPC:

  • 1. Dynamic proxy technology: The Client Stub and Sever Stub programs we mentioned above are all programs automatically generated using dynamic proxy technology in the specific coding and development practice. 2. Serialization and deserialization: In the process of RPC call, we can see that data needs to be transmitted from one machine to another. On the Internet, all data is transmitted in the form of bytes. In the process of programming, we often use data objects. Therefore, if we want to transmit data objects and related variables on the network, we need to serialize and deserialize data objects.
  • Serialization: The process of converting an object into a byte sequence is called serialization of the object, which is the process of encoding. Deserialization: The process of restoring the byte sequence to the object is called the deserialization of the object, which is the process of decoding.

Our common Json, XML and other related frameworks can perform serialization and deserialization encoding and decoding operations on data. At the same time, in the previous course of “Go Language Microservice Theory and Practice”, we have learned the Protobuf protocol, which is also a protocol for data encoding and decoding, which is more widely used in the RPC framework

Written by

Digital Nomad

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store