Why do you need the RPC protocol when you have the HTTP transport protocol?
Tags: http
Published: 2023-08-08 03:23:12


Before explaining, we first need to understand what is the [HTTP protocol](https://www.cdebyte.com/news/533) and what is the RPC protocol.
What is the HTTP protocol


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/38v3dczgcc05eszhzox9.png)

HTTP is a widely used network transmission protocol. The HTTP protocol defines the communication format and communication method between the client (such as browser, mobile phone user APP, etc.) It is a communication model based on request-response, that is, the server returns a response according to the request. Both the request and the response contain some information about the interaction between the two ends (client, server), such as method and header. section, text, etc.

Basic flowchart of HTTP interaction

Figure 1 Basic flowchart of HTTP interaction

The HTTP protocol has many advantages. The HTTP transport protocol supports multiple data formats and encoding methods, and can realize cross-platform and cross-language communication. The communication is simple, flexible, and easy to expand. But at the same time, the HTTP protocol also has some disadvantages:

1) The HTTP transport protocol is stateless, and the connection needs to be re-established for each request, which will increase network overhead and delay.

2) The data transmission of the HTTP transmission protocol is based on text, which will result in a large amount of data and low parsing efficiency.

3) The security of the HTTP transmission protocol is poor, and it is vulnerable to man-in-the-middle attacks and replay attacks.

4) The semantics of the HTTP transport protocol is weak, and it can only express basic addition, deletion, modification and query operations, and cannot satisfy complex business logic.
What is the RPC protocol

RPC, or Remote Procedure Call, is a remote procedure call protocol that allows a client to call a function on a remote server as if it were a local function.

RPC protocol interaction process

Figure 2 RPC protocol interaction process

The advantages of the RPC protocol are efficient, powerful, and easy to use, but it also has some disadvantages, such as:

1) The RPC protocol is stateful and needs to maintain the connection state between the client and the server, which will increase the complexity and resource consumption of the system.

2) The data transmission of the RPC protocol is based on binary, which makes the data difficult to read and debug.

3) The compatibility of the RPC protocol is poor, and there may be inconsistencies in protocols and interfaces between different RPC frameworks.

4) The scalability of the RPC protocol is poor, and it is difficult to support functions such as dynamic service discovery and load balancing.

To sum up, in actual use, the HTTP protocol and the RPC protocol have their own advantages and disadvantages, and there is no absolute distinction between good and bad. Choose the appropriate protocol for different usage scenarios. for example:

1) In the microservice architecture, frequent internal calls are required between services, and the RPC protocol can provide higher performance and reliability.

2) In distributed computing, a large number of computing tasks need to be distributed to different nodes for execution. The RPC protocol can achieve a more flexible load balancing and fault tolerance mechanism.

3) In real-time communication, low-latency and high-concurrency data exchange needs to be realized. The RPC protocol can support multiple transmission protocols and communication modes.

4) If you need to achieve cross-platform and cross-language communication, or you need to support multiple data formats and encoding methods, or you need to use the existing HTTP protocol infrastructure and tools, you can choose the HTTP protocol.

Of course, this is not an absolutely fixed combination. You can also combine the two protocols to achieve a better network, for example:

1) We can encapsulate the RPC protocol on the HTTP protocol, so that the RPC protocol request can be forwarded and processed through the HTTP proxy or gateway.

2) We can use the HTTP protocol as the transport layer on the RPC protocol, so that RPC protocol requests can use the characteristics of HTTP to implement caching, compression, encryption and other functions.

So in general, the emergence of RPC is to deal with network scenarios that require performance that cannot be met by the HTTP protocol. They are not mutually exclusive, but can be selected and combined according to different scenarios and needs.