gRPC and REST are two different methods for building APIs (Application Programming Interfaces) that allow different services to communicate with each other over a network. In this article, we will create simple gRPC and SpringBoot applications to compare their speed of them. Which will win and why?
Application Structure
We will have 3 modules:
- gRPC server (localhost:50051): This module will implement a gRPC service receive a proto request and send back a proto response via the gRPC stub implemented in the benchmark client.
- Spring Boot server (http://localhost:8081): This module will implement a Spring Boot application that receives an HTTP request and sends back a JSON response via the rest client implemented in the benchmark client.
- Benchmark client (http://localhost:8082): This module will receive a testing tool request and send it to gRPC server or Spring Boot server based on the request.
Both the gRPC server and Spring Boot server will have no logic, logging, or database connection to get maximum performance. The response of the Spring Boot server will re-use the generated code of Protobuf to make sure the response size is the same as gRPC server.
I will use the docker-compose to run all modules, not use debug mode in the IDE.
The proto file:
syntax = "proto3";
package com.benchmarking.grpc;
option java_multiple_files = true;
message GetRequest {
int32 number = 1;
}
message GetResponse {
int32 result = 1;
}
service SampleService {
rpc Get(GetRequest) returns (GetResponse);
}
Benchmark request
More {number} more response data.
The benchmark REST request:
curl --location 'localhost:8082/rest/{number}'
Sample request
The benchmark gRPC request:
curl --location 'localhost:8082/grpc/{number}'
AB testing
I'm using Apache HTTP server benchmarking tool to run the benchmark test.
I will send 1000 requests and 100 requests concurrency (request at a time)
Run the test for REST
ab -n 1000 -c 100 http://localhost:8082/rest/100
Run test for gRPC
ab -n 1000 -c 100 http://localhost:8082/grpc/100
Compare result
Time taken (seconds) | Requests per second | Longest request (milliseconds) | |
---|---|---|---|
REST | 43.674 | 22.9 | 5882 |
gRPC | 7.077 | 141.30 | 799 |
So, gRPC is the winner in this benchmark.
Why gRPC is faster than REST?
There are two main reasons:
- Protocol: gRPC relies on HTTP/2, providing better performance and reduced latency, while REST uses HTTP/1.1.
- Data format: gRPC employs Protocol Buffers, a binary serialization format, resulting in smaller payloads and faster communication; REST usually leverages JSON or XML, which are text-based formats.
Conclusion
In this test, gRPC beat REST.
gRPC has better performance because of HTTP/2 protocol and Protocol Buffers.
Although gRPC has more advantages than REST, in some cases, REST still being a wise choice.
If we want to build an internal API system with performance as the highest priority, we should choose gRPC to implement it.
But if we don't focus on the performance, we want more human-readability of the data transformed or intergraded with other systems using REST, now REST could be a better choice.
So this is a small speed benchmark between gRPC and REST. Hopefully, this post can bring you some interesting information.
You can find the source code here.
Happy coding.