Backend Development 11 min read

Understanding RPC and Its Implementation with SRPC in C++

This article explains the concepts, components, and lifecycle of Remote Procedure Call (RPC), compares it with HTTP, and demonstrates how to use the open‑source C++ SRPC framework to build client and server applications, including code generation, serialization, compression, and protocol handling.

Refining Core Development Skills
Refining Core Development Skills
Refining Core Development Skills
Understanding RPC and Its Implementation with SRPC in C++

The article, originally authored by the SRPC creator and slightly modified by Li Yingxin, introduces Remote Procedure Call (RPC) as a custom network protocol often used alongside HTTP for high‑performance scenarios.

1. What is RPC

RPC consists of a user‑facing interface and an underlying network protocol. The interface defines input, output, and the remote method name, while the framework implements the protocol details.

1.1 User Interface Example

In SRPC, a client call looks like this:

int
main() {
    Example::SRPCClient client(IP, PORT);
    EchoRequest req; // user‑defined request
    EchoResponse resp; // user‑defined response
    req.set_message("Hello World");
    client.Echo(&req, &resp, NULL); // call remote method Echo
    return 0;
}

1.2 Network Protocol

SRPC supports several RPC protocols (SRPC, Thrift, BRPC, tRPC). The article shows a diagram comparing an RPC protocol with HTTP, highlighting that HTTP has a fixed request line, headers, and body, whereas RPC protocols define their own headers and payload formats.

2. What RPC Provides

Using SRPC as an example, the framework layers include:

User code (client calls / server implementations)

IDL serialization (protobuf / thrift)

Data organization (protobuf / thrift / JSON)

Compression (none / gzip / zlib / snappy / lz4)

Protocol (Sogou‑std / Baidu‑std / Thrift‑framed / tRPC)

Transport (TCP / HTTP)

The article focuses on three layers—IDL, protocol, and transport—showing how SRPC generates code from a example.proto file.

syntax = "proto3";

message EchoRequest { string message = 1; };
message EchoResponse { string message = 1; };

service Example { rpc Echo(EchoRequest) returns (EchoResponse); };

Running protoc and srpc_generator produces example.srpc.h , server.pb_skeleton.cc , and client.pb_skeleton.cc , which serve as bridges between user code and the framework.

3. RPC Lifecycle

The article outlines the full request‑response flow: the client serializes the request, optionally compresses it, adds protocol headers, sends it over the network, the server decompresses, deserializes, invokes the user‑implemented service method, then sends back the response through the same layered process.

4. Complete Server Example

A minimal SRPC HTTP server implementation is provided:

#include "example.srpc.h"
#include "workflow/WFFacilities.h"

using namespace srpc;
static WFFacilities::WaitGroup wait_group(1);

void sig_handler(int signo) { wait_group.done(); }

class ExampleServiceImpl : public Example::Service {
public:
    void Echo(EchoRequest *request, EchoResponse *response, srpc::RPCContext *ctx) override {
        response->set_message("OK"); // simple logic
    }
};

int main() {
    unsigned short port = 80; // HTTP server port
    SRPCHttpServer server;
    ExampleServiceImpl example_impl;
    server.add_service(&example_impl);
    server.start(port);
    wait_group.wait();
    server.stop();
    return 0;
}

Compiling with g++ -o server server.pb_skeleton.cc example.pb.cc -std=c++11 -lsrpc and invoking the service via curl -i 127.0.0.1:80/Example/Echo -H 'Content-Type: application/json' -d '{"message":"Hello World"}' demonstrates RPC‑HTTP interoperability.

5. Unlock More

The article concludes that RPC not only provides a clear interface but also enables decoupled design across compression, serialization, and protocol layers, and can be extended to advanced features such as tracing, clustering, and custom transport mechanisms. Links to the SRPC and Workflow GitHub repositories are provided for further exploration.

backend developmentRPCSerializationnetwork protocolsC++IDLSRPC
Refining Core Development Skills
Written by

Refining Core Development Skills

Fei has over 10 years of development experience at Tencent and Sogou. Through this account, he shares his deep insights on performance.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.