Performance Comparison: REST vs gRPC — Unveiling the Differences

Ajay Kumar
5 min readJul 15, 2023

--

In today’s interconnected world, efficient communication between software systems is crucial. When it comes to building distributed applications, developers have several options for designing communication protocols. Two popular choices are REST APIs and gRPC. REST (Representational State Transfer) and gRPC (Google Remote Procedure Call) are widely used frameworks that facilitate communication between client and server applications.

In this blog post, we aim to delve into the fundamental distinctions between REST API and gRPC, emphasizing their distinct characteristics and performance considerations. We will also establish the performance comparison environment for both REST and gRPC.

By comprehending the strengths and weaknesses of each approach, developers can make well-informed decisions when selecting the most appropriate communication protocol that aligns with their specific requirements.

High Level Comparison

Protocol :
REST uses RESTful architectural style using http to be specific it use http/1.1
gRPC use Remote Procedural Call (RPC) protocol , it use http/2 as an underlying protocol for client server communication.

Communication Model:
REST follows request response model, where clients makes requests via http protocols
gRPC follows bidirectional streaming and asynchronous communication allowing both client and server to send multiple messages over a single connection.

Data Format and Serialisation:
REST mainly supports human readable formats of data like JSON,XML etc.
gRPC support Protocol Buffers (protobuf), a binary serialization format that provides smaller message sizes and faster serialization/deserialization.

Language Support and Code Generation:
REST has broad language support due to reliance on http protocols
gRPC provides support for multiple programming languages through code generation.

Performance Comparison

1. Architecture :

2. Setup : For setting up compoaarision environment for REST and gRPC we will need following
- REST API : create a dummy Rest Api with multiple endpoints returning different sized response (like 100KB, 500KB, ….)
- gRPC Service : similarly create a dummy gRPC service with same set of responses for like to like comparison between Rest and gRPC
Follow the steps mentioned here Creating gRPC Service .
- gRPC Client : Create a Rest API having http client for calling our main Rest API and a gRPC client for calling our gRPC service, for creating gRPC Client Create gRPC Client in dotNet .
- Deployment Server : Now the last but not least we need a deployment server where we can deploy our service and can verify performance of it . For simplicity purposes we have used an ec2 machine. We will discuss a later how we can configure ec2 machine for our purpose.

3. Configure Services : After creating our we need to configure them to run on different ports because we will deploy both the services on same machine . For configuring port in Rest API add below snippet in application.json

For gRPC :

Now , our REST and gRPC Service will run on ports 8080 and 1234 respectively in our deployment environment.

4. Configure EC2 machine: Now, it’s time to create an EC2 instance and configure it to enable SSH access using a key pair while making it publicly available. You can set the Public IP to auto-assign. Additionally, make sure to configure the Security Group to allow all traffic in the inbound rule.

Note: Since our main focus is to compare the performance of the REST and gRPC services, we will not prioritize AWS security best practices in this case. As we intend to delete the EC2 machine after the experiment, there is no need to extensively configure security rules. However, if you plan to keep the machine for a longer duration and perform additional work, it is recommended to configure the security rules accordingly, considering the appropriate security best practices.

Now , try to ssh the EC2 machine with public IP address , Just make sure that we have configured EC2 correctly.
Note: Make sure httpd server is up and running on your EC2 machine.

5. Deploying Services to EC2 : With our deployment machine prepared, let’s proceed with deploying our services. Once again, we will follow the simplest possible route for deployment.

To publish the services in Linux mode, execute the following command: `dotnet publish -runtime linux-x64 -c Release -o ./grpc_app`. This command will publish the service to the “grpc_app” folder in Release mode. Similarly, publish the other service using a similar command, and then use the `scp` command to securely copy these folders to the EC2 machine.

With the services now in publish mode on the EC2 machine, you can directly execute them using the executable file located in their respective publish folders. For instance, if your gRPC service is named “grpc_app,” you will find an executable file with the same name in the publish folder. To start the application, simply use the command “./grpc_app”.

6. Connecting to Deployed Services: Now, we have our third service, the client, which includes both REST and gRPC clients. To connect with the REST client, we can easily utilize an HTTP client, with the connection URL being http://ip-of-ec2:port.

To make a call to the gRPC service using the gRPC client, it is necessary to place a copy of the protobuf file used in the gRPC service under the Protos folder. After doing this please follow the code snippet provided below.

Here grpcEndpoint is the url to your gRPC Service following the same structure as http://ip-of-ec2:port .

7. Collecting The Performance Numbers : With the completion of all the necessary setup, we are now ready to gather the performance metrics 🎉🎉

Run the client service locally and make call to Rest and gRPC endpoints using postman to capture the numbers.

Here are numbers we have captured with different payload size

From the observations, it is evident that gRPC exhibits nearly linear response behavior across different payload sizes, whereas the response time of REST APIs shows an exponential increase after reaching 1 MB.

CodeBase

You can find the code for all these services we have discussed on github
DotNet Repo : https://github.com/ajay-ksg/RestVsGrpc
Java Repo : https://github.com/deepi2003/REST_API_BigResponse

Conclusion :

In conclusion, the choice between REST and gRPC depends on the specific requirements of the system. REST is simple, flexible, and widely adopted, while gRPC offers high-performance and real-time communication. gRPC generally outperforms REST in terms of throughput, latency, and payload size, but it comes with increased complexity and potential development costs. Consider the trade-offs and choose the protocol that best suits your needs.

Special thanks to Deepti Mittal for collaborating and successfully completing the comparison between Rest and gRPC services.

--

--

Ajay Kumar

I am a Software Developer , Interested in exploring new technologies.