APIs stand for Application Programming Interfaces. These interfaces serve as a software intermediary that establishes specific determinations and rules for applications to interact and talk to each other. An API is responsible for delivering a response from a user to a system, which in turn is sent back from the system to the user. Does it still sound a bit confusing?
Let's imagine we are booking a hotel. We go to the hotel booking page on our laptop, and that page - which is connected to the Internet - sends data (our request) to a server. In turn, the server retrieves the data, interprets it, and then, once the required actions are executed, it sends a response back to us with the information on our interface. This process happens thanks to APIs.
An API specifies the types of requests that one application (web page or mobile app) can make to another and further establishes: how to make those requests; which data formats to use; and the practices that users have to follow.
This article compares gRPC (Google Remote Procedure Call) and REST (Representational State Transfer) because they represent the two most popular architectural styles when creating APIs.
On the one hand, in a monolithic application, all the project's functionalities are included in a single unit, more precisely, in a single codebase. On the other hand, a microservice architecture comprises several smaller services that communicate with each other using protocols like HTTP. The component services that are part of the microservices architecture communicate and interact with each other through APIs. In other words, APIs allow all the services that are integrated into a microservice application to connect and communicate.
The most used architectural style is the REST API. However, there are three main models when building an API: RPC (Remote Procedure Call), REST (Representational State Transfer), and GraphQL. In this article, we will focus on the first two.
RPC uses a client-server model. The requesting server (in other words, the client) requests a message that is translated by the RPC and sent to another server. Once the server receives the request, it sends the response back to the client. While the server is processing this call, the client is blocked, and the internal message passing within servers is hidden.
Further, RPC allows the client to request a function in a particular format and receive the response in the exact same format. Nonetheless, the method of submitting a call with RPC API is found in the URL. RPC supports remote procedure calls both in local and distributed environments.
Like a REST API, RPC also establishes the rules of interaction and how a user can submit "calls" (requests) to invoke methods that communicate and interact with the service.
When using REST APIs, the response from the back-end data is passed to the clients (or users) through the JSON or XML messaging format. This architectural model tends to follow the HTTP protocol. However, it is not uncommon for RPC designs to also select a couple of ideas from HTTP while maintaining the RPC model. In fact, the majority of modern APIs are implemented by mapping the APIs to the same HTTP protocol, despite the model used (RPC or REST).
When the REST API is publicly available, each service that integrates the microservice application can be presented to the user/client as a resource which can be accessed through the following HTTP commands:GET
, DELETE
, POST
, and PUT
.
gRPC stands for Google Remote Procedure Call and is a variant based on the RPC architecture. This technology follows an RPC API's implementation that uses HTTP 2.0 protocol, but HTTP is not presented to the API developer nor to the server. Hence, there is no need to worry about how the RPC concepts are mapped to HTTP, which reduces complexity.
Overall, gRPC aims to make data transmissions between microservices faster. It is based on the approach of determining a service, establishing the methods and respective parameters to enable remote calling and return types.
Moreover, it expresses the RPC API model in an IDL (interface description language), which offers a more straightway to determine remote procedures. By default, the IDL uses Protocol Buffers (but other alternatives are also available) to describe the service interface as well as the payload messages' structure.
Now that we have an overview of gRPC vs REST, let's look at their main differences.
REST APIs follow a request-response model of communication that is typically built on HTTP 1.1. Unfortunately, this implies that if a microservice receives multiple requests from multiple clients, the model has to handle each request at a time, which consequently slows the entire system. However, REST APIs can also be built on HTTP 2, but the request-response model of communication remains the same, which forbids REST APIs to make the most out of the HTTP 2 advantages, such as streaming communication and bidirectional support.
gRPC does not face a similar obstacle. It is built on HTTP 2 and instead follows a client-response communication model. These conditions support bidirectional communication and streaming communication due to gRPC's ability to receive multiple requests from several clients and handle those requests simultaneously by constantly streaming information. Plus, gRPC can also handle "unary" interactions like the ones built on HTTP 1.1.
In sum, gRPC is able to handle unary interactions and different types of streaming:
This aspect is probably one of the main REST API advantages over gRPC. On the one hand, REST is fully supported by all browsers. On the other hand, gRPC is still quite limited when it comes to browser support. Unfortunately, it requires gRPC-web and a proxy layer to perform conversions between HTTP 1.1 and HTTP 2. Therefore, gRPC ends up being mainly used for internal/private systems (API programs within a particular organisation’s backend data and application functionality).
As previously mentioned, gRPC uses Protocol Buffer by default to serialize payload data. This solution is lighter since it enables a highly compressed format and reduces the messages' size. Further, Protobuf (or Protocol Buffer) is binary; thus, it serializes and deserializes structured data in order to communicate and transmit it. In other words, the strongly typed messages can be automatically converted from Protobuf to the client and server's programming language.
In contrast, REST mainly relies on JSON or XML formats to send and receive data. In fact, even though it does not mandate any structure, JSON is the most popular format due to its flexibility and ability to send dynamic data without necessarily following a strict structure. Another significant benefit of using JSON is its human-readability level, which Protobuf cannot compete with yet.
Nonetheless, JSON is not as light-weight or fast when it comes to data transmission. The reason for that lies in the fact that when using REST, JSON (or other formats) must be serialised and turned into the programming language used on both the client and server sides. This adds an extra step to the process of transmitting data which can consequently damage performance and open a possibility for errors.
Unlike gRPC, REST API does not provide in-built code generation features, meaning that developers must use a third-party tool like Swagger or Postman to produce code for API requests.
In contrast, gRPC has native code generation features due to its protoc compiler, which is compatible with several programming languages. This is particularly beneficial for microservices systems that integrate various services developed in different languages and platforms. All in all, the built-in code generator also facilitates creating SDK (Software Development Kit).
As mentioned, despite the many advantages gRPC offers, it has one major obstacle: low browser compatibility. Consequently, gRPC is a bit limited to internal/private systems.
Contrarily, REST APIs may have their disadvantages, as we have discussed, but they remain the most known APIs for connecting microservices-based systems. Plus, REST follows the HTTP protocol standardisation and offers universal support, making this API architectural style a top option for web services development as well as app and microservices integrations. However, this does not mean we should neglect gRPC's applications.
gRPC architectural style has promising features that can (and should) be explored. It is an excellent option for working with multi-language systems, real-time streaming, and for instance, when operating an IoT system that requires light-weight message transmission such as the serialized Protobuf messages allow. Moreover, gRPC should also be considered for mobile applications since they do not need a browser and can benefit from smaller messages, preserving mobiles' processors' speed.
Is gRPC better than REST API? Both gRPC and REST API have their use cases. gRPC excels in high-performance environments, supports bidirectional streaming, and uses Protocol Buffers for efficient serialization. REST API is simpler, more flexible, and better suited for use with web applications or when interacting with multiple programming languages.
REST, or Representational State Transfer, is a popular architectural style for building web services. It uses standard HTTP methods like GET, POST, DELETE, and PUT to communicate between clients and servers. REST is known for its simplicity and statelessness, making it highly suitable for web-based applications and microservices architectures. It typically uses JSON or XML for data exchange.
gRPC is an open-source framework developed by Google for efficient and high-performance communication between microservices. It stands out because it uses HTTP/2 for transport and Protocol Buffers (Protobuf) for its serialisation format. gRPC enables direct client-server communications and streaming capabilities, making it faster and more efficient for specific use cases.
The key differences between gRPC vs REST lie in their protocols, data formats, API designs, and streaming capabilities. gRPC uses HTTP/2 and Protobuf, offering advantages like smaller payloads and bidirectional streaming. Conversely, REST relies on the more traditional HTTP/1.1 and JSON/XML formats, focusing on stateless communication and resource manipulation through standard HTTP verbs.
Whether gRPC is better than REST depends on your project's specific requirements. gRPC offers benefits in performance, efficiency, and suitability for microservices and real-time data streaming. REST shines with its simplicity, wide adoption, and ease of use for web-based services. Each has its place, depending on your application's needs.
gRPC is generally faster than REST due to its use of HTTP/2 and Protobuf, which enable more efficient data serialisation and reduced latency. However, the performance advantage depends on the specific use case, including the nature of the data being transmitted and the network conditions.
Yes, gRPC is actively used and developed. It's particularly favoured in microservices architectures where performance and efficient communication are critical. Its support for multiple programming languages and platforms further contributes to its popularity in diverse tech ecosystems.
gRPC's popularity stems from its high performance, efficiency in communication, and versatility across languages and platforms. Its ability to handle streaming data and complex communication patterns between microservices makes it a strong candidate for modern, scalable applications.
Marketing intern with a particular interest in technology and research. In my free time, I play volleyball and spoil my dog as much as possible.
Your everyday web developer who likes to hide in the backend. Javascript and Ruby are my jam. I still fumble with Docker and my builds break quite often.
People who read this post, also found these interesting: