Implement RPC demo from scratch
Last updated on January 9, 2024 am
Implement RPC demo from scratch
Remote Procedure Call (RPC) is a protocol that one program can call another program’s service via network. It is a way to make distributed system easier to use.
The core idea of RPC is all about calling a function provided by a remote server. The client side calls a function, and the server side executes the function and returns the result to the client side.
sequenceDiagram
activate Client
loop
Client ->> Server: send request
deactivate Client
activate Server
Server ->> Client: send response
activate Client
deactivate Server
end
activate Client
deactivate Client
The source code can be found on GitHub: LingkKang/rpc_demo.
The documentation of client side is available at lingkang.dev/rpc_demo.
Design
The design of RPC is simple. The client side calls a function, and the server side executes the function and returns the result to the client side.
In this small demo, I plan to set the task as calculating the hypotenuse of a right triangle. The client side sends the length of the two sides of the triangle, and the server side calculates the hypotenuse and returns the result to the client side. All of them are denoted as float64.
Next step is to design the interface of the RPC, i.e., the message used for communication between client and server. The design is simple and not considering scalability and security issue.
I did not use JSON or XML for the message format, as parsing them will require 3rd party libraries. Instead, I used raw binary format. The message format is as follows:
component | size (bits) |
---|---|
message type | 2 |
message length | 6 |
message payload | 0 - 62 bytes |
message checksum | 8 |
And there are 3 types of messages:
Bits | Type |
---|---|
0b00 |
Error |
0b01 |
Request |
0b10 |
Response |
0b11 |
Reserved |
See the documentation of client::protocol::message
for more details.
Implementation
There are lots of RPC frameworks available, but implementing it from scratch is a good way to understand how it works.
As described above, the code will be divided into two parts: client side and server side. Source code is available on GitHub: LingkKang/rpc_demo.
Remote development
The server side in Go was developed on an Aliyun remote server, and the client side in Rust was developed on a local machine.
VS Code with remote development extension was really helpful for this. The latency is almost intangible, and the experience is almost the same as developing on a local machine.
If you see the commit history in graph, you will see that server and client were developed in parallel, but interactively.
Reference
- Introduction to Distributed System Design - Google Code University - Google Code
- rpc package - net/rpc - Go Packages
- google/tarpc: An RPC framework for Rust with a focus on ease of use.
- gRPC
- Visual Studio Code Remote Development
- Remote Development - Visual Studio Marketplace
- Implementing remote procedure calls | ACM Transactions on Computer Systems
- Remote Procedure Calls (RPC) | 416 Distributed Systems
- RPC and Threads | 6.5840 Distributed Systems