gRPC vs REST

avital trifsik - Dec 26 '22 - - Dev Community

The popular client-server architecture divides communication into two parts: one that takes up all heavy tasks and provides services, known as the server, and the other one that enjoys those services, known as the client.

In general client-server communication, the client simply sends a request asking for resources or services to the server, and the server responds to that request.

For client-server communication, clients and servers need to have libraries that can understand the protocol in which they are communicating. A protocol is a language or set of internet communication rules. They are transport mechanisms that follow some guidelines for transporting data over the internet.

The second most important aspect of client communication is the message format on which both the client and server can agree. This message format is based on some schemas, and by not following these schemas, communication wouldn’t be taking place. Message formats can be similar to XML, which adheres to a schema, or a JSON file, which must contain specific key-value pairs.

Another important aspect of this type of communication to understand is that it is based on a request and response mechanism, which means that the server only communicates when the client initiates the communication. With REST and GraphQL, this is mostly unidirectional. This is a basic problem that will be solved by technology like gRPC.


Why did REST come into existence?

In the early ’90s, a popular client-server protocol called SOAP used XML message format with a hardcoded schema. The schema for the message format was very rigid. Lack of freedom is what caused the abandonment of SOAP and the emergence of REST.

REST came into existence due to the growing popularity of JavaScript, which led to the growth in JSON file format popularity. It was simple to understand and convenient. It just had some key and value pairs in its message format.

In simple words, Rest is a guideline for transferring JSON messages over the internet with HTTP as its protocol (transport mechanism). With Rest, one doesn’t need to worry about making a schema.

What is REST?

We talked about the emergence of REST. Now let’s deep dive into its core technology. So let me tell you that REST stands for Representational State Transfer. Rest is a standardized software architectural style, an API used a lot in the industry.

Reasons for the popularity and widespread use of REST:

  1. REST is simple and standardized for communication architecture. While utilizing R, you wouldn’t have to worry about formatting your message or data. You don’t need to bother with your message format each time, as it is all standardized and industry-used.

  2. REST is scalable. If your service becomes bigger and needs more functionality, you can easily revamp your server without bothering about the performance of REST of the server, and you can create new functions in complete isolation unless they are messing up your data.

  3. REST is stateless. This means each request will have some data that must be understood by the server. The server’s architecture makes the server recall this request’s information, but in the REST architecture, the session state is stored on the client side, making the server more efficient and giving the server little workload for finer functioning.

  4. Last but not least, Rest is a high-performance architecture and supports caching.

When to use REST:

Imagine you are making a website for a restaurant. You have all the menus online, and food items are divided into three categories

  1. Starters

  2. Main course

  3. Beverages

Now, let’s say you want to see all the beverages online. In the Rest architecture, you can easily and consistently partition all of your resources on API endpoints. Of course, you can use multiple authentications on them to make them secure.

In this type of case, we send a GET request to the server to an endpoint where we can get beverage resource data.

Similarly, we can perform all CRUD (Create, Read, Update, Delete) in the Rest API, which makes it suitable for big projects that don’t require super-transfer of data and do not need to be real-time.

Rest works best for projects where the efficient transfer of data is an important factor. Caching is another feature of REST that is useful for standard applications like food booking apps, hotel booking apps, blog websites, online forum websites, etc

Limitations and problems with REST:

REST is great, but it has many cons that are quite crucial in some cases. Let’s talk about them.

  • The need for bi-directional communication.
    What if the server needs to send some data to the client? The mean server wants to initiate communication. In REST architecture, this is not possible. Of course, you can use some tricks, but they are not practical.

  • Imagine making a live score website. How will you manage the server to send a request to the client to update score data? We can make clients send requests every 10 seconds, but it is not a good practice at all. It will overload the server, which might lead to speed problems.

  • REST architecture is purely requested and response and does not support data streaming (continuous event processing).

  • REST property of being stateless can be considered a blessing and a curse because you can’t decide the state of data on REST architecture.


Why did gRPC come into existence?

To tackle the first problem with REST, which is the need for bi-directional communication, WebSocket was invented, which permits the server to initiate the communication, but the problem with it is that it doesn’t have the format. It just sends bytes and has no restrictions.

The Websockets didn’t have any problems of their own, but the actual issue is that any type of communication requires a protocol, and each protocol requires a client library that can communicate using that protocol.

If you are creating a Python application that works on the Rest architecture, you will need an HTTP client that can communicate with the server. In many cases, client libraries are made by a third party, mainly an independent developer. Using third-party libraries can expose your security, and your entire application will depend on a third party for communication.

In the case of a web application, the browser acts like a client library, but for non-web projects, you will need a third-party client library. If you are thinking of making one on your own, then remember that it is not that easy, and you will burden yourself with maintaining another application.

So, to tackle some problems with Rest and to tackle problems with client libraries, Google invented gRPC in 2015.

For gRPC, one client library is maintained by Google for almost all popular languages. It uses the HTTP 2 protocol under the hood and protocol buffer (protobuf) as its message format.

You don’t need to worry about any client library as Google is maintaining it for you and almost every programming language. A single and centralized client library is one of the major strengths of gRPC.

Another benefit of gRPC is the message format that it uses. The protocol buffer is language agnostic, so you can make clients in Python and servers in Go and still be able to communicate without making any fuss.

gRPC is essentially one client library and one protocol that can be used on any device.

What is gRPC?

