Curious to find out whether gRPC is the future? This article aims to explain two architectural APIs styles: REST and gRPC. Before we move on to their differences, we will first explain what an API is and why it is essential for microservices infrastructures.

Afterward, we will describe how RPC is the base for gRPC and consider the critical aspects of differentiation between gRPC and REST APIs. Considering their comparison, we will finally analyze when to use one architectural type or the other.

Table of Contents

Understanding what an API is
    ➤  APIs and Microservices
What is RPC?
What is REST?
What is gRPC?
gRPC and REST: comparison
    ➤  HTTP 1.1 vs HTTP 2
    ➤  Browser Support
    ➤  Payload Data Structure
    ➤  Code Generation Features
    ➤  gRPC and REST: comparison table
When to use gRPC or REST?
Is gRPC better than REST API?
Conclusion
Frequently Asked Questions

Understanding what an API is

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?

How APIs work

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.

APIs and Microservices

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.

What is RPC?

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.

What is REST?

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.

What is gRPC?

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.

Your guide to conducting a thorough code review

gRPC and REST: comparison

Now that we have an overview of gRPC vs REST, let's look at their main differences.

HTTP 1.1 vs HTTP 2

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:

  • Unary: when the client sends a single request and receives a single response.
  • Server-streaming: when the server responds with a stream of messages to a client's request. Once all the data is sent, the server additionally delivers a status message to complete the process.
  • Client-streaming: when the client sends a stream of messages and in turn receives a single response message from the server.
  • Bidirectional-streaming: the two streams (client and server) are independent, meaning that they both can transmit messages in any order. The client is the one who initiates and ends the bidirectional streaming.

Types of Streaming gRPC vs REST

Browser Support

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 organization’s backend data and application functionality).

Payload Data Structure

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 serialized 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.

Code Generation Features

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).

gRPC and REST: comparison table

Features gRPC REST

HTTP 1.1 vs HTTP 2

Follows a client-response model of communication and is built on HTTP 2, which allows for: streaming communication and bidirectional support.

Follows a request-response model of communication and is typically built on HTTP 1.1.

Browser Support

Limited browser support. gRPC requires gRPC-web and a proxy layer to perform conversions between HTTP 1.1 and HTTP 2.

Universal browser support.

Payload Data Structure

gRPC uses Protocol Buffer by default to serialize payload data.

REST mainly relies on JSON or XML formats to send and receive data.

Code Generation Features

gRPC has native code generation features.

Developers must use a third-party tool like Swagger or Postman to produce code for API requests.

When to use gRPC or REST?

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 standardization 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?

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.

Frequently Asked Questions

What is REST?

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.

What is gRPC?

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 serialization format. gRPC enables direct client-server communications and streaming capabilities, making it faster and more efficient for specific use cases.

What's the Difference Between gRPC vs REST?

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.

Is gRPC better than REST?

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.

Is gRPC always faster than REST?

gRPC is generally faster than REST due to its use of HTTP/2 and Protobuf, which enable more efficient data serialization 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.

Is gRPC still used?

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.

Why is gRPC so popular?

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.

Want to read more about gRPC and REST?

More than 500.000 people read our blog every year and we are ranked at the top of Google for topics such as gRPC and REST. If you liked this blog post and would love to read all our blog posts on gRPC and REST, press here.


Build scalable products with Web & Mobile Development

Found this article useful? You might like these ones too!