Understanding RPC for Beginners:
Introduction
Lately, job descriptions have been increasingly mentioning knowledge of the fancy RPC protocol, which got me thinking: what does an analyst need to know to ace an interview? I will tell you in simple words what RPC is, how it works and how it differs from REST.
Understanding RPC
What is RPC?
RPC (Remote Procedure Call) is a method that allows a program on one computer to call a function on another computer as if the function were on the first computer. Imagine asking a friend to do something for you – that's the idea of RPC.
How does RPC work?
RPC works like this:
Client (the one making the request) sends a request to the server, telling it what function it wants to perform and what data it needs to use.
Server (the one who processes the request) receives the request, executes the function and sends the result back to the client.
An example to understand RPC
Imagine you have a weather app and want to know the current weather in New York City. In this example, the app on your phone is the client, and the server that knows the weather is the server. In the case of RPC, the process will look like this:
Client (you): Your phone app wants to know the weather in New York. It calls a remote function
GetWeather("New York")
on server. This function call looks like the functionGetWeather
resides in your application, although it is actually running on a remote server.Server: The server receives this function call
GetWeather("New York")
processes it (perhaps querying another service or database) and gets information about the weather in New York.Response from the server: The server returns the result of the function (such as current temperature, humidity, weather conditions, etc.) back to your application.
Client (you): Your app receives the response and shows you the weather in New York.
The important difference between RPC and REST in this process is that the client calls the function directlyas if it were local, and you don't have to worry about the details of HTTP requests and responses.
![](https://habrastorage.org/getpro/habr/upload_files/13a/b4a/6d8/13ab4a6d83f4b51bbb2fbb0faa791fc9.png)
Interview questions and answers
What is RPC?
Answer: RPC (Remote Procedure Call) is a protocol that allows a program on one computer to call a function on another computer as if the function were on the first computer. This simplifies communication between programs distributed across different systems and hides the complexity of network communication.
How does RPC work?
Answer: RPC works like this:
The client sends a request to the server specifying the function and data to be used.
The server receives the request, executes the specified function, and returns the result back to the client.
This is similar to a normal function call in a program, but is done over the network.
Example: In a weather application, a client calls a remote function
GetWeather("New York")
on server. The server processes the request, receives weather data and returns it to the client.How is RPC different from REST?
Answer: In RPC, the client calls functions directly using binary formats to transfer data, while REST uses standard HTTP requests and text formats such as JSON or XML.
RPC: Uses binary formats (such as Protocol Buffers) to make data transfer more efficient.
REST: Uses text formats (such as JSON) to make debugging and reading data easier.
Example:
RPC: Client calls method
GetWeather(cityName)
and receives a structured answer.REST: The client sends a GET request to the URL
/weather?city=New York
and receives a JSON response.
When is it better to use RPC and when to use REST?
Answer:
RPC: Better suited for internal communication between microservices due to high performance and low overhead*. Used when speed and efficiency of data transfer are important.
REST: Ideal for web services and public APIs due to its simplicity and compatibility with web protocols. Used when it is important to have simple integration and standard interaction methods.
Example:
RPC: Used within a company for interaction between different services (for example, a payment processing service and an order management service).
REST: Used to create a public API that can be used by third party developers.
What are the advantages of RPC over REST?
Answer: RPC can be faster and more efficient by using binary data formats and direct function calls, which reduces overhead* compared to REST.
Performance: Binary formats (eg Protocol Buffers) are more compact and faster to process.
Direct calls: Calling functions directly simplifies the interaction structure and reduces latency.
Example: In highly loaded systems, where minimum latency and maximum performance are important, the use of RPC allows you to speed up data exchange between services.
Overhead* is additional resources spent on completing a task that do not bring direct results, but are necessary to maintain the operation of the system.