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

  1. Introduction to Distributed System Design - Google Code University - Google Code
  2. rpc package - net/rpc - Go Packages
  3. google/tarpc: An RPC framework for Rust with a focus on ease of use.
  4. gRPC
  5. Visual Studio Code Remote Development
  6. Remote Development - Visual Studio Marketplace
  7. Implementing remote procedure calls | ACM Transactions on Computer Systems
  8. Remote Procedure Calls (RPC) | 416 Distributed Systems
  9. RPC and Threads | 6.5840 Distributed Systems

Implement RPC demo from scratch
https://lingkang.dev/2024/01/08/rpc/
Author
Lingkang
Posted on
January 8, 2024
Licensed under