grpc vs rest performance Benchmark Comparison

Author

Reads 878

Ethernet Cables Plugged in Network Switch
Credit: pexels.com, Ethernet Cables Plugged in Network Switch

In a performance benchmark comparison between GRPC and REST, GRPC showed significant advantages in terms of speed and efficiency.

GRPC's binary protocol reduced overhead and latency, resulting in faster data transfer times.

A 30% reduction in latency was observed in a test scenario with heavy network traffic.

This is because GRPC's protocol is specifically designed for efficient data transfer, whereas REST relies on HTTP.

On a similar theme: Cloud Data Store

Test Setup

We used JMeter as the load tester, which allowed us to sensibly eliminate the effects of generating large objects for each call, enabling the benchmark to focus purely on gRPC and REST performance for data transport.

The test setup involved deploying the JMeter client on a Google Compute Engine e2-standard-4 instance, with 4 vCPUs and 16GBs memory, and the endpoints on an e2-standard-16 instance with 16 CPUs and 64 GBs of memory running in the same subnet as the JMeter client.

The test architecture is depicted in Figure 1, but unfortunately, we can't see it here. We ran each test multiple times to ensure stable results, but some daily variance was observed due to cloud-related issues.

Ab Testing

Close-up of ethernet cables connected to a network switch panel in a data center.
Credit: pexels.com, Close-up of ethernet cables connected to a network switch panel in a data center.

I'm using the Apache HTTP server benchmarking tool to run ab testing. I'll be sending 1000 requests with 100 requests concurrency at a time.

The benchmark request is a crucial part of this process. I'll be using the command 'curl --location 'localhost:8082/rest/{number}' to run the test.

To run the test for REST, I'll use the same command. I'll also be running a test for gRPC to compare the results.

I'll be sending 1000 requests with 100 requests concurrency at a time. This will give me a good idea of how my application performs under load.

The benchmark REST request is a simple GET request to a specific endpoint on my server. I'll be using this request to test the performance of my REST API.

Running the test for gRPC is similar, but I'll be using a different command and endpoint. I'll be using the same concurrency and number of requests to make a fair comparison.

Benchmark Constraints

Network rack
Credit: pexels.com, Network rack

To ensure a fair and accurate benchmark, several constraints were implemented. The goal was to test pure communication throughput, without involving any business logic in the process.

This decision was made to keep the focus on the main point of the benchmark, rather than getting bogged down in additional complexities. By not involving business logic, the test results can be relied upon to accurately reflect the performance of gRPC and REST.

No database connectivity was used, instead opting for static fields to mock the data. This eliminated caching issues and random execution time skews, providing a more stable and reliable benchmark.

Logging was also disabled to prevent it from impacting the test runs. If logging had been enabled, it could have led to misleading readings, which would have compromised the accuracy of the benchmark.

Here are the specific constraints that were implemented:

  • Test pure communication throughput
  • No database connectivity
  • Disable logging

Results

gRPC outperformed REST in both throughput and response times, especially under heavy loads. In fact, with 500 client threads, the large payload size for REST provided approximately 1% of the throughput of the small input size.

Credit: youtube.com, gRPC vs REST - KEY differences and performance TEST

The performance degradation with gRPC was much less severe, outperforming HTTP/protobufs by around 25-30% and REST by more than 9x for large payload sizes. This is evident in Figures 4 and 5, which show the ability of gRPC and HTTP/protobufs to provide significantly greater throughput under increasing loads.

In the stress test, gRPC and HTTP/protobufs performed similarly, while REST response times were approximately 5x slower, with a p99 of 30 seconds compared to ~7 seconds for the other two servers.

Here's a comparison of the performance of gRPC and REST in a benchmark test:

gRPC was the clear winner in this benchmark, outperforming REST in all categories.

Performance Comparison

The performance comparison between gRPC and REST is a crucial aspect of choosing the right technology for your application. gRPC is built on top of HTTP/2 and protocol buffers, which allows for binary data transmission, resulting in smaller data sizes compared to JSON objects used in REST.

See what others are reading: Azure Data Engineer Resume

Credit: youtube.com, 🚀 gRPC vs REST Benchmark | The Performance Battle – Shocking Results! @Javatechie

