What is the difference between REST and RPC?

Mondo Health Updated on 2024-01-30

The way distributed systems communicate with each other is critical to their efficiency, reliability, and overall functionality. REST (Representational State Transfer) and RPC (Remote Procedure Call) are well-known communication paradigms.

REST and RPC allow different systems or components to communicate with each other. However, they are fundamentally different in terms of philosophy, design, and application.

This tutorial focuses on the differences between REST and RPC, revealing their history, principles, advantages, and disadvantages.

The differences in the origins of REST and RPC highlight the evolution of web communication and highlight the challenges and contexts that both aim to solve.

The concept of RPC can be traced back to the early days of distributed computing. The system needs a way to execute a procedure or function on a remote computer, essentially calling it like a local procedure.

With the introduction of SunRPC, developed by Sun Microsystems, the RPC system began to take shape in the 80s of the 20th century. With the development of the Internet, the functionality of RPCs has been continuously enhanced. This led to the development of variants such as xml-rpc, which allowed remote calls using xml over http and later json-rpc.

REST doesn't focus on operations or methods like RPCs, but rather is resource-centric. Fielding emphasizes the use of the inherent capabilities of the web, specifically the stateless requests and standardized approach to HTTP.

Given its simplicity and consistency with networking principles, REST quickly gained traction. It became the standard for public APIs on the web. While RESTful principles are still very common, more and more people are moving to more flexible and efficient protocols, especially in specific use cases where REST may not be optimal.

REST is an architectural style. It provides a set of constraints that enable the system to achieve properties such as performance, scalability, and modifiability.

At its core, REST is about representing and manipulating resources on a network. A resource can be anything – an object, a service, data – and is uniquely identified by its URI.

RPC is a protocol that a program can use to request services from a program on another computer. It allows a program to make a procedure or subroutine execute on another address space, usually another computer on a shared network. This is similar to calling a method on an object that exists on a different machine.

The way data is transferred and the conventions used for such communication make REST and RPC significantly different. Let's dissect their respective communication models.

REST relies on its standardized set of methods to communicate with the power and simplicity of the HTTP protocol. These standard http verbs ensure consistency and acceptability of interactions.

Because of stateless communication, every request from the client to the server must contain all the information needed to understand and process that request. This makes the interaction clear and eliminates the need to maintain session state on the server.

RPC's model is more about invoking operations. Its method calls are more detailed and indicate the specific process.

RPC is like calling a function. The client specifies the process to be executed along with the parameters, and the server returns the result. This can appear more straightforward and granular than a resource-based approach.

An important aspect to note is that RPCs may appear to be tightly coupled due to the nature of their processes. Modern implementations such as grpc are built with flexibility and efficiency in mind, allowing for more scalable and loosely coupled systems.

How a system identifies and processes the data or services it interacts with is central to its design and functionality. The REST and RPC paradigms have different approaches to this problem.

In the RESTful world, everything revolves around the concept of resources. Every resource in a RESTful system is uniquely identified by its URI. This design choice provides an easy way to locate, manipulate, and interact with entities on the network.

In REST, it's not just about identifying a resource, it's about how we represent it. Common representations include JSON and XML, among others. The idea is to use these representations to interact with resources via standard HTTP methods.

RPC revolves around invoking procedures or methods. In RPC, we call a specific method provided by the server. The method name and parameters are the core of the request. There is no standard like URIs in REST. Instead, the method name and its signature become the defining aspect.

Parameters can be passed by name or by location, and the method naming usually reflects what it is trying to achieve on the server side. While this can be seen as flexible, it can also lead to challenges with version control and change method signatures over time.

The representation and structure of the transmitted data plays a key role in the clarity, efficiency, and performance of the communication. REST and RPC have different strategies for this.

REST is independent of data format, but there are some popular conventions that are widely adopted in the industry. Most RESTful services use JSON because it's lightweight and easy to use. XML was more popular in early implementations, especially in SOAP-based services, but support for JSON has declined.

The beauty of REST is that we can choose the representation that best suits our needs. For example, while JSON and XML are common, some systems may use YAML, HTML, or even custom formatting.

The format of the data in RPC largely depends on the type and specification of the RPC protocol used.

In newer RPC protocols such as GRPC, there is a strong emphasis on performance. For example, Protobuf is smaller and faster than JSON and XML. This can significantly improve performance, especially in systems with high throughput requirements.

List of high-quality authors

Related Pages