GraphQL vs gRPC vs REST

GraphQL vs gRPC vs REST

#grapql #apis #rest

In the realm of APIs we often hear REST, GRPC and GraphQL used interchangeably by different stakeholders, and how using this approach or the other is the right way to design APIs. in this article we will discuss the differences between these concepts and what kind of problems these technologies were meant to solve.

What is REST

Representational state transfer or REST is an architectural style that describes a uniform interface between physically separate components(1) often across the internet in a client server architecture(1). APIs define an interface between two pieces of software.

Before REST there was other approaches to service to service communication like SOAP, but for reasons of practicality the REST approach ended up gaining the upper-hand.

A REST API is a service that allows you to perform actions on resources by sending requests in the form of URLs. HTTP and HTTPs are protocols for sending requests over the network which are then processed by the API which sends back responses to those requests. it uses JSON most of the time to serialize data between client and servers and HTTP methods to manipulate data over the network.

What is GraphQL

With classic rest APIs when clients request resources with complex data structures they might end up querying more than they actually need, so the GraphQL query language was introduced to mitigate this shortcoming, the GraphQL query language allows the clients to declare and retrieve data from APIs using a single expressive language. It's designed for maximum flexibility and can be used with any back-end technology (even gRPC). It allows you to write queries in the form of queries and mutations, which are then translated into HTTP requests and responses by the client library.

What is gRPC

gRPC is a protocol for high performance RPC (Remote procedure call) developed by Google and released in 2015, It's a form of client-server communication that allows you to communicate with other applications over the network using functions call rather than HTTP calls like REST. this abstracts the network from you, letting you call methods as if they were local code. gRPC uses Protocol buffers to serialize data between clients and servers

When to use what?

as we've seen in this article only REST and gRPC are architectural styles, GraphQL is only a query languages that can be used in combination with any of the above styles.

then when to use what?, let's start with GraphQL ; let's consider two uses cases. the first one is where you have one client application with many data sources. the second use case is when you have many client applications and one data source. when you have many multiple data sources for a single application, and you want to streamline those into a single interface. GraphQL is the perfect tool to unite multiple sources of data in a single layer. this layer allows you hide the complexity of the underlying services. The second uses case is when you have multiple clients that require access to the same data source but in different ways (eg A mobile and a web app). trying to have a REST API for both these interface can result in bloated endpoints with lots of data that is unnecessary to one or the other clients. Graphql allows both of these clients to use the data in a way that makes sense to them.

How about GRPC, gRPC is really shines when you're designing a low-latency, highly scalable distributed system, gRPC with its default serialization mechanism (Protobuf) is wire efficient (fast data transfer over the network w.r.t JSON). with protobuf you're not sending giant strings of payloads like REST and JSON. you're not spending resources in parsing those strings. if you're also building backend systems that include hundred or thousands of interconnected microservices, gRPC will offer efficiency and speed. it's an approach that shines when submillisecond latency is really important

REST shines when the endusers of the API are not known. in some organizations you have are at both the receiving and the giving end of the service so it might make sens to use

gRPC or GraphQL depending on your requirements. but when you don't know the end users, their proficiency level with the technology. the REST approach is the safe bet, it's an equalizer any one who needs to interact with APIs knows how to do it with the REST approach.

Conclusion

In this article we explored the REST, gRPC and GraphQL approaches to APIs. we compared their features and benefits and even discussed their use-cases scenarios. in light of this it is important to know how each of them works for your use case before falling for the current buzzword in tech.

要查看或添加评论,请登录

社区洞察

其他会员也浏览了