Mastering RPC: Fundamentals, Serialization, and a TCP-Based Java Implementation

This article explains RPC fundamentals, its advantages, serialization processes, and provides a step‑by‑step TCP‑based Java RPC implementation with code examples and performance comparisons, helping backend developers understand and build simple remote procedure call systems.

Java Backend Technology
Java Backend Technology
Java Backend Technology
Mastering RPC: Fundamentals, Serialization, and a TCP-Based Java Implementation

1. RPC Basic Concepts

RPC (Remote Procedure Call) is a protocol that allows a program to request services from a remote computer without needing to understand the underlying network details. It assumes the existence of transport protocols such as TCP or HTTP, spanning the transport and application layers of the OSI model.

RPC simplifies the development of distributed applications by using a client/server model: the client sends a request with parameters to the server, the server processes the request, returns a response, and the client continues execution.

As system workloads grow, splitting a large system into independent services that communicate via HTTP or other protocols becomes common; RPC serves as an effective tool for inter‑service communication.

Advantages

RPC overcomes hardware cost limits of a single server by turning local calls into remote method invocations, greatly increasing throughput.

It isolates different systems, allowing developers to focus on interfaces rather than implementations, which improves development efficiency and maintenance.

Understanding RPC is essential for building distributed applications.

RPC flow diagram
RPC flow diagram

The diagram shows a typical RPC interaction where the client process acts as a consumer and the server process as a provider.

Object Serialization and Deserialization

Serialization converts an object's state into a format suitable for storage or transmission, while deserialization reconstructs the object from that format. This is necessary because data transmitted over a network must be in binary form.

Serialization vs Deserialization summary
Serialization vs Deserialization summary

Common serialization methods include Java's built‑in serialization, Hessian, JSON, and XML. Performance and space overhead vary among them, as shown in the following charts.

Serialization performance comparison
Serialization performance comparison
Serialization space overhead
Serialization space overhead

2. TCP‑Based RPC Implementation

The core of RPC is socket communication. The basic workflow is:

The consumer establishes a socket connection to the provider.

The consumer sends the method name and parameters through the socket.

The provider parses the request, executes the corresponding method, and returns the result.

The consumer receives and processes the response.

The following images show the actual Java code for a simple TCP‑based RPC demo.

Project structure diagram
Project structure diagram

ConsumerDemo

ConsumerDemo source code
ConsumerDemo source code

ProviderDemo

ProviderDemo source code
ProviderDemo source code

ProviderDemoImpl

ProviderDemoImpl source code
ProviderDemoImpl source code

ProviderServer

ProviderServer source code
ProviderServer source code

Running the server first and then executing ConsumerDemo yields correct responses, as shown in the screenshots below.

Client request result
Client request result

The server loop processes multiple client requests, demonstrating a simple yet functional RPC system. In practice, frameworks like Dubbo provide richer features such as service registration and discovery.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

JavaBackend DevelopmentRPCTCP
Java Backend Technology
Written by

Java Backend Technology

Focus on Java-related technologies: SSM, Spring ecosystem, microservices, MySQL, MyCat, clustering, distributed systems, middleware, Linux, networking, multithreading. Occasionally cover DevOps tools like Jenkins, Nexus, Docker, and ELK. Also share technical insights from time to time, committed to Java full-stack development!

0 followers
Reader feedback

How this landed with the community

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.