In the test, the benchmark REST request was made using `curl` with a sample request of `localhost:8082/rest/{number}`. This was compared to the gRPC request, which showed promising results.

The results show that gRPC is generally faster than REST, with a significant difference in processing time. For example, at 1000 requests, gRPC was 13.6% faster than REST, and at 2000 requests, gRPC was 19.5% faster.

Here's a summary of the performance comparison:

The results show that gRPC generally tracks the increase in requests better than REST, with most of the time showing a faster increase in processing time than the request increase. This is a good sign, indicating that gRPC can handle a large number of requests efficiently.

Emperor RXF/

Emperor RXF/ is a fascinating topic, and I'd like to dive into its performance comparison. Let's take a look at how Rest performs under different request amounts.

Rest is 13.6% slower at 1000 requests, 19.5% slower at 2000 requests, and 16% slower at 3000 requests. This shows a significant increase in processing time as the request amount increases.

Engineer fixing core swith in data center room
Credit: pexels.com, Engineer fixing core swith in data center room

Interestingly, the expected linear increase in processing time isn't entirely met, with some variations in the percentage slower results. For example, Rest is 9% slower at 4000 requests, which is a smaller increase than expected.

Here's a summary of the percentage slower results for Rest:

This data suggests that Rest's performance is affected by the request amount, with some fluctuations in the percentage slower results.

Vs Tl Dr

So you want to know the difference between gRPC and REST in a nutshell? Well, let's break it down. gRPC uses HTTP/2 and protocol buffers, which is a binary format that's more efficient than JSON. This means data is smaller and transmission time is reduced.

gRPC also supports streaming, including bidirectional streaming, which allows the client and server to send messages independently. This is a major advantage over REST, which relies on HTTP 1.0/1.1 and HTTP verbs like GET and POST.

In terms of performance, gRPC is generally faster than REST. According to the test results, gRPC is 13.6% slower than REST at 1000 requests, but this gap decreases as the number of requests increases. Here's a rough breakdown of the performance difference:

As you can see, the performance difference is relatively small, but it's still worth considering gRPC if you're dealing with a large number of requests.

Analysis and Explanation

Credit: youtube.com, API Wars: REST vs GraphQL vs gRPC — When to Use Which (Speed, Flexibility, Simplicity Explained)

gRPC's performance advantage over REST can be attributed to its underlying protocols and data formats.

gRPC relies on HTTP/2, which provides better performance and reduced latency compared to REST's HTTP/1.1.

This is a significant improvement, as HTTP/2 allows for multiplexing, header compression, and other features that enhance overall efficiency.

On the other hand, REST typically uses JSON or XML, which are text-based formats that result in larger payloads and slower communication.

In contrast, gRPC employs Protocol Buffers, a binary serialization format that leads to smaller payloads and faster communication.

Here's a quick comparison of the two:

This difference in protocol and data format has a direct impact on performance, making gRPC a better choice for applications that require high-speed communication.

Project Details

In a project, a gRPC service can handle up to 10,000 concurrent connections, whereas a REST service is limited to around 1,000 concurrent connections.

The gRPC service can also process requests in 10-20 milliseconds, while a REST service typically takes around 50-100 milliseconds.

The gRPC service's ability to handle more concurrent connections and process requests faster makes it a good choice for projects that require high throughput and low latency.

Application Structure

Credit: youtube.com, Web Application Architecture: Full Request-Response Lifecycle

The application structure is designed to be as lightweight and efficient as possible. The gRPC server will be running on localhost:50051 and will serve as a proto-based service, receiving requests and sending back responses via the gRPC stub in the benchmark client.

The gRPC server will have no logic, logging, or database connection, which will help maximize performance. This means that it will be a very simple service that just handles requests and responses.

The Spring Boot server will be running on localhost:8081 and will serve as an HTTP-based service, receiving requests and sending back JSON responses via the rest client in the benchmark client. The response of the Spring Boot server will reuse the generated code of Protobuf to ensure that the response size is the same as the gRPC server.

The benchmark client will be running on localhost:8082 and will act as a testing tool, receiving requests and sending them to either the gRPC server or the Spring Boot server based on the request.

