Rest VS gRPC
Tags: webdev, newbie, beginners, hasanelsherbiny
Published: 2023-09-10 07:28:00

Since the first appearance of API there has been many approaches to make it simpler and much powerful and easy to use.
if you don't know [what is API read this article](https://dev.to/hasanelsherbiny/what-is-api-21bc)

this is a quick comparison between REST And gRPC APIS

REST is an architectural style for designing networked applications. 
It is not a protocol, but a set of constraints and principles for creating web services.

REST APIs are built considering these concepts:
Resources: In REST, everything is a resource, which can be a physical object, a piece of data or a service. 
Each resource is identified by a uniqu URL.

1. HTTP Methods: 
REST uses standard HTTP methods (GET, POST, PUT, DELETE, etc.) to perform CRUD (Create, Read, Update, Delete) operations on resourcs

2. Stateless:
RESTful APIs are statelss, meaning each request from a client to the server must contain all the information needed to understand and fulfil the request

3.Representation: 
Resources can have multiple representations, such as JSON or XML, which clients can request based on their needs.

## Why To Choose REST?
Simplicity: REST is easy to understand and implement making it a popular choice for web APIs

Language Agnostic: Clients and servers can be written in different programming languages promoting interoperability.

Caching: REST leverage HTTP caching mechanisms for better performance.

Maturity: REST has been around for a long time and has widespread support in various web frameworks and tools.

## Drawbacks of REST
1. Overfetching / Underfetching: Clients may receive more or less data than they need impacting efficiency.

2. Latency: Multiple round-trips can result in higher latency when dealing with complex operations.

## So What Is gRPC?
gRPC is a high-performance, open-source framework developed by Google that allows you to define RPC (Remote Procedure Call) services and generate client-server code for multiple programming languages. It uses Protocol Buffers (protobufs) for data serialization and HTTP/2 for transport.
## Why To Choose gRPC?

1.IDL (Interface Definition Language): 
gRPC uses a language-agnostic IDL to define services and message types. These definitions can then be used to generate client and server code in multiple languages.

2.Strongly Typed: 
Data serialization with protobufs is strongly typed reducing the chances of runtime errors due to data mismatches.

3.HTTP/2: 
gRPC uses HTTP/2 for transport, offering features like multiplexing, flow control, and header compression which enhance performance.

## Why To Choose gRPC?
1.Bidirectional Streaming: 
gRPC supports bidirectional streaming allowing both the client and server to send a stream of messages.

2.Efficiency: 
gRPC's use of HTTP/2 and binary serialization makes it highly efficient in terms of bandwidth and latency.

3.Code Generation: 
gRPC automatically generates client and server code reducing the likelihood of human error.

4.Strong Typing: 
Protobufs ensure strong typing and versioning of data contracts.

5.Bidirectional Streaming: 
Ideal for real-time applications and chat systems.

## Drawbacks of gRPC
1.Complexity: 
The additional complexity of protobuf definitions and code generation can be challenging for some developers.

2.Learning Curve: 
Learning to work with gRPC may require additional time and effort.

## When To Choose REST OR gRPC?
This depends project's requirements and constraints but

## Use REST if:

- You need simplicity and quick development.
- Your clients and servers are implemented in different languages.
- You want to leverage the maturty and existing support of REST in web frameworks.

## Use gRPC if:

- You prioritize efficiency and low latency.
- You have strong typing and versioning requirements.
- Bidirectional streaming or real-time communication is essential.
- You want to benefit from code generation and maintainable APIs.