gRPC v/s REST
What is REST? What is gRPC? Which one fares best in performance in gRPC vs REST? Why use gRPC instead of REST? What are the differences between gRPC vs REST? What is the performance benchmark when it comes to APIs? Which is faster gRPC vs REST? Let’s find out the answers to these questions and more.
What is RPC API?
RPC(Remote Procedure Call) is a web architecture that allows you to call a function on the remote server in a defined format and receive a response in the same form. RPC architecture doesn’t consider the format of the server executing the request, whether a local server or a remote server.
However, the primary function of the RPC API is similar to the REST API. The RPC API defines the rules for interaction and methods that enable the interaction process. Later, the client uses the “argument” to call these methods. The arguments that invoke the procedures reside in the query string of URLs.
What is gRPC API?
gRPC (short for "Google Remote Procedure Call") is an open-source framework for building high-performance, scalable APIs (Application Programming Interfaces) that can be used for inter-service communication within a microservices architecture or for communication between client and server applications.
gRPC uses the protocol buffers language (protobufs) for defining the structure of the data being exchanged between services, which provides a more efficient and compact way of transmitting data compared to traditional JSON or XML-based APIs. gRPC also supports bi-directional streaming and flow control, allowing for more efficient communication between services.
One of the key benefits of gRPC is its ability to generate code for client and server applications in multiple programming languages (such as Java, Python, Go, and C#), which simplifies the development process and makes it easier for developers to work with APIs in their preferred language. Additionally, gRPC supports several authentication mechanisms, making it a secure way to transmit sensitive data over the network.
Server Client creation steps with gRPC:
Creating a server and client with gRPC is very simple and the following are the steps
Create the service definition and payload structure in the Protocol Buffer (.proto) file.
Generate the gRPC code from the .proto file by compiling it using protoc
Implement the server in one of the supported languages.
Create the client that invokes the service through the Stub.
Run the server and client(s).
Github Link for code reference: https://github.com/sankarpatw/learnings/tree/master/grpc/greetor
Advantages and Disadvantages of gRPC:
Advantages of gRPC
It can be said that gRPC is designed to provide high performance while ensuring safety and reliability, achieved through the use of Google protobuf and HTTP/2 protocol, which enables multiplexing, transport of data as binary, duplex streaming, and other features.
Duplex streaming allows for simultaneous client-side and service streaming. Additionally, gRPC has a first-class load balancing feature built into its library that can intelligently choose which backend to send traffic to.
Selective message compression is also available, enabling the turning off of compression for images or other mixed compressible content.
The use of protoc allows for auto-generated client and server code. The gRPC library is continuously benchmarked to ensure optimization and avoid speed regressions.
Connection pools can be created with persistent connections to servers through managed channels that can be in either connected or idle states.
Disadvantages of gRPC
It can be observed that gRPC does not support browsers, and therefore cannot be used for external services.
Furthermore, it does not have URL endpoints, which means it cannot be easily tested using tools like Postman or curl to check the response.
Additionally, gRPC does not provide predefined status codes, and creating custom status codes may result in conflicts.
Why not REST?
gRPC and REST are two different architectural styles for building APIs, each with its own strengths and weaknesses. Here are some reasons why one might choose gRPC over REST:
Performance: gRPC is generally faster and more efficient than REST because it uses binary encoding instead of text-based encoding, which reduces the amount of data that needs to be transmitted over the network. Additionally, gRPC uses HTTP/2, which supports multiplexing, compression, and server push, resulting in faster and more efficient communication between client and server.
Language agnostic: While REST is commonly used with JSON, gRPC uses protobufs, which can be used across multiple languages, making it easier to build APIs that can be consumed by services written in different programming languages. This is particularly useful in microservices architecture, where services are built using different programming languages.
Type safety: gRPC uses protocol buffers to define the structure of the data being exchanged between services, which provides strong typing and validation of data. This reduces the chances of runtime errors caused by type mismatches, which can be a problem in REST APIs that use JSON.
Bi-directional/Full-Duplex streaming: gRPC supports bi-directional streaming, which allows for more efficient communication between services. This is particularly useful in cases where services need to send and receive large amounts of data in real time.
Contract-based: gRPC APIs are contract-based, meaning that the API is defined using a protocol buffer schema, which serves as a contract between the client and server. This makes it easier to build APIs that are predictable, reliable, and maintainable.
REST is still a widely used and well-established architectural style for building APIs, and there are situations where it may be more appropriate than gRPC. Ultimately, the choice between gRPC and REST depends on the specific requirements of the project, and factors such as performance, language interoperability, and API design goals should be considered when making the decision.
When to use gRPC over REST?
Let's say we are working on a backend project of a food ordering application, where we had to deal with volumes of real-time data transactions, literally in millions. Basically, it's a mobile application backed with a microservices backend, whose availability and performance is much more important considering the scale.
Our backend services are basically microservices that talk to each other on purpose. And we definitely need a high-performance communication mechanism for communicating between microservices. One of the microservice is exposed public and the rest of the other microservices are internal. So my advice is to use gRPC when we come across the below-mentioned scenarios:
When the microservices is only internal and when one server needs to talk to the other.
When your internal services require duplex streaming with a high load of data.
When you don’t feel to write client libraries.
To conclude, APIs created with gRPC had given us incredible performance improvement compared to our legacy REST API where our response time for same had reduced from 15 ms to 1.71 ms for each request, where average requests ranges from 600 rpm to 1700 rpm.
If you found this helpful and would like to be notified of future posts and tutorials, please subscribe to the newsletter with your email below!
Subscribe to my newsletter
Read articles from Subham Acharya directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by