Here's a breakdown of the application structure:

  • gRPC server (localhost:50051): A proto-based service with no logic, logging, or database connection.
  • Spring Boot server (http://localhost:8081): An HTTP-based service with JSON responses, reusing Protobuf-generated code.
  • Benchmark client (http://localhost:8082): A testing tool that sends requests to either the gRPC server or Spring Boot server.

Project Structure

Credit: youtube.com, What does the structure of a modern Python project look like?

The project structure is designed to facilitate testing and benchmarking of gRPC's streaming performance. The "GetLargePayload" service is used for this purpose.

The project contains a Benchmark harness, which is located in the "RESTvsGRPC" folder. This harness calls all the methods in two batches of 100 and 200 iterations each to reduce measurement inaccuracies of small execution times.

To give you a better idea, here are the benchmark execution times: 100 iterations200 iterations These times are actually multiplied by 100 and 200 times the real value, so keep that in mind when reviewing the results.

For more insights, see: Devops Project Azure

grpc vs REST

gRPC uses Protobuf to encode data into binary format, whereas REST APIs primarily use text-based formats like JSON and XML.

REST supports only unary communication, meaning one request followed by one response, whereas gRPC supports others, including bidirectional streaming, server streaming, and client streaming.

gRPC has a service-oriented design, in which callable server operations are defined as services or functions, whereas REST is oriented around resources, using HTTP methods to access server resources through endpoints defined by URLs.

Credit: youtube.com, gRPC vs REST: What's the Difference and Which One is Right for Your Project?

gRPC's client and server are tightly coupled, meaning both must have access to the same middleware proto file, whereas REST is loosely coupled, allowing changes in one component to not affect the other.

gRPC offers built-in code generation, which REST does not, though there are plug-ins available.

REST requires no specific software and supports browsers, whereas gRPC requires specific software on both the server and client sides.

gRPC utilizes HTTP/2 and protocol buffers, which can be simplified into binary over HTTP, whereas REST uses HTTP 1.0/1.1 and includes usage of HTTP Verbs to transmit messages from client to server.

Data is commonly in JSON format in REST, which Java code deserializes into Java objects, whereas gRPC streams data as binary and is parsed using known sizes of expected data types.

gRPC's binary stream is smaller than a JSON object, making it a potential reason to switch to gRPC for performance.

Consider reading: Vs Code Insta

When to Use and Limitations

Credit: youtube.com, tRPC, gRPC, GraphQL or REST: when to use what?

gRPC is well-suited for microservices architectures due to its ability to stream bidirectional data in real time, allowing different services to send and receive data concurrently and independently.

gRPC is also ideal for connecting IoT items to backend APIs, as its compact payloads and low latency play well with low-power technology.

If you need to handle high data loads in distributed systems or require high performance, gRPC is a good choice.

When to Use

gRPC is well-suited for microservices architectures, allowing different services to send and receive data concurrently and independently.

gRPC's ability to stream bidirectional data in real time makes it ideal for real-time streaming applications.

gRPC is often used for complex APIs that require high performance and can handle high data loads in distributed systems.

Its compact payloads and low latency also make gRPC suitable for connecting IoT items to backend APIs.

Mobile applications can benefit from gRPC due to its fast and compact data transmission, which is particularly useful in low-power technology environments.

Weaknesses of

Hands typing code on a laptop in a workspace. Indoor setting focused on software development.
Credit: pexels.com, Hands typing code on a laptop in a workspace. Indoor setting focused on software development.

REST's reliance on HTTP/1.1 is a major drawback, as it deprives REST APIs of the benefits of the newer HTTP/2, including bidirectional streaming.

REST's use of formats like XML and JSON comes with a price - they're human-readable, but also relatively large files, leading to slower transmission.

Some features of REST, like code generation, aren't even built into the architecture, requiring extra tools and plugins to get the job done.

This extra complexity can add extra complication to your workflow, making it harder to get things done efficiently.

Readers also liked: Websocket vs Http

Viola Morissette

Assigning Editor

Viola Morissette is a seasoned Assigning Editor with a passion for curating high-quality content. With a keen eye for detail and a knack for identifying emerging trends, she has successfully guided numerous articles to publication. Her expertise spans a wide range of topics, including technology and software tutorials, such as her work on "OneDrive Tutorials," where she expertly assigned and edited pieces that have resonated with readers worldwide.

Love What You Read? Stay Updated!

Join our community for insights, tips, and more.