WEB SERVICES: REST VS gRPC
A web service is a service provided by an independent separate program that can be called by an application and can be run on different machines. A web service implements an API (Application Programming Interface) to communicate between client and server over a network usually through HTTP.
To implement the web services and to perform communication between the devices on the network Service API are used. Service API is the method that handles these communications, such as REST (Representational State Transfer) and gRPC(Google Remote Procedure Call).
REST is an architecture based design standard for providing communication between computer systems on the internet which makes the communication process efficient, scalable and reliable in a distributed system. REST was introduced by Roy Fielding, a computer scientist. So, REST is a designing pattern for APIs. REST is based on HTTP specification. The communication is done between the client and the server. The communication is done without client and server knowing about each other in an independent manner. So, that changes made in any one side does not affect the operation of the other side.
So, whenever a RESTful API is called, the server will transfer the client with a representation of the state of the requested resource by the client. The resource is any object that the API can provide data from the server.
Statelessness: REST follows a stateless paradigm, meaning that the server does not store the state of the client.
Benefits of using REST:
- Uniform Interface: HTTP GET, POST, PUT and DELETE
- Uniform State Representation: Define a virtual machine whose states are represented by interconnected resources through URL.
- Unrestricted Scalability: The web is the limit
What is RPC?
RPC which stands for Remote Procedure Call is a method which enables program running on one computer on a distributed network make procedure call over the shared network to another device.
IT extends the notion of traditional procedure calling. The procedure call is also known as a function call or a subroutine call. It also uses the client server model. In object-oriented programming paradigm, the RPC procedure call is represented as Remote Method Invocation(RMI). It is a synchronous operation which requires the requesting program to be suspended until the output of the current remote procedure has been returned. The implementation of light weight threads allows multiple RPCs to execute parallel.
The concept of RPC has been around for more than half a century.
It is the RPC protocol where the procedure calls are encoded with XML as data format and HTTP as the transport mechanism. It can be implemented with any of the programming languages and also allows cross language communication. An XML-RPC message is just an HTTP-POST request. They have request and responses.
JSON is a light weighted data exchange format. JSON RPC is a light weighted stateless RPC protocol which uses JSON as the data format. All the data transfer types in JSON-RPC is of single object type, which are serialized using JSON. It is quite similar to XML-RPC. JSON RPC features and allows notifications (message that does not require response) and multiple calls that can be sent to the server.
It is a web service protocol that uses JSON. Inspired from JSON-RPC due to the absence of service description specification with documentation in JSON-RP. The main difference between the two is that JSON-WSP can describe its own service methods. Furthermore, JSON-WSP support attachments in specification.
The new RPC in the tech world currently and the most popular one is gRPC. As, most of the user are shifting towards gRPC currently. Obviously, its super-fast and super effective procedure call system making the data communication process faster is one of the reason for the popularity of gRPC.
gRPC is an open source RPC framework that can run anywhere which was developed by Google. It uses HTTP/2 for data transport making it easier to traverse through proxies and firewalls. gRPC uses protocol buffers as the Interface Definition Language(IDL). Protocol buffers are faster, simpler and smaller providing better performance in comparison to XML and JSON. Protocol buffers are not just message interchange format but, also used for description of message interchange unlike, JSON and XML.
It consists of two parts, GRPC protocol and the data serialization method. It uses Protobuf for data serialization but, any other serialization method can be implemented with gRPC . Protobuf is a binary transfer protocol which packs the data in a compact format for faster and effective data transmission.
As, gPRC is based on http2, so it inherits many features from http2, such as compressing headers, cancellation privilege, persistent single TCP connections, timeout contract between client and server .
Some of the features and benefits of gRPC are listed below:
- Coverage and Simplicity: Available for every development platform.
- Supports messages from KBs to 100 MBs
- Free and open:
- Interoperability and Reachability to the destination.
- General Purpose: It can be used for wide array of use-cases
- Polygot Programming: Available in almost all popular programming languages.
- Supports traditional Request/Response model
- Conversion to REST (with Protobuf and via Proxy)
- Supports bidirectional streaming:
- Almost Server less: gRPC implementation is as server less as it can get.
- Layered architecture.
- Blocking and Unblocking: Supports both asynchronous and synchronous processing of the sequence of messages exchanged.
- Capability for building full duplex streaming.
- High Performance.
- Build with mobile clients in mind
- Pluggable: Provides extension point for plugging in various features such as security, load-balancing,etc. according to the need.
- Metadata: lets the client provide information associated with the call to the server and vice versa.
REST vs gRPC : The Future
For a long time, REST API has been the pillar of web programming. But now, gRPC seems to be replacing REST and encroaching its territory.
Why this is happening you can find in the list below:
- Format of the payload: REST API works with JSON while gRPC works with Protobuf messages.
- HTTP/2 VS HTTP 1.1: gRPC uses the newer HTTP/2 protocol while the REST is stick with the older version.
- Message Vs. Resources and Verbs: REST is built very tightly on top of HTTP. So, it is quite difficult to map logic and operations into this strict REST format. While on the other hand gRPC have services with clear interfaces and structured messages.
- Streaming vs Request/Response: REST supports request-response model only, while the gRPC supports bidirectional constant streaming taking on the advantage of HTTP/2.
- The gRPC Gateway: Support for gRPC in the browser is not widely popular. For using gRPC sevice from a web application or from cross language not supported by gRPC. It provides a REST API gateway to define your service.
In the world full of microservices, the reign of REST is very soon going to be replaced by gRPC with it’s alluring features, performance benefits and ease of development. But, with REST still excelling in publicly exposed APIs and for backward compatibility reasons, it is still going to be around for quite some time.
Also Read: C# 8.0 features
Also Read: Best Laptop for a programmer