gRPC uses protobuf to communicate. It serializes proto files into binary format and sends them to the server, and on the server side, they are deserialized into the original format. That’s how it works with protobuf.

gRPC has different forms of communication. You can think of them as features of gRPC.

Features of gRPC:

  • Single request:
    It is a simple type of request-response communication where the client sends a proto request and, on receiving it, the server waits for some time to perform some process and then returns some response.

  • Server streaming
    On making a single request, a flood of data comes as a response from the server. For example, when clicking on a video, a lot of video-related data floods from the server side.

  • Client Streaming
    It is vice versa for server streaming. Here the client sends a lot of data at once to the server. For example, it happens when you share a large file on the internet or upload an image or video to the internet. The client constantly sends data to the server side.

  • Bi-directional streaming:
    In this type of communication, the client and the server send multiple messages. Chatting is an excellent example of it.

These are four popular features of gRPC that make it so powerful.

When to use gRPC

As for the features of gRPC, we saw some use cases for gRPC. Let’s imagine you want to make a chat application. You are not going to use the Rest API as it will not be able to allow fast streaming of bi-directional communication. For this, we will be using the gRPC stream, which will provide some more benefits other than speed.

First, its language-neutral behavior doesn’t matter in what programming language the server or other clients are writing. Communication can still be established until the message format is accepted.

So, with this feature, the Android version of the chat app can communicate with the iOS version and vice versa.

Problems with gRPC:

There are issues with everything, including gRPC.

  • Limited browser support:
    gRPC uses HTTP 2, so it is hard to call the gRPC service directly from the browser. which sometimes requires setting up a proxy.

  • Non-readable form
    As we all know, gRPC uses a binary format that is not readable by humans. It is a disadvantage in some cases.

  • Lack of maturity
    gRPC was developed in 2015, so it is still a bit immature compared to REST, and many bugs and problems need to be resolved.

  • Learning issues:
    Rest and GraphQL use JSON, which is easier to learn. Most people will try to stick to them as protobuf is still a new and complex topic, so it will be hard to find gRPC experts.


Rest vs. gRPC:

Now we will discuss the technical difference between REST and gRPC.

Message format:
The message format used by REST is mostly JSON (sometimes XML format), which is a more readable form and easier to handle. You will not have to worry about the schema in Rest. On the other hand, gRPC uses a protocol buffer to serialize data. The compressed form is lighter and faster to travel with. It is in binary format, so it serializes and deserializes data to transmit it.

HTTP 1.1 vs. HTTP 2:
The Rest API usually uses HTTP 1.1 as its protocol, whereas gRPC uses HTTP 2 as its protocol underhood. The Rest API can still use HTTP 2 but is still limited because of the request-response mechanism. gRPC uses HTTP 2 and takes advantage of HTTP 2 support for both client-response and bidirectional communication. This is another aspect that makes gRPC performance better than REST. It can manage unidirectional requests like HTTP 1.1 and bidirectional requests simultaneously like HTTP 2.

Latency:
gRPC’s low latency and high speed in communication make it useful for connecting to systems that consist of lightweight microservices architecture, which increases message transmission efficiency. In most cases of the network, latency REST takes 25ms, whereas gRPC latency is far less than REST.

Payload limit:
Rest has a maximum payload limit of 45MB when sending outgoing messages, whereas gRPC doesn’t have any limit, meaning your outgoing message can be as heavy as you want.

Security:
In terms of security, Rest will lag as it uses HTTP 1.1 and JSON format, which is easy to decrypt and access. On the other hand, gRPC uses HTTP 2, which is far more secure, and uses protobuf, which is in binary format and hard to decrypt.

Speed:
Here again, gRPC wins, as it offers 10x more speed than Rest, and it is for the same reason, using HTTP 2 as the protocol and protobuf as the message format.

The code generation function
Rest does not provide built-in code generation features, meaning the developer needs third-party apps to produce API code, whereas gRPC has native code generation features due to its protobuf compiler, which is compatible with several programming languages. This is another benefit, particularly for microservice architectures.


Memphis REST Gateway

To enable message production via HTTP calls for various use cases and ease of use, Memphis added an HTTP gateway to receive REST-based requests (=messages) and produce those messages to the required station.

Common use cases that benefit from the REST Gateway are:

  • Produce events directly from a frontend

  • Connect Debezium using HTTP Server

  • ArgoCD webhooks

  • PostHog integration

  • Receive data from Fivetran/Rivery/Any ETL platform using HTTP calls.

Memphis REST Gateway + a JSON schema can be a powerful combination to increase data quality and ensure healthy communication between applications or services. Memphis will soon support gRPC as well.

Read more here


Conclusion:

Ultimately, I would like to say that REST is still the most used and popular architecture, and it is impossible to abandon. REST has many cons, like lack of data streaming, no bi-directional communication, slow speed, etc., but it is best for standard applications that we see in daily life.

On the other hand, gRPC, being young and tough to learn, provides many things like high data streaming on both the client and server side, good bi-directional data streaming, is fast and compact, and uses HTTP 2. Due to its fast performance, gRPC is best suited for high-workload applications like video streaming, song streaming, online gaming, file sharing, and chat applications.


Join 4500+ others and sign up for our data engineering newsletter.


Follow Us to get the latest updates!
GithubDocsDiscord


Originally published at memphis.dev by Shay Bratslavsky, Software Engineer at Memphis.dev.

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .