REST vs GraphQL vs gRPC

Atul Agrawal
4 min readFeb 11, 2022

Today, developers have three choices for API development — REST, GraphQL and gRPC. The purpose of this article is highlight key differences between each of them so that developers can pick the right method for solving their problems.

REST

Description: REST is an acronym for Representational State Transfer. In this approach, client calls the server to get the current state of the resources in the specified representation format (JSON, XML) and server responds to the request by returning the resource’s representation.

Key characteristics : Stateless, Client-Server interaction.

Message Format : JSON, XML (common)

Protocols : HTTP (common)

Communication Channel : Unidirectional — Client to Server

Maturity : Very High

Strengths:

  • Resource Based — The business capabilities are modeled as resources and their state is represented using JSON/XML. The boundaries between the APIs is enforced by design using HTTP methods (GET, PUT, POST etc).
  • Simplicity The API contract is simple to understand and use. The HTTP operations are self explanatory as developers have very good of understanding of the semantics of the HTTP protocol.

Limitations :

  • Complex relationships : If relationships between resources is multi-directional or have multi-level hierarchies (Social Media use case) then model resources modelling become challenging and performance becomes a major factor for both client and server due to over-fetching and under-fetching of the underlying resource data. This also have side effect on network due to back and forth chatty calls.
  • Streaming : REST is not built for streaming use cases due to the constraints imposed by its specification (Client-Sever, Stateless etc).
  • Payload : REST uses human readable formats (JSON, XML etc) and due to this the payload size is relatively larger and can cause impact on the applications that are very sensitive to network bandwidth (E.g. IOT devices).

Recommended Use Cases :

In general, REST can be used to used to model business capabilities that are not data intensive and streaming is not expected.

GraphQL

Description : GraphQL is a query language for APIs and a runtime for fulfilling those queries with your existing data. The client uses the GraphQL Query to specify the data fields that it needs and server returns the values in JSON response.

Key characteristics : Flexibility, Performance.

Message Format : GraphQL Query, JSON

Protocols: GraphQL over HTTP

Communication Channel : Unidirectional — Client to Server

Maturity : High

Strengths:

  • Field Level Control: Client have fine-grained control at field level as they can query specific data that they need. This fixes the problem of over-fetching and under-fetching leading to significant performance improvements.
  • Data Sources: GraphQL API can support various types of data sources — You can plug combination of REST APIs, Database Queries or File based SoR for providing the response. (Check Resolver)

Limitations :

  • Streaming : GraphQL doesn’t support streaming out of the box right now. P.S. The feature roadmap has proposal for streaming.

API Contract : GraphQL uses “Query” and “Mutation” for representing the API operations. This forces the developers to define the interfaces in “SOAP” style format.

Payload : Like REST, GraphQL uses human readable formats (JSON) and due to this the payload size is relatively bigger that can have impact on the applications which are very sensitive to network bandwidth (E.g. IOT devices).

Recommended Use Cases :

GraphQL is well suited for applications that needs to expose complex representations to different clients (Social media, Twitter). It can also be used for providing standard unified interface to client by hiding the underlying technology stack (REST, DB, File etc).

gRPC

Description: In gRPC, a client application can directly call a method on a server application on a different machine as if it were a local object, making it easier for you to create distributed applications and services.

Key characteristics : Performance. Streaming.

Message Format : Protocol Buffers (ProtoBuf)

Protocols: HTTP

Communication Channel : Bidirectional— Client to Server

Maturity : High

Strengths:

  • Streaming: gRPC have native support for streaming that makes it a preferred choice for streaming application as comparison to GraphQL and REST.
  • Payload : gRPC using binary encoding for request and response using Protobuff. This helps in reducing the payload size and helps in improving the network performance.

Limitations:

  • API Contract: Like GraphQL, gRPC also uses SOAP style contract for defining the business operations.

Recommended Use Cases :

gRPC is great fit for use cases where machine to machine interaction is required (e.g. Real-time dashboards and IoT).

References

Please provide your feedback in case if I have missed any important details.

--

--