Design and Implementation of a Lightweight Distributed RPC Framework (buddha)
This article introduces buddha, a lightweight distributed RPC framework, detailing its design principles, serialization choices, TCP packet handling, BIO vs NIO networking, service registration and discovery using ZooKeeper, and provides implementation insights for building efficient remote procedure call systems.
Introduction
Remote Procedure Call (RPC) is a communication protocol that allows a program on one computer to invoke a sub‑program on another computer without the programmer having to write explicit networking code. It aims to simplify building distributed applications while preserving the simplicity of local call semantics.
During a spare period before an internship, the author implemented a lightweight distributed RPC framework named buddha (https://github.com/tinylcy/buddha). Although the codebase is small, it includes all essential components, and this article explains its design, component breakdown, and considerations.
Serialization and Deserialization
Since data transmitted over the network must be converted to bytes, an RPC framework needs to serialize objects to byte arrays and deserialize them back. Java’s default serialization can become a performance bottleneck under high concurrency, so many efficient serialization libraries such as Kryo, fastjson, and Protobuf are used. Buddha currently supports Kryo and fastjson.
TCP Packet Framing (Sticky/Partial Packets)
TCP is a byte‑stream protocol and does not know the boundaries of higher‑level messages. Large messages are split (requiring the server to reassemble them), while small messages may be buffered and sent together (requiring the server to separate them). The common solution is to add length information or delimiters to each packet. Buddha adopts the length‑header approach.
BIO vs. NIO
Blocking I/O (BIO) follows a one‑connection‑one‑thread model, which leads to high thread‑creation cost, large memory consumption, and expensive context switches under high concurrency. Non‑blocking I/O (NIO) allows a single thread to manage many connections by registering interest events. Buddha uses Netty to build a clear NIO‑based networking layer.
Service Registration and Discovery
To ensure stability and reliability, RPC services are often deployed in clusters. A service registry stores the addresses of available providers; clients query the registry to obtain service lists and perform load balancing. Buddha implements this functionality with ZooKeeper, supporting dynamic updates and failure notifications.
Implementation
The complete source code of buddha is available on GitHub (https://github.com/tinylcy/buddha).
References
RPC concept model and implementation analysis (http://mindwind.me/blog/2016/05/22/RPC)
NettyRpc (https://github.com/luxiaoxun/NettyRpc)
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Architecture Digest
Focusing on Java backend development, covering application architecture from top-tier internet companies (high availability, high performance, high stability), big data, machine learning, Java architecture, and other popular fields.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
