Remote Procedure Call (RPC) API Explained 🎓 (2024)

Understanding Technology

RPC, or Remote Procedure Call, entails invoking a function or method on a server located remotely. The RPC protocol ensures consistent result retrieval for a given problem, irrespective of the execution location — whether it’s local or on a remote server with superior resources. Unlike REST, RPC is an older protocol that extends conventional local procedure calling, allowing the called procedure to exist in a different address space than the calling procedure. These two processes can reside on the same system or on different systems connected by a network.

How does RPC work?

Below image shows sequence in which RPC call happens between Caller (Client) and Callee (Server). The calling environment undergoes suspension, transferring procedure parameters across the network to the execution environment where the procedure is intended to run. Subsequently, the procedure executes in that environment. Once the procedure concludes and generates results, these results are sent back to the calling environment, where execution resumes as if returning from a standard procedure call.

Remote Procedure Call (RPC) API Explained 🎓 (3)
Remote Procedure Call (RPC) API Explained 🎓 (4)

A client initiates a client stub procedure by passing parameters in the conventional manner. The client stub resides within the client’s own address space. Subsequently, the client stub marshals (packs) the parameters into a message. Marshalling involves converting the representation of the parameters into a standardized format and copying each parameter into the message. The client stub forwards the message to the transport layer, which transmits it to the remote server machine.

On the server side, the transport layer conveys the message to a server stub. The server stub demarshalls (unpacks) the parameters and invokes the desired server routine using the standard procedure call mechanism. Upon completion of the server procedure, it returns to the server stub (e.g., through a regular procedure call return). The server stub then marshals the return values into a message and hands the message to the transport layer.

The transport layer dispatches the result message back to the client’s transport layer, which returns the message to the client stub. The client stub demarshalls the return parameters, and execution returns to the caller.

Below is sample C# example where RPC is achieved using TCP client:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Sockets;
using System.Net;
namespace Server {
class Program {
static void Main(string[] args) {
try {
IPAddress ipaddress = IPAddress.Parse("192.168.117.1");
TcpListener mylist = new TcpListener(ipaddress, 8000);
mylist.Start();
Console.WriteLine("Server is Running on Port: 8000");
Console.WriteLine("Local endpoint:" + mylist.LocalEndpoint);
Console.WriteLine("Waiting for Connections…");
Socket s = mylist.AcceptSocket();
Console.WriteLine("Connection Accepted From:" + s.RemoteEndPoint);
byte[] b = new byte[100];
int k = s.Receive(b);
Console.WriteLine("Recieved..");
for (int i = 0; i < k; i++) {
Console.Write(Convert.ToChar(b[i]));
}
ASCIIEncoding asencd = new ASCIIEncoding();
s.Send(asencd.GetBytes("Automatic Message:" + "String Received byte server !"));
Console.WriteLine("\nAutomatic Message is Sent");
s.Close();
mylist.Stop();
Console.ReadLine();
} catch (Exception ex) {
Console.WriteLine("Error.." + ex.StackTrace);
}
}
}
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Sockets;
using System.IO;
namespace Client {
class Program {
static void Main(string[] args) {
try {
TcpClient Tcpclient = new TcpClient();
Console.WriteLine("Connecting..");
Tcpclient.Connect("192.168.117.1", 8000);
Console.WriteLine("Connected");
Console.WriteLine("Ente the String you want to send ");
string str = Console.ReadLine();
Stream stm = Tcpclient.GetStream();
ASCIIEncoding ascnd = new ASCIIEncoding();
byte[] ba = ascnd.GetBytes(str);
Console.WriteLine("Sending..");
stm.Write(ba, 0, ba.Length);
byte[] bb = new byte[100];
int k = stm.Read(bb, 0, 100);
for (int i = 0; i < k; i++) {
Console.Write(Convert.ToChar(bb[i]));
}
Tcpclient.Close();
Console.ReadLine();
} catch (Exception ex) {
Console.WriteLine("Error" + ex.StackTrace);
}
}
}
}

RPC APIs

RPC APIs represent a more intuitive style of API, involving the creation of functions that perform calculations or return data. These APIs typically adhere to the use of the GET and POST HTTP verbs.

Consider an API endpoint designed to retrieve data for all orders a user wishes to view:

GET /getAllOrders?user_id=5

An endpoint responsible for creating a new order might take the following form:

POST /createOrder
{ "products": [ { "id": 5, "price": 55.56 } ] }

For these APIs, client-side stubs are generated to facilitate calls. To illustrate further, envision WCF services where a service is defined using contracts and exposed in a configuration file. Subsequently, a client class can be created using SVCUtil, providing method stubs for the service.

Advantages

RPC offers ABSTRACTION by concealing the message-passing nature of network communication from the user. It frequently bypasses certain protocol layers to enhance performance, recognizing the significance of even slight performance gains due to frequent invocation of RPCs by a program.

Moreover, RPC facilitates the use of applications in distributed environments, extending functionality beyond local settings. It minimizes the effort required for code rewriting or redevelopment when implementing RPC.

RPC supports both process-oriented and thread-oriented models, providing versatility in its application. They’re simple to implement and easy to extend — you just add another function/endpoint. Also, performance oriented — RPC is a great option when performance and/or low-latency writes are critical.

Disadvantages

RPC involves more coupling — its methods can be a leaky abstraction. It is difficult to discover methods and predict what the API’s semantic conventions look like. Code is harder to keep “clean” since every action is a separate method call. This can lead to an explosion of separate endpoints that look similar. Developers are often-times less familiar with RPC, and so RPC-style APIs oftentimes requires more developer support.

When to use RPC APIs?

RPC serves as an excellent choice for synchronous service-to-service communication. When speed is crucial for inter-service communication in particular microservices architecture, RPC offers the performance necessary for your API requirements.

High Performance RPC

gRPC is one of the modern RPC protocol built based on protobuf and HTTP/2. Lot of developers are using this for interservice communication between microservices. We will cover in-depth regarding gRPC in our microservices blog series.

🙏Thanks for taking the time to read the article. If you found it helpful and would like to show support, please consider:

  1. 👏👏👏👏👏👏Clap for the story and bookmark for future reference
  2. Follow me on Chaitanya (Chey) Penmetsa for more content
  3. Stay connected on LinkedIn.

Wishing you a happy learning journey 📈, and I look forward to sharing new articles with you soon.

Remote Procedure Call (RPC) API Explained 🎓 (2024)

References

Top Articles
Latest Posts
Article information

Author: Aron Pacocha

Last Updated:

Views: 6291

Rating: 4.8 / 5 (68 voted)

Reviews: 83% of readers found this page helpful

Author information

Name: Aron Pacocha

Birthday: 1999-08-12

Address: 3808 Moen Corner, Gorczanyport, FL 67364-2074

Phone: +393457723392

Job: Retail Consultant

Hobby: Jewelry making, Cooking, Gaming, Reading, Juggling, Cabaret, Origami

Introduction: My name is Aron Pacocha, I am a happy, tasty, innocent, proud, talented, courageous, magnificent person who loves writing and wants to share my knowledge and understanding with you.